Interop Encryption / Decryption between Java / Net with AES and Point IV and Key

I am trying to implement symmetric encryption using AES, where I specify the key and IV. Within each ecosystem (Java → Java / .NET → .NET), encryption and decryption work the way they were designed, but if I try to go between them, I get a lot of errors regarding padding, etc. (Details below).

I read several posts, including http://blogs.msdn.com/b/dotnetinterop/archive/2005/01/24/java-and-net-aes-crypto-interop.aspx , but none of them have one the same use case. Here is my code:

Java

public class Main { public static void main(String[] args) throws Exception { String data = "Something to decrypt"; String encrypt = Encrypt(data); System.out.println(encrypt); String decrypt = Decrypt(encrypt); System.out.println(decrypt); } private static String Encrypt(String raw) throws Exception { Cipher c = getCipher(Cipher.ENCRYPT_MODE); byte[] encryptedVal = c.doFinal(raw.getBytes("UTF-8")); return new BASE64Encoder().encode(encryptedVal); } private static Cipher getCipher(int mode) throws Exception { Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding", new SunJCE()); //a random Init. Vector. just for testing byte[] iv = "e675f725e675f725".getBytes("UTF-8"); c.init(mode, generateKey(), new IvParameterSpec(iv)); return c; } private static String Decrypt(String encrypted) throws Exception { byte[] decodedValue = new BASE64Decoder().decodeBuffer(encrypted); Cipher c = getCipher(Cipher.DECRYPT_MODE); byte[] decValue = c.doFinal(decodedValue); return new String(decValue); } private static Key generateKey() throws Exception { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); char[] password = "Pass@word1".toCharArray(); byte[] salt = "S@1tS@1t".getBytes("UTF-8"); KeySpec spec = new PBEKeySpec(password, salt, 65536, 128); SecretKey tmp = factory.generateSecret(spec); byte[] encoded = tmp.getEncoded(); return new SecretKeySpec(encoded, "AES"); } } 

.NET

 internal class Program { private static void Main(string[] args) { string encrypted = Encrypt("Something to decrypt"); Console.Out.WriteLine(encrypted); string decrypted = Decrypt(encrypted); Console.Out.WriteLine(decrypted); Console.Out.WriteLine("Press any key to continue"); Console.ReadKey(); } private static string Encrypt(string raw) { using (var csp = new AesCryptoServiceProvider()) { ICryptoTransform e = GetCryptoTransform(csp, true); byte[] inputBuffer = Encoding.UTF8.GetBytes(raw); byte[] output = e.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length); string encrypted = Convert.ToBase64String(output); return encrypted; } } public static string Decrypt(string encrypted) { using (var csp = new AesCryptoServiceProvider()) { var d = GetCryptoTransform(csp, false); byte[] output = Convert.FromBase64String(encrypted); byte[] decryptedOutput = d.TransformFinalBlock(output, 0, output.Length); string decypted = Encoding.UTF8.GetString(decryptedOutput); return decypted; } } private static ICryptoTransform GetCryptoTransform(AesCryptoServiceProvider csp, bool encrypting) { csp.Mode = CipherMode.CBC; csp.Padding = PaddingMode.PKCS7; var passWord = "Pass@word1"; var salt = "S@1tS@lt"; //a random Init. Vector. just for testing String iv = "e675f725e675f725"; var spec = new Rfc2898DeriveBytes(Encoding.UTF8.GetBytes(passWord), Encoding.UTF8.GetBytes(salt), 65536); byte[] key = spec.GetBytes(16); csp.IV = Encoding.UTF8.GetBytes(iv); csp.Key = key; if (encrypting) { return csp.CreateEncryptor(); } return csp.CreateDecryptor(); } } 

Outputs for applications:

Java

 EO88Y8EjPAbaiZGoQM47z5i3vvy8jgVoehCPJDQES/4= Something to decrypt 

.NET

 T/m/GatOCvFEJ4frVIoY8CbuQ1av97HoKdhUhAZcXp0= Something to decrypt 

When I try to decrypt a .NET string in Java, I get: An exception in the stream "main" javax.crypto.BadPaddingException: this final block is not filled correctly.

In .NET, I get a similar one: System.Security.Cryptography.CryptographicException was not handled HResult = -2146233296 Message = Padding is invalid and cannot be deleted.

In .NET, I use Rfc2898DeriveBytes to generate my key based on password, salt, and number of iterations. I assume this is the root cause, as in Java I use SecretKeyFactory, hoping to achieve the same result. However, if that's the reason, I'm not sure how to reconcile the two ...?

Thanks for any help.

** UPDATED - SOLVED **

Boy, I feel dumber than usual. The problem was a typo in my code. The .NET salt value has a value of "1" and "L", where the value of the Java salt value has a value of "1" and "1". Fixing this error fixed the problem. Sorry to spend any time.

.NET

 var salt = "S@1tS@lt"; 

Java

 byte[] salt = "S@1tS@1t" 
+57
java c # interop aes encryption-symmetric
Jun 14 '13 at 16:25
source share

No one has answered this question yet.

See similar questions:

one
java AES code decryption in VB.net
0
error returning byte array
0
Equivalent code for System.Security.Cryptography in Java
-2
Java encryption and decryption in MS SQL Server

or similar:

2956
What is the difference between public, secure, batch, and private in Java?
663
Encrypt and decrypt a string in C #?
four
Java AES-128 encryption of 1 block (16 bytes) returns 2 blocks (32 bytes) as output
3
Symmetric encryption between .NET and Java
3
Decrypt encrypted DES data from Java to .Net
3
RSA encryption in Java / .NET and decryption in .NET.
one
Incorrect key Java AES decryption
one
jcryption 3.0 encryption for Java decryption
one
Java Encryption and C # Extension Problem
0
Error decrypting file in java that is in.Net encrypted using Rijndael



All Articles