AES Encryption in Java
หน้า 1 จาก 1
AES Encryption in Java
- Code:
package win.text.util;
import java.security.MessageDigest;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* Utility class, providing AES encryption and decryption
* @required: bcprov-jdk16-146.jar, (http://www.java2s.com/Code/Jar/b/Downloadbcprovjdk16146jar.htm)
* @required: commons-codec-1.4.jar, (http://commons.apache.org/proper/commons-codec/download_codec.cgi)
* @author psupawa@gmail.com
*/
public class CipherUtils
{
// Declare variables
private Cipher cipher;
private MessageDigest digest;
private IvParameterSpec ivSpec;
private SecretKeySpec keySpec;
private String keyString = "thisIsASecretKeyLongString32bits";
private String ivString = "thisIsAnIV16bits";
/**
* Constructor
* @throws Exception
*/
public CipherUtils() throws Exception {
init();
}
/**
* Constructor
* @param keyString
* @param ivString
* @throws Exception
*/
public CipherUtils(String keyString, String ivString) throws Exception {
this.keyString = keyString;
this.ivString = ivString;
init();
}
/**
* Initialize system
* @throws Exception
*/
public void init() throws Exception {
Security.addProvider(new BouncyCastleProvider());
digest = MessageDigest.getInstance("SHA-256");
digest.update(keyString.getBytes());
byte[] key = new byte[ivString.length()];
System.arraycopy(digest.digest(), 0, key, 0, key.length);
keySpec = new SecretKeySpec(key, "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
IvParameterSpec zeroIv = new IvParameterSpec(new byte[ivString.length()]);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, zeroIv);
}
/**
* Encrypt the string
* @param strToEncrypt
* @return encrypted string
* @throws Exception
*/
public String encrypt(String strToEncrypt) throws Exception {
String encryptedString = null;
ivSpec = new IvParameterSpec(cipher.doFinal(ivString.getBytes()), 0, ivString.length());
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] encryptedByte = cipher.doFinal(strToEncrypt.getBytes());
encryptedString = Base64.encodeBase64String(encryptedByte);
return encryptedString.replaceAll("(\\r|\\n)", "");
}
/**
* Decrypt the string
* @param strToDecrypt
* @return decrypted string
* @throws Exception
*/
public String decrypt(String strToDecrypt) throws Exception {
String decryptedString = null;
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
return decryptedString.replaceAll("(\\r|\\n)", "");
}
/**
* @param args
*/
public static void main(String args[]) {
CipherUtils cu;
final String strToEncrypt = "HelloWorld";
String encryptedStr = null;
try {
System.out.println("Input : " + strToEncrypt);
cu = new CipherUtils();
encryptedStr = cu.encrypt(strToEncrypt);
System.out.println("Encrypted : " + encryptedStr);
String strToDecrypt = encryptedStr;
String decryptedStr = cu.decrypt(strToDecrypt);
System.out.println("Decrypted : " + decryptedStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
หน้า 1 จาก 1
Permissions in this forum:
คุณไม่สามารถพิมพ์ตอบ
|
|