Note that there are some explanatory texts on larger screens.

plurals
  1. PODecrypt a mesage using RSA in .NET - m**d mod p*q not working
    primarykey
    data
    text
    <p>I am experimenting with Mega encryption API, and I am stuck with the RSA decryption part.</p> <p>The JavaScript used is this:</p> <pre><code>// Compute m**d mod p*q for RSA private key operations. function RSAdecrypt(m, d, p, q, u) { var xp = bmodexp(bmod(m,p), bmod(d,bsub(p,[1])), p); var xq = bmodexp(bmod(m,q), bmod(d,bsub(q,[1])), q); var t=bsub(xq,xp); if(t.length==0) { t=bsub(xp,xq); t=bmod(bmul(t, u), q); t=bsub(q,t); } else { t=bmod(bmul(t, u), q); } return badd(bmul(t,p), xp); } </code></pre> <p>I am using .NET (4.0 with support for BigInteger) and I am trying to replicate the same behaviour. </p> <p>The RSA data I have is:</p> <pre><code>p (1024 bits) q (1024 bits) d (2044 bits) u (1024 bits) m (2044 bits) (the ciphered data) </code></pre> <p>I tried with RSACryptoServiceProvider with no luck. In other <a href="https://stackoverflow.com/questions/14576424/how-do-i-use-rsacryptoserviceprovider-to-decrypt-given-that-i-only-have-p-q-d">post</a> another user reported that he managed using the basic RSA decryption algorithm (m**d mod p*q) and avoiding RSACryptoServiceProvider.</p> <p>I implemented it (the U part is not necessary, I understand), made sure that the input was exact the same as the Javascript input, but still had no luck. The ouput is not the same as the expected by the Javascript code.</p> <p>This is the function implemented:</p> <pre><code>Public Shared Function RSA_Decrypt(ByVal P As Numerics.BigInteger, ByVal Q As Numerics.BigInteger, ByVal D As Numerics.BigInteger, ByVal M As Numerics.BigInteger) As Byte() Dim N As System.Numerics.BigInteger = Numerics.BigInteger.Multiply(P, Q) Dim DecryptedData As System.Numerics.BigInteger = Numerics.BigInteger.ModPow(M, D, N) Return DecryptedData.ToByteArray End Function </code></pre> <p>The test code:</p> <pre><code>Dim P As New Numerics.BigInteger(Convert.FromBase64String("gbb1FjTy...s=")) Dim Q As New Numerics.BigInteger(Convert.FromBase64String("o0fGo0v...8=")) Dim D As New Numerics.BigInteger(Convert.FromBase64String("GFVe9C...bQ==")) Dim Data As New Numerics.BigInteger(Convert.FromBase64String("BnrSc/..Dg==")) Dim ResultBytes As Byte() = cripto.RSA_Decrypt(P, Q, D, Data) Dim ResultStr As String = Convert.ToBase64String(ResultBytes) </code></pre> <p>This code returns:</p> <pre><code>Vd2jBCzObTx...QW1y+VRSZHAw== </code></pre> <p>However, the JavaScript function returns </p> <pre><code>LUyj3pyIyr4g...1aZU= </code></pre> <p>Do you have an idea what am I doing wrong, and how I can solve this? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload