Encryption between Android and C #

I use the following C # source code to encrypt plain text using AES (ECB 256):

public static string Encode(string PlainText) { byte[] Key = ASCIIEncoding.UTF8.GetBytes("12345678901234567890123456789012"); string encrypted = null; RijndaelManaged rj = new RijndaelManaged(); rj.BlockSize = 256; rj.KeySize = 256; rj.Key = Key; rj.GenerateIV(); byte[] IV = rj.IV; rj.Mode = CipherMode.ECB; rj.Padding = PaddingMode.Zeros; try { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write)) { using (StreamWriter sw = new StreamWriter(cs)) { sw.Write(PlainText); sw.Close(); sw.Dispose(); } cs.Close(); cs.Dispose(); } byte[] encryptArray = ms.ToArray(); encrypted = (Convert.ToBase64String(encryptArray)); ms.Close(); ms.Dispose(); } } catch (Exception ex) { throw ex; } finally { rj.Clear(); } return encrypted; } 

And I need to decrypt / encrypt data using the same algorithm, but I don't know how to do it.

Here is my Java class ( not working ):

  public static String encrypt(byte[] key, String cleartext, boolean base64) throws Exception { byte[] rawKey = key; byte[] result = encrypt(rawKey, cleartext.getBytes()); // Base 64 if (base64) return toBase64(result); // Hex return toHex(result); } public static String decrypt(byte[] key, String encrypted) throws Exception { byte[] rawKey = key; byte[] enc = toByte(encrypted); byte[] result = decrypt(rawKey, enc); return new String(result); } private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(clear); return encrypted; } 

Java call:

 encrypt("12345678901234567890123456789012".getBytes(), "Example Message", true); 

I do not know how I can choose the block size or PaddingMode.Zeros in Java.

ยฟAny idea?

Thanks in advance

+7
java android c # encryption
source share
3 answers

Yes, the problem is the 128-bit block size limit in AES (see "Strong cryptography with unlimited power").

Finally, I used GNU Crypto and it works !. I imported all the source code and I deleted the code that I am not using.

If someone wants clean source code , they only need to ask me.

Thanks for the help.

+3
source share

You should also consider Bouncy Castle, it is available for both C # and Java.

http://www.bouncycastle.org

+2
source share

Reading this article , it looks like you might need to use a version of Java that allows you to use cryptography with unlimited power so you can use the large size key (AES-192 and AES-256). They intentionally limit key lengths that can be used by default due to import restriction restrictions imposed by some countries. See the section โ€œStrong curve versus unlimited forceโ€ for more information.

+1
source share

All Articles