记录一些Java常用的编-解码和加-解密的使用
大部分情况下都是对二进制字节数组进行编-解码和加-解密操作,如果是字符串的话,通过String的getBytes方法,可以转成对应的字节数组,例如
byte[] bytes = "我是一段文本".getBytes(StandardCharsets.UTF_8);
示例都是使用jdk自带组件实现
base64
是将二进制数据转成可读的字符串,编码成为文本,便于处理,示例使用jdk自带Base64工具类,也可以用org.apache.commons.codec的工具类org.apache.commons.codec.binary.Base64
编码
import java.util.Base64;
public static String encode(byte[] clearData) {
return Base64.getEncoder().encodeToString(bytes);
}
解码
import java.util.Base64;
public static String decode(String base64Str) {
byte[] bytes = Base64.getDecoder().decode(base64Str);
return new String(bytes, StandardCharsets.UTF_8);
}
MD5
MD5是很常用的一种签名方法,可以用于密码的加密存储,数字签名,文件完整性验证等,
MD5算法是单向加密,md5加密后的密文,目前无法解密,安全系数相对来说比较高
加密签名
//使用org.apache.commons.codec.digest.DigestUtils
public static byte[] md5(byte[] data) {
return DigestUtils.md5(data);
}
//使用jdk自带方法:java.security.MessageDigest
public byte[] md5(byte[] data) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
return messageDigest.digest(data);
}
AES
AES是双向加密的算法,使用对称密钥(公钥私钥一样),明文加密后,密文可以使用密钥解密,AES需要一个密钥进行加密解密,AES支持三种长度的密钥:128位,192位,256位
生成密钥
public static byte[] generateKey() throws Exception {
//密钥生成器
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128, new SecureRandom());
// 生成秘密密钥
SecretKey secretKey = kg.generateKey();
// 获得密钥的二进制编码数据
return secretKey.getEncoded();
}
加密
public static byte[] encrypt(byte[] key, byte[] data) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
解密
public static String decrypt(byte[] key, byte[] data) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
解密后的数据可以自行转成字符串,new String(byte[]);
RSA
RSA是一种非对称加密算法,它有两个密钥:公钥和私钥。它们是一对,如果用公钥进行加密,只有用对应的私钥才能解密;如果用私钥进行加密,只有用对应的公钥才能解密。
一般情况下使用公钥加密,私钥解密
生成密钥对
/**
* 生成密钥对
*/
public static KeyPair generateKeyPair() throws Exception {
// 实例化密钥对生成器
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 初始化密钥对生成器,SecureRandom无需设置seed,用系统默认算法获取随机数
keyPairGen.initialize(KEY_SIZE, new SecureRandom());
// 生成密钥对
KeyPair keyPair = keyPairGen.generateKeyPair();
//公钥二进制数据
//byte[] publicKeyData = keyPair.getPublic().getEncoded();
//私钥二进制数据
//byte[] privateKeyData = keyPair.getPrivate().getEncoded();
return keyPair;
}
密钥可以使用base64编码后写入文件中保存起来
公钥加密
/**
* 公钥加密
*/
public static byte[] encryptWithPublicKey(byte[] data, byte[] publicKeyData) throws Exception {
//对公钥二进制数据进行编码
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyData);
//初始化密钥工厂,使用算法为RSA
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//生成公钥
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
// 对数据加密
Cipher cipher = Cipher.getInstance("RSA");
//设置为解密模式,密钥为公钥
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
私钥解密
/**
* 私钥解密
*/
public static byte[] decryptWithPrivateKey(byte[] data, byte[] privateKeyData) throws Exception {
//对私钥二进制数据进行编码
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyData);
//初始化密钥工厂,使用算法为RSA
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//生成私钥
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
// 对数据解密,使用算法RSA
Cipher cipher = Cipher.getInstance("RSA");
//设置为解密模式,密钥为私钥
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
本文暂时没有评论,来添加一个吧(●'◡'●)