Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JCE and C# AES encryption compatibility

843811Mar 4 2005 — edited Aug 27 2007
What is the best way to pass the IV? Put it in the code itself for generate it from the secret key?

Java
package crypttest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class IDMCrypt {
	private static final byte[] initVectorData ={(byte)50,(byte)51,(byte)52,(byte)53,(byte)54,(byte)55,(byte)56,(byte)57};

	public static void main(String[] args) {
		try{
		String text = "password";
		String password = "test";
		String encrypted = encrypt(text,password);
		System.out.println(text + " encrypted is " + encrypted );
		String decrypted = decrypt(encrypted,password);
		System.out.println(encrypted + " decrypted is " + decrypted );
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	public static String encrypt(String text, String password) throws Exception{
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			
			//setup key
			byte[] keyBytes= new byte[16];
		  	byte[] b= password.getBytes("UTF-8");
		  	int len= b.length; 
		  	if (len > keyBytes.length) len = keyBytes.length;
		  	System.arraycopy(b, 0, keyBytes, 0, len);
			
			SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
			
			//the below may make this less secure, hard code byte array the IV in both java and .net clients
			IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
			 
			cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);
			byte [] results = cipher.doFinal(text.getBytes("UTF-8"));
			BASE64Encoder encoder = new BASE64Encoder();
			return encoder.encode(results);
	}
	
	public static String decrypt(String text, String password) throws Exception{
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			
			//setup key
			byte[] keyBytes= new byte[16];
		  	byte[] b= password.getBytes("UTF-8");
		  	int len= b.length; 
		  	if (len > keyBytes.length) len = keyBytes.length;
		  	System.arraycopy(b, 0, keyBytes, 0, len);
			SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
			IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
		    cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
			
			BASE64Decoder decoder = new BASE64Decoder();
			byte [] results = cipher.doFinal(decoder.decodeBuffer(text));
			return new String(results,"UTF-8");
			

	}

}
C#
using System;
using System.Security.Cryptography;
using System.Text;


namespace crypttest
{
	class CryptTest
	{
		static void Main(string[] args)
		{
			try
			{
				string text="password";
				string password="test";
				System.Console.WriteLine("encrypting " + text);
				string encrypted = encrypt(text,password);
				System.Console.WriteLine( text + " encrypted is " + encrypted);
				string decrypted = decrypt(encrypted,password);
				System.Console.WriteLine( encrypted + " decrypted is " + decrypted);
				
			}
			catch (Exception e)
			{
				System.Console.WriteLine(e.StackTrace);
			}

		}

		public static string encrypt(string text, string password) 
		{
			RijndaelManaged rijndaelCipher = new RijndaelManaged();

			rijndaelCipher.Mode = CipherMode.CBC;
			
			rijndaelCipher.Padding = PaddingMode.PKCS7;
		
			rijndaelCipher.KeySize = 128;

			rijndaelCipher.BlockSize = 128;

			byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);

			byte[] keyBytes = new byte[16];
			
			int len= pwdBytes.Length;
			
			if (len > keyBytes.Length) len= keyBytes.Length;
			
			System.Array.Copy(pwdBytes,keyBytes,len);

			rijndaelCipher.Key = keyBytes;
			
			rijndaelCipher.IV = keyBytes;

			ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

			byte [] plainText = Encoding.UTF8.GetBytes(text);

			byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
			
			return Convert.ToBase64String(cipherBytes);

		}

		public static string decrypt(string text, string password)

		{
			RijndaelManaged  rijndaelCipher = new RijndaelManaged();

			rijndaelCipher.Mode = CipherMode.CBC;

			rijndaelCipher.Padding = PaddingMode.PKCS7;

			rijndaelCipher.KeySize = 128;

			rijndaelCipher.BlockSize = 128;
			
			byte[] encryptedData = Convert.FromBase64String(text);

			byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);

			byte[] keyBytes = new byte[16];
			
			int len= pwdBytes.Length;
			
			if (len > keyBytes.Length) len= keyBytes.Length;
			
			System.Array.Copy(pwdBytes,keyBytes,len);
			
			rijndaelCipher.Key = keyBytes;
			
			rijndaelCipher.IV = keyBytes;

			ICryptoTransform transform = rijndaelCipher.CreateDecryptor();

			byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
			
			return Encoding.UTF8.GetString(plainText);

		}


	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 24 2007
Added on Mar 4 2005
11 comments
3,789 views