자바 대칭 복호화(DES,3DES,ASE)와 BASE 64
4032 단어 Java
package fwk;
import java.io.IOException;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
/**
* this class is used to encrypt and decrypt
*
* @author fangwk
* @date 2016-8-3 14:11:27
*/
public class CipherUtil {
/**
* encrypt base on Base64
*
* @param target
* The target (which is used to encrypt) @return @throws
*/
public static String encryptByBase64(byte[] target) {
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(target);
}
/**
* decrypt base on Base64
*
* @param target
* The target (which is used to decrypt)
* @return
*/
public static byte[] decryptByBase64(String target) {
BASE64Decoder decoder = new BASE64Decoder();
try {
return decoder.decodeBuffer(target);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* encrypt base on DES or 3DES or AES.
*
* @param target
* The target (which is used to encrypt)
* @param algorithm
* The algorithm can be DES or DESede or AES
* @param key
* The key (which is key to handle this encrypt)
* @return
*/
public static String encrypt(String target, String algorithm, String key) {
try {
byte[] targetToByte = target.getBytes("UTF-8");
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, getKey(key, algorithm));
byte[] result = cipher.doFinal(targetToByte);
// System.out.println("base64:" +encryptByBase64(result));
return encryptByBase64(result); // BASE64 ,
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* decrypt base on DES or 3DES or AES.
*
* @param target
* The target (which is used to decrypt)
* @param algorithm
* The algorithm can be DES or DESede or AES
* @param key
* The key (which is key to handle this decrypt)
* @return
*/
public static String decrypt(String target, String algorithm, String key) {
try {
byte[] targetToByte = decryptByBase64(target); // BASE64 ,
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, getKey(key, algorithm));
byte[] result = cipher.doFinal(targetToByte);
// System.out.println("base:" + new String(result,"UTF-8"));
return new String(result, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Key getKey(String key, String algorithm) {
try {
KeyGenerator generator = KeyGenerator.getInstance(algorithm);
generator.init(new SecureRandom(key.getBytes()));
return generator.generateKey();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String target = " ";
// String target = "test";
// DES
String encrypt = encrypt(target, "DES", "im a key");
System.out.println(" :" + encrypt);
String decrypt = decrypt(encrypt, "DES", "im a key");
System.out.println(" :" + decrypt);
// 3DES
/*
* String encrypt = encrypt(target, "DESede", "im a key");
* System.out.println(" :" + encrypt); String decrypt =
* decrypt(encrypt, "DESede", "im a key"); System.out.println(" :" +
* decrypt);
*/
// AES
/*
* String encrypt = encrypt(target, "AES", "im a key");
* System.out.println(" :" + encrypt); String decrypt =
* decrypt(encrypt, "AES", "im a key"); System.out.println(" :" +
* decrypt);
*/
}
}
콘 솔:
암호 화 후:6Q2FSLKepBE= 복호화 후:테스트
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.