views:

452

answers:

2

Hi, I got a module which RSA encrypts the data and passes on to the C#. C# needs to decrypt it based on the public key (64 bit encoded) and the passed token.

I have token , 64 bit encoded public key, can some help me get with the sample to get started. All I know from Java end is, it is using. I have got the result from Java end and need to write a parser in C# to decrypt this. I get both public key and token as a string value.

    Cipher cipher = Cipher.getInstance(ALGORITHM); //Algorithm = "RSA"
 cipher.init(Cipher.DECRYPT_MODE, key);

Thanks

+1  A: 
erickson
The encoding is done using the private key in Java side. I assume public key can be used in C# end to decrypt this?
bkhanal
Thanks. So, given I have public key (not the certificate) - I think I can get it,but the code is passing me the key from certificate instead, where do I start? :)...
bkhanal
A: 

Check this code out.

public static string Decrypt(string inputText)
      {
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
        byte[] encryptedData = Convert.FromBase64String(inputText.Replace(" ","+"));
        PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);

        using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
        {
          using (MemoryStream memoryStream = new MemoryStream(encryptedData))
          {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
            {
              byte[] plainText = new byte[encryptedData.Length];
              int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
              return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
            }
          }
        }
Henry Gao
RijndaelManaged : looks like a different algorithm to me?
bkhanal