import com.yutu.pwd.util.HexUtils;
import org.junit.jupiter.api.Test;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
/**
* Aes加密
*/
public class AESTest {
private static final String UTF8 = StandardCharsets.UTF_8.name();
//加密算法名称
private static final String ALGORITHM = "AES";
//AES默认的长度只有16,24,32
private static final String KEY = "12345678abcdefgh";
@Test
public void S() throws Exception{
String str = "小汪学java";
//DES加密
String encrypt = encrypt(str);
System.out.println("16进制 + Aes 加密后的字符串: " + encrypt);
System.out.println("------------------------");
String decrypt = decrypt(encrypt);
System.out.println("16进制 + Aes 解密后的字符串: " + decrypt);
}
/**
* des加密
* @param text 待加密的内容
* @return
*/
private String encrypt(String text) throws Exception{
Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, KEY);
//加密
byte[] encodedBytes = cipher.doFinal(text.getBytes(UTF8));
return HexUtils.covertBytes2HexStr(encodedBytes);
}
/**
* 解密
* @param encodedStr 加密后的字符串
* @return
* @throws Exception
*/
private String decrypt(String encodedStr) throws Exception{
byte[] bytes = HexUtils.convertHex2Bytes(encodedStr);
Cipher cipher = Cipher.getInstance(ALGORITHM);
SecretKey secretKey = new SecretKeySpec(KEY.getBytes(UTF8), ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE,secretKey);//解密模式
//获取解码后的字节数组
byte[] decryptBytes = cipher.doFinal(bytes);
return new String(decryptBytes,UTF8);
}
/**
* 获取Cipher对象
* @param type 加解密模式
* @param seed 密钥key
* @return
*/
private Cipher getCipher(int type,String seed) throws Exception{
Cipher cipher = Cipher.getInstance(ALGORITHM);
SecretKey secretKey = new SecretKeySpec(seed.getBytes(UTF8), ALGORITHM);
cipher.init(type,secretKey);//解密模式
return cipher;
}
}
顺带也优化得了Des算法加解密,des的密钥长度位8位.
import org.apache.commons.codec.binary.Base64;
import org.junit.jupiter.api.Test;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
/**
* des加密
*/
public class DesTest {
private static final String UTF8 = StandardCharsets.UTF_8.name();
//加密算法名称
private static final String ALGORITHM = "DES";
private static final String KEY = "12345678";
@Test
public void S() throws Exception{
String str = "小汪学java";
//DES加密
String encrypt = encrypt(str);
System.out.println("base + des 加密后的字符串: " + encrypt);
System.out.println("------------------------");
String decrypt = decrypt(encrypt);
System.out.println("base + des 解密后的字符串: " + decrypt);
}
/**
* des加密
* @param text 待加密的内容
* @return
*/
private String encrypt(String text) throws Exception{
Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, KEY);
//加密
byte[] encodedBytes = cipher.doFinal(text.getBytes(UTF8));
//展示加密的字节数组,可以使用base64,也可以转为16进制字符串
return Base64.encodeBase64String(encodedBytes);
}
/**
* 解密
* @param encodedStr 加密后的字符串
* @return
* @throws Exception
*/
private String decrypt(String encodedStr) throws Exception{
Cipher cipher = getCipher(Cipher.DECRYPT_MODE, KEY);//解密模式
//获取解码后的字节数组
byte[] decryptBytes = cipher.doFinal(Base64.decodeBase64(encodedStr.getBytes(UTF8)));
return new String(decryptBytes,UTF8);
}
/**
* 获取Cipher对象
* @param type 加解密模式
* @param seed 密钥key
* @return
*/
private Cipher getCipher(int type,String seed) throws Exception{
Cipher cipher = Cipher.getInstance(ALGORITHM);
SecretKey secretKey = new SecretKeySpec(seed.getBytes(UTF8), ALGORITHM);
cipher.init(type,secretKey);//解密模式
return cipher;
}
}