Envryption interop between .NET and java

  • Follow


hi..
we have an application in dotnet that should encrypt a string in
dotnet and send it to a java application (both application web but
both need credentrial to get in, so the password will be encrypted in
dotnet side sent as URL paramter to the other java webapp where it
willl be decrypted and used)

so as a test i'm trying to create acode in dotnet and code in java
that should encrypt the same string and produce the same encrypted
byte array, in dotnet:

         string plainText = "This is a test";
           byte[] keySalt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16 };

            byte[] input = Encoding.Default.GetBytes(plainText);
            Console.WriteLine(getBytes(input));

            // INIT the CIPHER and key
            RijndaelManaged AesCipher = new
RijndaelManaged();

            AesCipher.Key = keySalt;

            // init vector, don't know what that is, otherwise the
array is always different
            AesCipher.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16 };
            ICryptoTransform crypt = AesCipher.CreateEncryptor();

            // ENCRYPT
            byte[] cipherText =crypt.TransformFinalBlock(input, 0,
input.Length);

            Console.WriteLine(getBytes(cipherText));

in java

                String msg="This is a test";
	   byte[] keySalt=new byte[]{1, 2, 3, 4, 5, 6,7, 8, 9,10, 11, 12, 13,
14, 15, 16};

	   byte[] input=msg.getBytes();
	   System.out.println(getBytes(input));

	   // INIT the CIPHER and key

	   Cipher aesChiper=Cipher.getInstance("AES");
	   Key key=new SecretKeySpec(keySalt,"AES");
	   aesChiper.init(Cipher.ENCRYPT_MODE,key);

	   // ENCRYPT
	   byte[] encBytes=  aesChiper.doFinal(msg.getBytes());

	   System.out.println(getBytes(encBytes));

but the array in both platform comes out differently..
why?

0
Reply elh.maayan (25) 8/29/2007 3:48:19 PM

i got it, in java init the IV goes like this:

	   IvParameterSpec spec=new IvParameterSpec(new byte[] { 0,0, 0, 0,
0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0 });
	   AlgorithmParameters prm=AlgorithmParameters.getInstance("AES");
	   prm.init(spec);

Cipher aesChiper=Cipher.getInstance("AES/CBC/PKCS5Padding");

	   Key key=new SecretKeySpec(keySalt,"AES");
	   aesChiper.init(Cipher.ENCRYPT_MODE,key,prm);

2nd, and here is the kicker:
bytes in dotnet are unsigned while in java are signed.. bummers so i
had to convert one of them ..
On Aug 29, 6:48 pm, elh.maa...@gmail.com wrote:
> hi..
> we have an application in dotnet that should encrypt a string in
> dotnet and send it to a java application (both application web but
> both need credentrial to get in, so the password will be encrypted in
> dotnet side sent as URL paramter to the other java webapp where it
> willl be decrypted and used)
>
> so as a test i'm trying to create acode in dotnet and code in java
> that should encrypt the same string and produce the same encrypted
> byte array, in dotnet:
>
>          string plainText = "This is a test";
>            byte[] keySalt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9,
> 10, 11, 12, 13, 14, 15, 16 };
>
>             byte[] input = Encoding.Default.GetBytes(plainText);
>             Console.WriteLine(getBytes(input));
>
>             // INIT the CIPHER and key
>             RijndaelManaged AesCipher = new
> RijndaelManaged();
>
>             AesCipher.Key = keySalt;
>
>             // init vector, don't know what that is, otherwise the
> array is always different
>             AesCipher.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
> 11, 12, 13, 14, 15, 16 };
>             ICryptoTransform crypt = AesCipher.CreateEncryptor();
>
>             // ENCRYPT
>             byte[] cipherText =crypt.TransformFinalBlock(input, 0,
> input.Length);
>
>             Console.WriteLine(getBytes(cipherText));
>
> in java
>
>                 String msg="This is a test";
>            byte[] keySalt=new byte[]{1, 2, 3, 4, 5, 6,7, 8, 9,10, 11, 12, 13,
> 14, 15, 16};
>
>            byte[] input=msg.getBytes();
>            System.out.println(getBytes(input));
>
>            // INIT the CIPHER and key
>
>            Cipher aesChiper=Cipher.getInstance("AES");
>            Key key=new SecretKeySpec(keySalt,"AES");
>            aesChiper.init(Cipher.ENCRYPT_MODE,key);
>
>            // ENCRYPT
>            byte[] encBytes=  aesChiper.doFinal(msg.getBytes());
>
>            System.out.println(getBytes(encBytes));
>
> but the array in both platform comes out differently..
> why?


0
Reply elh 8/29/2007 4:33:04 PM


1 Replies
291 Views

(page loaded in 0.059 seconds)


Reply: