javascript - react-native AES Encryption matching Java Decryption algorithm -
the full code of java encryption/decryption algorithm:
public class aesencryptutil { private static aesencryptutil instance = new aesencryptutil(); private string password = "123456"; private key key; private cipher cipher; public aesencryptutil(){ try { keygenerator kgen = keygenerator.getinstance("aes"); kgen.init(128, new securerandom(password.getbytes())); secretkey secretkey = kgen.generatekey(); byte[] encodeformat = secretkey.getencoded(); key = new secretkeyspec(encodeformat, "aes"); cipher = cipher.getinstance("aes"); } catch (exception e) { e.printstacktrace(); } } public static byte[] encrypt(string content) throws exception { byte[] bytecontent = content.getbytes("utf-8"); instance.cipher.init(cipher.encrypt_mode, instance.key); byte[] result = instance.cipher.dofinal(bytecontent); return result; } public static byte[] decrypt(byte[] content) throws exception { instance.cipher.init(cipher.decrypt_mode, instance.key); byte[] result = instance.cipher.dofinal(content); return result; } public static string parsebyte2hexstr(byte buf[]) { stringbuffer sb = new stringbuffer(); (int = 0; < buf.length; i++) { string hex = integer.tohexstring(buf[i] & 0xff); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.touppercase()); } return sb.tostring(); } public static byte[] parsehexstr2byte(string hexstr) { if (hexstr.length() < 1) return null; byte[] result = new byte[hexstr.length() / 2]; (int = 0; < hexstr.length() / 2; i++) { int high = integer.parseint(hexstr.substring(i * 2, * 2 + 1), 16); int low = integer.parseint(hexstr.substring(i * 2 + 1, * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } public static string getnonce() { string base = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; random random = new random(); stringbuffer sb = new stringbuffer(); (int = 0; < 16; i++) { int number = random.nextint(base.length()); sb.append(base.charat(number)); } return sb.tostring(); } public static void main(string[] args) throws exception { string content = "test"; system.out.println("content: " + content); byte[] encryptresult = encrypt(content); string encryptresultstr = parsebyte2hexstr(encryptresult); system.out.println("encryptresultstr: " + encryptresultstr); byte[] decryptfrom = parsehexstr2byte(encryptresultstr); byte[] decryptresult = decrypt(decryptfrom); system.out.println("decryptresult: " + new string(decryptresult)); } }
i've tried many times , many ways match java algorithm, result different. module should use ? can me deal ? lot !
i found right way match 2 algorithm:
java part:
public static string encrypt() throws exception { try { string data = "123456"; string key = "1234567812345678"; string iv = "1234567812345678"; cipher cipher = cipher.getinstance("aes/cbc/nopadding"); int blocksize = cipher.getblocksize(); byte[] databytes = data.getbytes(); int plaintextlength = databytes.length; if (plaintextlength % blocksize != 0) { plaintextlength = plaintextlength + (blocksize - (plaintextlength % blocksize)); } byte[] plaintext = new byte[plaintextlength]; system.arraycopy(databytes, 0, plaintext, 0, databytes.length); secretkeyspec keyspec = new secretkeyspec(key.getbytes(), "aes"); ivparameterspec ivspec = new ivparameterspec(iv.getbytes()); cipher.init(cipher.encrypt_mode, keyspec, ivspec); byte[] encrypted = cipher.dofinal(plaintext); return new sun.misc.base64encoder().encode(encrypted); } catch (exception e) { e.printstacktrace(); return null; } } public static string desencrypt() throws exception { string encrypted = encrypt() ; try { string data = encrypted ; string key = "1234567812345678"; string iv = "1234567812345678"; byte[] encrypted1 = new base64decoder().decodebuffer(data); cipher cipher = cipher.getinstance("aes/cbc/nopadding"); secretkeyspec keyspec = new secretkeyspec(key.getbytes(), "aes"); ivparameterspec ivspec = new ivparameterspec(iv.getbytes()); cipher.init(cipher.decrypt_mode, keyspec, ivspec); byte[] original = cipher.dofinal(encrypted1); string originalstring = new string(original); return originalstring; } catch (exception e) { e.printstacktrace(); return null; } }
react native part:
pre coding: npm install crypto-js
import cryptojs 'crypto-js' ; encryptfun() { var data = "123456"; var key = cryptojs.enc.latin1.parse('1234567812345678'); var iv = cryptojs.enc.latin1.parse('1234567812345678'); var encrypted = cryptojs.aes.encrypt( data, key, {iv:iv,mode:cryptojs.mode.cbc,padding:cryptojs.pad.zeropadding }); console.log('encrypted: ' + encrypted) ; var decrypted = cryptojs.aes.decrypt(encrypted,key,{iv:iv,padding:cryptojs.pad.zeropadding}); console.log('decrypted: '+decrypted.tostring(cryptojs.enc.utf8)); }
the result :
encrypted: ak7+ux24ttbgftnandz9aq== decrypted: 123456
i hope code someone:)
Comments
Post a Comment