一,双向加密AES
1.创建Util类
package com.rtsm.zhjs.background.util;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class AESUtils {
//实例化密钥
private static Key key;
//原始密钥
private static String KEY_STR = "my-springmvc-2019-04-17";
//编码
private static String CHARSETNAME = "UTF-8";
//密钥算法
private static String KEY_ALGORITHM = "AES";
//加密-解密算法 / 工作模式 / 填充方式
private static String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
/**
* 初始化key
*/
static {
try {
KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY_STR.getBytes());
kgen.init(128, random);
key = kgen.generateKey();
kgen = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String getEncryptString(String str) {
try {
byte[] bytes = str.getBytes(CHARSETNAME);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(bytes);
return Base64.getEncoder().encodeToString(doFinal);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @description: 对AES加密字符串进行解密
* @param str
* @return
*/
public static String getDecryptString(String str) {
try {
byte[] bytes = Base64.getDecoder().decode(str);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(bytes);
return new String(doFinal, CHARSETNAME);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
2.编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class GcglDmpzTest {
@Test
public void jiamiTest() {
String password = "123456";
String pass = AESUtils.getEncryptString(password);
System.out.println("加密:"+pass);
System.out.println("解密:"+ AESUtils.getDecryptString(pass));
}
}
3.输出结果:
加密:Db9BY4b58pQm6eZ4pwVccg==
解密:123456
二,SHA256单向加密
1.util类
package com.rtsm.zhjs.background.util;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* SHA256 摘要算法工具类
* @author Administrator
*
*/
public class SHA256Util {
/**
* 利用java原生的摘要实现SHA256加密
* @param str 加密后的报文
* @return
*/
public static String getSHA256StrJava(String str){
MessageDigest messageDigest;
String encodeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
encodeStr = byte2Hex(messageDigest.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encodeStr;
}
/**
* 将byte转为16进制
* @param bytes
* @return
*/
private static String byte2Hex(byte[] bytes){
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i=0;i<bytes.length;i++){
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length()==1){
//1得到一位的进行补0操作
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}
}
2.测试
/**
* @author syx
* @date 2019/3/2015:54
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class GcglDmpzTest {
@Test
public void jiamiTest() {
String password = "123456";
String pass = SHA256Util.getSHA256StrJava(password);
System.out.println("加密:"+pass);
}
}
3.输出结果
加密:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92