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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

AES/Rijndael - Moving from .NET to Java

843811Dec 9 2008 — edited May 1 2010
I'm in the middle of converting a program from .NET to java, and I've been banging my head trying to get the java encryption algorithm to match the .NET algorithm. I'll admit, I know very little about .NET (and the guy who wrote the .NET code is long gone), so that's a big part of my problem.

I managed to get the password encryption converted to Java (an MD5 hash), but the credit card encryption piece is giving me fits. I'm sure I'm just missing some detail. I think it might have something to do with the PaddingMode defined in the .NET code, because I'm not really sure how to convert that over to Java-land. Could anyone help me out? Here's the .NET code below for the credit card decryption:

I can post the Java code I have so far if anyone thinks that'll help.

Thanks in advance!
public class Cryptographer {
	/// <summary>
	/// Use AES to encrypt data string. The output string is the encrypted bytes as a base64 string.
	/// The same password must be used to decrypt the string.
	/// </summary>
	/// <param name="data">Clear string to encrypt.</param>
	/// <param name="password">Password used to encrypt the string.</param>
	/// <returns>Encrypted result as Base64 string.</returns>
	public static string EncryptData(string data, string password, string salt) {
		if (data == null) throw new ArgumentNullException("data");
		if (password == null) throw new ArgumentNullException("password");

		byte[] encBytes = EncryptData(Encoding.UTF8.GetBytes(data), password, PaddingMode.ISO10126, salt);
		return Convert.ToBase64String(encBytes);
	}

	/// <summary>
	/// Decrypt the data string to the original string.  The data must be the base64 string
	/// returned from the EncryptData method.
	/// </summary>
	/// <param name="data">Encrypted data generated from EncryptData method.</param>
	/// <param name="password">Password used to decrypt the string.</param>
	/// <returns>Decrypted string.</returns>
	public static string DecryptData(string data, string password, string salt) {
		if (data == null) throw new ArgumentNullException("data");
		if (password == null) throw new ArgumentNullException("password");

		byte[] encBytes = Convert.FromBase64String(data);
		byte[] decBytes = DecryptData(encBytes, password, PaddingMode.ISO10126, salt);
		return Encoding.UTF8.GetString(decBytes);
	}

	public static byte[] EncryptData(byte[] data, string password, PaddingMode paddingMode, string salt) {
		if (data == null || data.Length == 0) throw new ArgumentNullException("data");
		if (password == null) throw new ArgumentNullException("password");

		PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, Encoding.UTF8.GetBytes(salt));
		RijndaelManaged rm = new RijndaelManaged();
		rm.Padding = paddingMode;
		ICryptoTransform encryptor = rm.CreateEncryptor(pdb.GetBytes(16), pdb.GetBytes(16));

		using (MemoryStream msEncrypt = new MemoryStream())
		using (CryptoStream encStream = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
			encStream.Write(data, 0, data.Length);
			encStream.FlushFinalBlock();
			return msEncrypt.ToArray();
		}
	}

	public static byte[] DecryptData(byte[] data, string password, PaddingMode paddingMode, string salt) {
		if (data == null || data.Length == 0) throw new ArgumentNullException("data");
		if (password == null) throw new ArgumentNullException("password");

		PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, Encoding.UTF8.GetBytes(salt));
		RijndaelManaged rm = new RijndaelManaged();
		rm.Padding = paddingMode;
		ICryptoTransform decryptor = rm.CreateDecryptor(pdb.GetBytes(16), pdb.GetBytes(16));

		using (MemoryStream msDecrypt = new MemoryStream(data))
		using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
			// Decrypted bytes will always be less then encrypted bytes, 
			//so len of encrypted data will be big enouph for buffer.
			byte[] fromEncrypt = new byte[data.Length];

			// Read as many bytes as possible.
			int read = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
			if (read < fromEncrypt.Length) {
				// Return a byte array of proper size.
				byte[] clearBytes = new byte[read];
				Buffer.BlockCopy(fromEncrypt, 0, clearBytes, 0, read);
				return clearBytes;
			}
			return fromEncrypt;
		}
	}
}

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 29 2010
Added on Dec 9 2008
18 comments
8,297 views