/** * Copyright (c) CI Wise Inc. All rights reserved. http://www.ciwise.com * The software in this package is published under the terms of the Apache * version 2.0 license, a copy of which has been included with this distribution * in the LICENSE.txt file. * */packagecom.ciwise.accounting.util;importjavax.crypto.Cipher;importjavax.crypto.spec.IvParameterSpec;importjavax.crypto.spec.SecretKeySpec;importjava.security.Key;/** * @author <a href="mailto:david@ciwise.com">David L. Whitehurst</a> * */publicclassAESCryptoUtil{/** * This method encrypts a string and returns a byte array of * the encrypted result. * * @param value * @return */publicstaticbyte[]encrypt(Stringvalue){byte[]encrypted=null;try{byte[]raw=newbyte[]{'A','b','c','D','E','f','g','z','4','x','9','9','0','O','c','j'};KeyskeySpec=newSecretKeySpec(raw,"AES");Ciphercipher=Cipher.getInstance("AES/CBC/PKCS5Padding");byte[]iv=newbyte[cipher.getBlockSize()];IvParameterSpecivParams=newIvParameterSpec(iv);cipher.init(Cipher.ENCRYPT_MODE,skeySpec,ivParams);encrypted=cipher.doFinal(value.getBytes());//System.out.println("encrypted string:" + encrypted.length);}catch(Exceptionex){ex.printStackTrace();}returnencrypted;}/** * @param encrypted * @return */publicstaticbyte[]decrypt(byte[]encrypted){byte[]original=null;Ciphercipher=null;try{byte[]raw=newbyte[]{'A','b','c','D','E','f','g','z','4','x','9','9','0','O','c','j'};Keykey=newSecretKeySpec(raw,"AES");cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");//the block size (in bytes), or 0 if the underlying algorithm is not a block cipherbyte[]ivByte=newbyte[cipher.getBlockSize()];//This class specifies an initialization vector (IV). Examples which use//IVs are ciphers in feedback mode, e.g., DES in CBC mode and RSA ciphers with OAEP encoding operation.IvParameterSpecivParamsSpec=newIvParameterSpec(ivByte);cipher.init(Cipher.DECRYPT_MODE,key,ivParamsSpec);original=cipher.doFinal(encrypted);}catch(Exceptionex){ex.printStackTrace();}returnoriginal;}}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.