If we have to save some configuration-related data into database table, our customer asks us to do encryption before we save into database if it is password-related information.
Some other function will retrieve user name and password to do authentication, we need to do decryption .
Solution
Here has sample code, using AES to do encryption and decryption:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | package albert.practice.encrypt; import java.security.GeneralSecurityException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; @Slf4j public class AESUtils { private static String secretKey = "your key"; public static String encryptText(String plainText) { byte[] raw; String encryptedString = ""; SecretKeySpec skeySpec; byte[] encryptText = plainText.getBytes(); Cipher cipher; raw = Base64.decodeBase64(secretKey); skeySpec = new SecretKeySpec(raw, "AES"); try { cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); encryptedString = Base64.encodeBase64String(cipher.doFinal(encryptText)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (IllegalBlockSizeException e) { throw new RuntimeException(e); } catch (BadPaddingException e) { throw new RuntimeException(e); } return encryptedString; } public static String decryptText(String encryptedText) { Cipher cipher; String encryptedString; byte[] encryptText = null; byte[] raw; SecretKeySpec skeySpec; raw = Base64.decodeBase64(secretKey); skeySpec = new SecretKeySpec(raw, "AES"); encryptText = Base64.decodeBase64(encryptedText); try { cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); encryptedString = new String(cipher.doFinal(encryptText)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (IllegalBlockSizeException e) { throw new RuntimeException(e); } catch (BadPaddingException e) { throw new RuntimeException(e); } return encryptedString; } public static void main(String[] args) throws NoSuchPaddingException, GeneralSecurityException { String plainText = "test123"; String encryptedText = AESUtils.encryptText(plainText); String decryptedText = AESUtils.decryptText(encryptedText); log.debug("plainText = " + plainText); log.debug("encryptedText = " + encryptedText); log.debug("decryptedText = " + decryptedText); } } |
Execution result:
1 2 3 | 2016-04-20 14:06:50.239 [main] DEBUG albert.practice.encrypt.AESUtils - plainText = test123 2016-04-20 14:06:50.255 [main] DEBUG albert.practice.encrypt.AESUtils - encryptedText = P25QKVFoX00OXs5GMOTAhg== 2016-04-20 14:06:50.255 [main] DEBUG albert.practice.encrypt.AESUtils - decryptedText = test123 |
Reference
[1] http://stackoverflow.com/questions/17567996/illegal-block-size-exception-input-length-must-be-multiple-of-16-when-decrypting
No comments:
Post a Comment