title: using phpseclib's Crypt_RSA to encrypt verification code for a bank
body: <p>I am required to send a query to the bank which contains a verification code $vk_mac in a specified string format. The code has to be a SHA1 hash and RSA encrypted with my public key and presented in base64 format. Unfortunately, so far, I have been unsuccessful - the bank gives me "Wrong signature" and that all the info I'm getting. </p>
  
  <p>What I have is this:</p>
  
  <pre><code>$rsa = new Crypt_RSA();
  $rsa-&gt;loadKey(file_get_contents("private_key.pem"));
  $rsa-&gt;loadKey($rsa-&gt;getPublicKey());
  $rsa-&gt;setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
  $encrypted = $rsa-&gt;encrypt(sha1($vk_mac));
  $vk_mac = base64_encode($encrypted);
  </code></pre>
  
  <p>private_key.pem here is my private key in plain text. I tried setting the encryption mode to CRYPT_RSA_ENCRYPTION_OAEP without luck. I am 99.9% sure, that the starting $vk_mac string is formatted correctly and contains all the required details.</p>
  
  <p>Does anybody have any idea what can I be doing wrong? Thank you.</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>I've changed the code to this (where vk_mac is the starting formatted string that needs to be signed and private_key.pem is my the decoded private key):</p>
  
  <pre><code>$rsa = new Crypt_RSA();
  $rsa-&gt;loadKey(file_get_contents("private_key.pem"));
  $rsa-&gt;setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
  $hashed = $rsa-&gt;hash-&gt;hash($vk_mac);
  $encrypted = $rsa-&gt;sign($hashed);
  $signature = base64_encode($encrypted);
  </code></pre>
  
  <p>I can tell the generated signature is correct, since when I do this:</p>
  
  <pre><code>$rsa-&gt;loadKey($rsa-&gt;getPublicKey());
  $verified = $rsa-&gt;verify($hashed, base64_decode($signature));
  </code></pre>
  
  <p>$verified returns TRUE.</p>
  
  <p>The bank though, responds "Incorrect signature". Any more ideas?</p>
  
  <p><strong>Edit:</strong></p>
  
  <h2>Specification</h2>
  
  <blockquote>
    <p><strong>VK_MAC control code calculation</strong></p>
    
    <p>VK_MAC, for electronic signature, using in the request, for checking and confirming used version of the algorithm, indicated in the parameter VK_VERSION.  In this time version 008 is used. VK_MAC is presented as a request parameter in BASE64 coding.</p>
    
    <p><strong>Version 008</strong></p>
    
    <p>The value of the MAC008 function is calculated using the public key algorithm RSA. Values of empty fields are taken into account as well – “000”.</p>
    
    <p>MAC008(x1,x2,…,xn) := RSA(SHA-1(p(x1)|| x1|| p(x2 )|| x2 || … ||p(xn)||xn),d,n)</p>
    
    <p>Where:
    || is an operation of adding the string
    x1, x2, …, xn are the query parameters
    p is a function of the parameter length. The length is a number in the form of a three-digit string
    d is the RSA secret exponent 
    n is the RSA modulus
    The signature is calculated in accordance with the PKCS1 standard (RFC 2437).</p>
  </blockquote>