Sunday 15 September 2013

Preferred method for AES Encryption and Decryption in Java -


I have written the following two methods that encrypt and decrypt a given token:
  private static final string ALGORITHM_TYPE = "AES"; Private static final string CIPHER_TRANSFORMATION = "AES / CBC / PKCS 5 Padding"; Private static byte [] INITIALIZATION_VECTOR = new byte [] {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; Public string encryption (string token) {cipher cipher = null; Secret key = null; String tokenAshx = faucet; Byte [] encrypted token = faucet; Byte [] sksKey = getKeyAsByteArray (key); // Try the SecretKeySpec key {key = new secretive speak (scusk, algorithm_type); Algorithmparameterspap parasap = new IVPAMMPAC (initial size_aveter); Cipher = cipher Get Instances (CIPHRHRRAAFA); Cipher It (cipher NCRIPTMODEG, key, absolutecpeque); EncryptedToken = Cipher.DoPhinal (base64.xcodebus64 (token.getbits ("UTF-8")); } Hold (exception e) {new encryption exception (e); } Return Base64.encodeBase64String (encrypted token) .toLowerCase (); } Public string decrypt (string token) throws encryption expeditions {cipher cipher = faucet; Secret key = null; Byte [] decrypted token = faucet; Byte [] sksKey = getKeyAsByteArray (key); // Try the SecretKeySpec key {key = new secretive speak (scusk, algorithm_type); Algorithmparameterspap parasap = new IVPAMMPAC (initial size_aveter); Cipher = cipher Gate instances (CIPHRHRRAFFAF); Cipher It (cipher DECRAPTMODEG, key, absolutecpeque); DecryptedToken = cipher.dominal (base 64.decodeBase64 (token)); } Hold (exception e) {new encryption exception (e); } If (decryptedToken == null) {Removal of new encryption result ("Unable to decrypt the following token:" + token); } Return Base64.encodeBase64String (DecryptedToken); }   

However, I am unable to decrypt any encrypted strings successfully with the encrypt method. I discovered similar issues and found the closest here:. Even after using the same strategy, I'm still unable to decrypt the encrypted string.

In addition, I am encoding encrypted / decrypted byte array using Base64 instead of creating a new string in the form of the second result in an unsafe URL string. You are encrypting base 64-encoding, and then re-base-64-encoding, and decrying base 64-decoding, and then for some reason the base 64-encoding does not make sense You must be:

  1. Encryption base64-encoding, i.e. essentially return Base64.encode (cipher.doFinal (...))
  2. (1) To decrypt the base 64-decoding, that is basically the return cipher.domain (base 64.decode (...)) < / Html>

No comments:

Post a Comment