python and java encrypt with AES, but the result are not same -
java code:
public string encrypt (string str,string key) throws encryptexception { try{ javax.crypto.spec.secretkeyspec keyspec = new javax.crypto.spec.secretkeyspec(key.getbytes(), "aes"); javax.crypto.cipher c = javax.crypto.cipher.getinstance("aes"); c.init(javax.crypto.cipher.encrypt_mode, keyspec); byte[] src = str.getbytes("utf-8"); byte[] encrypt = c.dofinal(src); return new sun.misc.base64encoder().encode(encrypt).replaceall("\r|\n", ""); }catch(exception e){ throw new encryptexception("encrypt failed.",e); } }
python code:
def get_enctypted(self, param_req): bs = aes.block_size pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs) param_json = json.dumps(param_req) iv = random.new().read(aes.block_size) cipher = aes.new(self.get_key(), aes.mode_ecb, iv) encrypted = cipher.encrypt(pad(param_json)) encrypted_base64 = base64.b64encode(iv + encrypted) return encrypted_base64
when run code same key , string, 2 different result, know why?
take @ line - iv = random.new().read(aes.block_size)
. use random
value, of course results can't same.
Comments
Post a Comment