Porting RSA Public Key from Java to C# RRS feed

  • Question

  • Hello Everyone!

    I'm trying to do the following:

    1. Generate public/private keys in Java App

    2. Send public key to .NET application

    3. Encrypt password using public key and send back to Java App

    4. Decrypt password in Java App and verify

    I have developed a test scenario where I am using java to generate and encrypt information, and using the java key in .net to just encrypt information. Checking if both encrypted texts match or not will be enough. 

    What's happening currently is that when the java public key is sent to .NET (as a hard-coded 64string), the encrypted string generated by .NET is not the same as the one generated by Java.

    In the code below, [ i ] is replaced by Idea!!! :@

    Below is the java code......................

    private static byte[] stripLeadingZeros(byte[] a)


    int lastZero = -1;

    for (int i = 0; i < a.length; i++)


    if (aIdea == 0)


    lastZero = i;








    byte[] result = new byte[a.length-lastZero];

    System.arraycopy(a, lastZero, result, 0, result.length);

    return result;


    private static byte[] encrypt(byte[] inpBytes, PublicKey key, String xform) throws Exception


    Cipher cipher = Cipher.getInstance(xform);

    cipher.init(Cipher.ENCRYPT_MODE, key);

    return cipher.doFinal(inpBytes);


    private static byte[] decrypt(byte[] inpBytes, PrivateKey key, String xform) throws Exception


    Cipher cipher = Cipher.getInstance(xform);

    cipher.init(Cipher.DECRYPT_MODE, key);

    return cipher.doFinal(inpBytes);


    public static void main(String[] unused) throws Exception


    String xform = "RSA/NONE/PKCS1Padding";

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");

    kpg.initialize(512); // 512 is the keysize.

    KeyPair kp = kpg.generateKeyPair();

    PublicKey pubk = kp.getPublic();

    PrivateKey prvk = kp.getPrivate();

    BASE64Encoder b64e = new BASE64Encoder();

    RSAPublicKey rsaPublicKey = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(pubk.getEncoded()));

    // Send public key to user for encrypting password (.NET-compatible).

    byte[] modulusBytes = rsaPublicKey.getModulus().toByteArray();

    modulusBytes = stripLeadingZeros(modulusBytes);

    byte[] exponentBytes = rsaPublicKey.getPublicExponent().toByteArray();

    String modulusString = b64e.encode(modulusBytes);

    String exponentString = b64e.encode(exponentBytes);

    // export the modulusString and exponentString to .NET.





    byte[] dataBytes = "J2EE Security for Servlets, EJBs and Web Services".getBytes();


    System.out.println(new String(dataBytes));

    byte[] encBytes = encrypt(dataBytes, pubk, xform);

    System.out.println(".NET Encoded 64bitEncoding");


    //System.out.println(new String(encBytes));

    byte[] decBytes = decrypt(encBytes, prvk, xform);


    System.out.println(new String(decBytes));

    boolean expected = java.util.Arrays.equals(dataBytes, decBytes);

    for (int i = 0; i<dataBytes.length; i++)


    if (dataBytesIdea!=decBytesIdea)









    System.out.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));





    Here is the .NET Code........................


            public static byte[] encryptText(string strPlainText, byte[] keyPub, byte[] exponent)
                //Create a new instance of the RSACryptoServiceProvider class.
                RSACryptoServiceProvider rsaObj = new RSACryptoServiceProvider(512);

                //Create a new instance of the RSAParameters structure.
                RSAParameters rsaPars = new RSAParameters();

                 rsaPars.Modulus = Convert.FromBase64String("35X2lpAys+tyLvRw6YnYaSpQpsvy2NOhY6PwCHa7xgC1Z/fP+21nfzwMHLlozoDJjjNbqczpcsk2wY5oliWsXQ==");
                rsaPars.Exponent = Convert.FromBase64String("AQAB");
                Console.WriteLine("MODULUS in STRING FORM:>" + Convert.ToBase64String(rsaPars.Modulus));
                //Import key parameters into RSA.

                byte[] bytText = new byte[strPlainText.Length];

                for (int i = 0; i < strPlainText.Length; i++)
                    bytTextIdea = Convert.ToByte(strPlainTextIdea);

                byte[] bytEncText = rsaObj.Encrypt(bytText, false);

                return (bytEncText); // return bytes....


    Please help me out asap! I'm in a fix.



    Monday, January 8, 2007 7:52 AM