Android AES Encryption from C# to Java -


i converting c# encryption code android.

i facing issue not able encrypt text same c#. below copy paste both code.

both working code regarding using can use password & plain text .you find both have different output.

c# code

system.security.cryptography.rijndaelmanaged aes = new system.security.cryptography.rijndaelmanaged(); system.security.cryptography.md5cryptoserviceprovider hash_aes = new system.security.cryptography.md5cryptoserviceprovider();  final messagedigest hash_aes = messagedigest.getinstance("md5");          string encrypted = "";         try {             byte[] hash = new byte[32];             byte[] temp = hash_aes.computehash(system.text.asciiencoding.ascii.getbytes(pass));             final byte[] temp = hash_aes.digest(pass.getbytes("us-ascii"));              array.copy(temp, 0, hash, 0, 16);             array.copy(temp, 0, hash, 15, 16);             aes.key = hash;             aes.mode = system.security.cryptography.ciphermode.ecb;             system.security.cryptography.icryptotransform desencrypter = aes.createencryptor();             byte[] buffer = system.text.asciiencoding.ascii.getbytes(input);             encrypted = convert.tobase64string(desencrypter.transformfinalblock(buffer, 0, buffer.length));         } catch (exception ex) {          }         return encrypted; 

here android java code.

android java code

private static string transformation = "aes/ecb/nopadding"; private static string algorithm = "aes"; private static string digest = "md5"; byte[] encrypteddata;  public rijndaelcrypt(string password,string plaintext) {      try {          //encode digest         messagedigest digest;                    digest = messagedigest.getinstance(digest);                     _password = new secretkeyspec(digest.digest(password.getbytes()), algorithm);          //initialize objects         _cipher = cipher.getinstance(transformation);         _cipher.init(cipher.encrypt_mode, _password);         encrypteddata = _cipher.dofinal(text);      } catch (invalidkeyexception e) {         log.e(tag, "invalid key  (invalid encoding, wrong length, uninitialized, etc).", e);         return null;     } catch (invalidalgorithmparameterexception e) {         log.e(tag, "invalid or inappropriate algorithm parameters " + algorithm, e);         return null;     } catch (illegalblocksizeexception e) {         log.e(tag, "the length of data provided block cipher incorrect", e);         return null;     } catch (badpaddingexception e) {         log.e(tag, "the input data data not padded properly.", e);         return null;     }                     return base64.encodetostring(encrypteddata,base64.default); } 

should need use "us-ascii" in pass or take it?

  • use same mode of operation: either ecb or cbc
  • use same character set: it's best stick "utf-8"
  • use same key: in c# code you're doubling 128-bit key 256 bits

when using cbc random iv, expected ciphertext differs same plaintext. decryption operation determines whether succeeded.

note ecb not semantically secure. use cbc random iv. iv doesn't have secret, can prepend ciphertext , slice off before decryption.

it's better use authenticated mode gcm or eax or if it's not provided encrypt-then-mac scheme. it's hard implement correctly stick library rncryptor.


Comments