AES 복호화 실례
package com.bijian.study;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.axis.encoding.Base64;
public class AESTools {
private static String password = " complex password, ";
/**
*
*/
public static String getPassword() {
return password;
}
/**
*
* @param content
* @param password
* @return
*/
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(getKeyByStr(password));
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");//
byte[] byteContent = content.getBytes("UTF-8");
cipher.init(Cipher.ENCRYPT_MODE, key);//
byte[] result = cipher.doFinal(byteContent);
return result; //
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param content
* @param password
* @return
*/
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(getKeyByStr(password));
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");//
cipher.init(Cipher.DECRYPT_MODE, key);//
byte[] result = cipher.doFinal(content);
return result; //
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static byte[] getKeyByStr(String str) {
byte[] bRet = new byte[str.length() / 2];
for (int i = 0; i < str.length() / 2; i++) {
Integer itg = new Integer(16 * getChrInt(str.charAt(2 * i)) + getChrInt(str.charAt(2 * i + 1)));
bRet[i] = itg.byteValue();
}
return bRet;
}
public static int getChrInt(char chr) {
int iRet = 0;
if (chr == "0".charAt(0))
iRet = 0;
if (chr == "1".charAt(0))
iRet = 1;
if (chr == "2".charAt(0))
iRet = 2;
if (chr == "3".charAt(0))
iRet = 3;
if (chr == "4".charAt(0))
iRet = 4;
if (chr == "5".charAt(0))
iRet = 5;
if (chr == "6".charAt(0))
iRet = 6;
if (chr == "7".charAt(0))
iRet = 7;
if (chr == "8".charAt(0))
iRet = 8;
if (chr == "9".charAt(0))
iRet = 9;
if (chr == "A".charAt(0))
iRet = 10;
if (chr == "B".charAt(0))
iRet = 11;
if (chr == "C".charAt(0))
iRet = 12;
if (chr == "D".charAt(0))
iRet = 13;
if (chr == "E".charAt(0))
iRet = 14;
if (chr == "F".charAt(0))
iRet = 15;
return iRet;
}
public static String decryptString(String contents) {
return new String(decrypt(Base64.decode(contents), password));
}
public static String encryptString(String contents) {
return Base64.encode(encrypt(contents, password));
}
public static void main(String[] args) {
String text = " !Welcome to shenzhen!";
//
String encryptStr = AESTools.encryptString(text);
System.out.println("text length:" + text.length() + ",text:" + text);
System.out.println("encryptStr length:" + encryptStr.length() + ",encryptStr" + encryptStr);
System.out.println(" add length:" + (encryptStr.length() - text.length()));
//
String decryptStr = AESTools.decryptString(encryptStr);
System.out.println("decryptStr length:" + decryptStr.length() + ",decryptStr:" + decryptStr);
}
}
실행 결과:
text length:26,text: !Welcome to shenzhen!
encryptStr length:64,encryptStrujOEp9losb7mJx8a8Ht2nbCNaVtfCrAuKt9JRjTGdULpPYJOUr0nvzYQ9rvqWB6z
add length:38
decryptStr length:26,decryptStr: !Welcome to shenzhen!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.