Click here to Skip to main content
14,733,443 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have previously used a RSACryptoServiceProvider in C# to encrypt some data, and now I have to replicate this encryption in an Android program. I want my Android program to generate the same result as I got in my C# program.

Public Key:

<RSAKeyValue>        <Modulus>zz4qdc39y1BHyJgVXUkINJSbsUd1ZJPISyE9nNGjqgR+ZO1a4cE3ViVCSZCw+6dBdVMFNjzZPBxl0mT57GIq7rcuoT0scesZgxOftbMasPbxp0BGrh3HTpbBMJdCopgcYV98CZERakb8Pgbb0ne/DiW9Aq0kfTBE02/iEHRNuqMNfo1GFo55m0OKbxVoM6UBb8AITQ6lbdvfCgeIvMzRlVrHCwxUNrrX5cS6gurEfJ8Da+prKQmwWpFCkwDkPWje2W+bTSPUc9l6Ads0UimYE5sGs4Zsfz6Eocz4rJjR+qCiB8qt6HtdyjKo0auqYzyXIjdRv2950flc9tOh5bRlQQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

Java Encryption Programme:
byte[] modulusBytes = Base64.decode(Modoutput.getBytes("UTF-8"),Base64.DEFAULT);
byte[] exponentBytes = Base64.decode(Expoutput.getBytes("UTF-8"), Base64.DEFAULT);
BigInteger e = new BigInteger(1, exponentBytes);
BigInteger m = new BigInteger(1, modulusBytes);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKeyn = fact.generatePublic(keySpec);
Log.i("Publickey", pubKeyn.toString());
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, pubKeyn);
byte[] encryptedByteData = cipher.doFinal(byteData);
String outputEncrypted = Base64.encodeToString(encryptedByteData,Base64.NO_WRAP);
Log.i("Encrypteddata", outputEncrypted);

public static string EncryptText(string text, int keySize, string publicKeyXml)
           var encrypted = Encrypt(Encoding.UTF8.GetBytes(text), keySize, publicKeyXml);
           return Convert.ToBase64String(encrypted);

       public static byte[] Encrypt(byte[] data, int keySize, string publicKeyXml)
           if (data == null || data.Length == 0) throw new ArgumentException("Data are empty", "data");
           int maxLength = GetMaxDataLength(keySize);
           if (data.Length > maxLength) throw new ArgumentException(String.Format("Maximum data length is {0}", maxLength), "data");
           if (!IsKeySizeValid(keySize)) throw new ArgumentException("Key size is not valid", "keySize");
           if (String.IsNullOrEmpty(publicKeyXml)) throw new ArgumentException("Key is null or empty", "publicKeyXml");

           using (var provider = new RSACryptoServiceProvider(keySize))
               return provider.Encrypt(data, _optimalAsymmetricEncryptionPadding);

I tried the above code but it gives an entirely different output from C#. Can anyone tell me what is wrong with my code? Thanks in advance.
Updated 7-Mar-13 4:55am

1 solution


Try using BouncyCastle library with your Android program. They have Java & C# implementations and are interoperable.

AkshiD 8-Mar-13 0:46am
@Prasad:Thanks for your reply but can't we do without using third party library.
Prasad Khandekar 8-Mar-13 2:31am

You can definitely do this without third party libraries. But this library is really good.
AkshiD 8-Mar-13 6:29am

Okay Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900