DES, AES 암호화 해독 방법
DES
private static String strDefaultKey = "defineyourself";
/** */
private Cipher encryptCipher = null;
/** */
private Cipher decryptCipher = null;
/**
* byte 16 , :byte[]{8,18} :0813, public static byte[]
* hexStr2ByteArr(String strIn)
*
* @param arrB
* byte
* @return
* @throws Exception
*
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// byte ,
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
//
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 0F 0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
/**
* 16 byte , public static String byteArr2HexStr(byte[] arrB)
*
*
* @param strIn
* @return byte
* @throws Exception
*
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// , 2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/**
* ,
*
* @throws Exception
*/
public EncryptionDecryption() throws Exception {
this(strDefaultKey);
}
/**
*
*
* @param strKey
*
* @throws Exception
*/
public EncryptionDecryption(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/**
*
*
* @param arrB
*
* @return
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
/**
*
*
* @param strIn
*
* @return
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
/**
*
*
* @param arrB
*
* @return
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
/**
*
*
* @param strIn
*
* @return
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
try {
return new String(decrypt(hexStr2ByteArr(strIn)));
} catch (Exception e) {
return "";
}
}
/**
* , 8 8 0, 8 8
*
* @param arrBTmp
*
* @return
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 8 ( 0)
byte[] arrB = new byte[8];
// 8
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
//
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
AES private final static String encoding = "UTF-8";
/**
* AES
*
* @param content
* @param password
* @return
*/
public static String encryptAES(String content, String password) {
byte[] encryptResult = encrypt(content, password);
String encryptResultStr = parseByte2HexStr(encryptResult);
// BASE64
encryptResultStr = ebotongEncrypto(encryptResultStr);
return encryptResultStr;
}
/**
* AES
*
* @param encryptResultStr
* @param password
* @return
*/
public static String decrypt(String encryptResultStr, String password) {
// BASE64
String decrpt = ebotongDecrypto(encryptResultStr);
byte[] decryptFrom = parseHexStr2Byte(decrpt);
byte[] decryptResult = decrypt(decryptFrom, password);
return new String(decryptResult);
}
/**
*
*/
public static String ebotongEncrypto(String str) {
BASE64Encoder base64encoder = new BASE64Encoder();
String result = str;
if (str != null && str.length() > 0) {
try {
byte[] encodeByte = str.getBytes(encoding);
result = base64encoder.encode(encodeByte);
} catch (Exception e) {
e.printStackTrace();
}
}
//base64
return result.replaceAll("\r
", "").replaceAll("\r", "").replaceAll("
", "");
}
/**
*
*/
public static String ebotongDecrypto(String str) {
BASE64Decoder base64decoder = new BASE64Decoder();
try {
byte[] encodeByte = base64decoder.decodeBuffer(str);
return new String(encodeByte);
} catch (IOException e) {
e.printStackTrace();
return str;
}
}
/**
*
*
* @param content
* @param password
* @return
*/
private static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
// linux key
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom);
//kgen.init(128, new SecureRandom(password.getBytes()));
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
*/
private static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
// linux key
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom);
//kgen.init(128, new SecureRandom(password.getBytes()));
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;
}
/** 16
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/** 16
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
역암호화된gem lockbox의 버튼 연결에 걸려 넘어진 일안녕하세요. 최근 프로젝트에서는 은밀한 정보를 처리해 데이터베이스에 역암호화해 저장하기로 했다. 최초 사용 예정 나는 기술 고문과 상의했다 "Message Enceryptor를 사용하는 것이 좋을 것 같지만 요소안 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.