i have found nice little bit of code here implements cross platform encryption. in case c# , java.
i have implemented in android version 4.4.2 (avd) , works charm. have since moved android 5.0.1 (samsung) , fails decrypt other platform has encrypted. on both sides java , c# produce padding error.
what has changed in android versions cause error?
the code being used below:
java code:
private final string characterencoding = "utf-8"; private final string ciphertransformation = "aes/cbc/pkcs5padding"; private final string aesencryptionalgorithm = "aes"; public byte[] decrypt(byte[] ciphertext, byte[] key, byte [] initialvector) throws nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception, invalidalgorithmparameterexception, illegalblocksizeexception, badpaddingexception { cipher cipher = cipher.getinstance(ciphertransformation); secretkeyspec secretkeyspecy = new secretkeyspec(key, aesencryptionalgorithm); ivparameterspec ivparameterspec = new ivparameterspec(initialvector); cipher.init(cipher.decrypt_mode, secretkeyspecy, ivparameterspec); ciphertext = cipher.dofinal(ciphertext); return ciphertext; } public byte[] encrypt(byte[] plaintext, byte[] key, byte [] initialvector) throws nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception, invalidalgorithmparameterexception, illegalblocksizeexception, badpaddingexception { cipher cipher = cipher.getinstance(ciphertransformation); secretkeyspec secretkeyspec = new secretkeyspec(key, aesencryptionalgorithm); ivparameterspec ivparameterspec = new ivparameterspec(initialvector); cipher.init(cipher.encrypt_mode, secretkeyspec, ivparameterspec); plaintext = cipher.dofinal(plaintext); return plaintext; } private byte[] getkeybytes(string key) throws unsupportedencodingexception{ byte[] keybytes= new byte[16]; byte[] parameterkeybytes= key.getbytes(characterencoding); system.arraycopy(parameterkeybytes, 0, keybytes, 0, math.min(parameterkeybytes.length, keybytes.length)); return keybytes; } /// <summary> /// encrypts plaintext using aes 128bit key , chain block cipher , returns base64 encoded string /// </summary> /// <param name="plaintext">plain text encrypt</param> /// <param name="key">secret key</param> /// <returns>base64 encoded string</returns> public string encrypt(string plaintext, string key) throws unsupportedencodingexception, invalidkeyexception, nosuchalgorithmexception, nosuchpaddingexception, invalidalgorithmparameterexception, illegalblocksizeexception, badpaddingexception{ byte[] plaintextbytes = plaintext.getbytes(characterencoding); byte[] keybytes = getkeybytes(key); return base64.encodetostring(encrypt(plaintextbytes,keybytes, keybytes), base64.default); } /// <summary> /// decrypts base64 encoded string using given key (aes 128bit key , chain block cipher) /// </summary> /// <param name="encryptedtext">base64 encoded string</param> /// <param name="key">secret key</param> /// <returns>decrypted string</returns> public string decrypt(string encryptedtext, string key) throws keyexception, generalsecurityexception, generalsecurityexception, invalidalgorithmparameterexception, illegalblocksizeexception, badpaddingexception, ioexception{ byte[] cipheredbytes = base64.decode(encryptedtext, base64.default); byte[] keybytes = getkeybytes(key); return new string(decrypt(cipheredbytes, keybytes, keybytes), characterencoding); }
c# code:
public rijndaelmanaged getrijndaelmanaged(string secretkey) { var keybytes = new byte[16]; var secretkeybytes = encoding.utf8.getbytes(secretkey); array.copy(secretkeybytes, keybytes, math.min(keybytes.length, secretkeybytes.length)); return new rijndaelmanaged { mode = ciphermode.cbc, padding = paddingmode.pkcs7, keysize = 128, blocksize = 128, key = keybytes, iv = keybytes }; } public byte[] encrypt(byte[] plainbytes, rijndaelmanaged rijndaelmanaged) { return rijndaelmanaged.createencryptor() .transformfinalblock(plainbytes, 0, plainbytes.length); } public byte[] decrypt(byte[] encrypteddata, rijndaelmanaged rijndaelmanaged) { return rijndaelmanaged.createdecryptor() .transformfinalblock(encrypteddata, 0, encrypteddata.length); } /// <summary> /// encrypts plaintext using aes 128bit key , chain block cipher , returns base64 encoded string /// </summary> /// <param name="plaintext">plain text encrypt</param> /// <param name="key">secret key</param> /// <returns>base64 encoded string</returns> public string encrypt(string plaintext, string key) { var plainbytes = encoding.utf8.getbytes(plaintext); return convert.tobase64string(encrypt(plainbytes, getrijndaelmanaged(key))); } /// <summary> /// decrypts base64 encoded string using given key (aes 128bit key , chain block cipher) /// </summary> /// <param name="encryptedtext">base64 encoded string</param> /// <param name="key">secret key</param> /// <returns>decrypted string</returns> public string decrypt(string encryptedtext, string key) { var encryptedbytes = convert.frombase64string(encryptedtext); return encoding.utf8.getstring(decrypt(encryptedbytes, getrijndaelmanaged(key))); }
thanks
Comments
Post a Comment