1 相关小知识
1.1 常见加密算法
常见的开发加密算法包括对称密钥加密算法、非对称加密算法和其他数据算法。
- 对称密钥加密算法:使用同样的密钥来加密和解密数据,包括DES、3DES、AES等。其中,DES算法使用56位的密钥来加密数据,过程包括16个轮次的变换;3DES算法使用3个56位的密钥来加密数据,过程包括3个DES算法的轮次;AES算法使用128、192或256位的密钥来加密数据,过程包括轮密钥加、字节代换、行移位和列混淆等操作。
- 非对称加密算法:也称为公钥加密算法,包括RSA、SHA等。其中,SHA是一种较新的散列算法,可以对任意长度的数据运算生成一个160位的数值。
- 其他数据算法:包括一些常用编码算法及其与明文(ASCII、Unicode 等)转换等,如Base 64、Quoted Printable、EBCDIC等。
在开发过程中,可以根据具体需求选择合适的加密算法。
1.2 RSA加密算法
RSA加密算法是一种非对称加密算法,非常适用于现代数据加密和网络通信。这种算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥,即公钥,而两个大素数组合成私钥。公钥是可发布的供任何人使用,私钥则为自己所有,供解密之用。解密者拥有私钥,并且将由私钥计算生成的公钥发布给加密者。加密都使用公钥进行加密,并将密文发送到解密者,解密者用私钥解密将密文解码为明文。
1.3 RSA加密算法的原理是什么?
-
RSA加密算法的原理主要基于数论中的一些概念,如互质、欧拉函数、欧拉定理和模反元素等。这种算法利用了两个大素数相乘容易,而对其乘积进行因式分解困难的特点。
-
具体来说,RSA加密利用了单向函数正向求解简单,反向求解复杂的特性。在公开密钥加密和电子商业中RSA被广泛使用。RSA就是罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)三人姓氏开头字母拼在一起组成的。
-
RSA加密算法的公开密钥由两个大素数相乘得到,而私钥则由这两个大素数的特殊组合构成。加密过程是将明文信息使用公钥进行加密,生成密文;解密过程则是使用私钥对密文进行解密,还原成明文。由于公钥是可以公开的,因此任何人都可以使用公钥对信息进行加密,但只有拥有私钥的人才能解密信息,从而保证了信息的安全性。
-
小结:RSA加密算法利用了大素数相乘和因式分解困难的特点,通过公钥和私钥的配合使用,实现了对信息的加密和解密,保证了信息的安全性。
1.4 RES加密算法
-
RES(Resource Encryption Standard)加密算法是一种多轮迭代的方式进行加密的算法,它采用了轮函数(Round Function)和轮密钥生成(Round Key Generation)等步骤。
-
在RES算法中,初始置换(Initial Permutation)将输入的明文分组按照特定的规则进行初始置换,以增加密文的随机性和不可预测性。然后,每一轮的输入数据由上一轮的输出数据和轮密钥进行异或运算得到,经过一系列的置换、替代和混淆操作,得到当前轮的输出数据。这个过程是多轮迭代进行的,每轮迭代中都会使用不同的轮密钥。
-
在RES算法中,轮密钥生成是通过一个密钥扩展算法,根据输入的密钥生成多轮加密所需的轮密钥。轮密钥的长度通常比原始密钥长,以确保加密的安全性。
-
此外,RES算法只需要一套秘钥既可以完成加密/解密算法,并且公钥的秘钥长度明显的小于私钥的秘钥长度,支持“公钥加密,私钥解密”和“私钥加密,公钥解密”这两项加密解密原则。
-
小结:RES加密算法是一种具有特定结构和特点的加密算法,其使用的秘钥数量更少且算法较为简洁。
1.5 在线加解密工具
这里推荐几个在线加解密的工具
SOJson在线、MD5加密、优工具、在线加密解密
2 常规加密:
package com.xiaolu;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.*;
public class RSAUtil {
public static final String SIGN_ALGORITHMS = "SHA256WithRSA";
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
/** */
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 117;
/** */
/**
* RSA最大解密密文大小
*/
private static final int MAX_DECRYPT_BLOCK = 128;
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
return generator.generateKeyPair();
}
/**
* 签名
*
* @param content
* @param privateKey
* @return
*/
public static String sign(String content, String privateKey) {
try {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey));
KeyFactory keyf = KeyFactory.getInstance("RSA");
PrivateKey priKey = keyf.generatePrivate(priPKCS8);
Signature signature = Signature.getInstance(SIGN_ALGORITHMS);
signature.initSign(priKey);
signature.update(content.getBytes(DEFAULT_CHARSET));
byte[] signed = signature.sign();
return Base64.getEncoder().encodeToString(signed);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 验签
*
* @param content
* @param sign
* @param publicKey
* @return
*/
public static boolean verify(String content, String sign, String publicKey) {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = Base64.getDecoder().decode(publicKey);
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
Signature signature = Signature.getInstance(SIGN_ALGORITHMS);
signature.initVerify(pubKey);
signature.update(content.getBytes(DEFAULT_CHARSET));
return signature.verify(Base64.getDecoder().decode(sign));
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 公钥分段加密
*
* @param content
* @param publicKeyStr
* @return
* @throws Exception
*/
public static String publicEncrpyt(String content, String publicKeyStr) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(1, getPublicKey(publicKeyStr));
byte[] bytes = content.getBytes(DEFAULT_CHARSET);
int inputLen = bytes.length;
int offSet = 0;
byte[] cache;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int i = 0;
// 对数据分段加密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(bytes, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(bytes, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return Base64.getEncoder().encodeToString(encryptedData);
}
/**
* 私钥分段加密
*
* @param content
* @param privateKeyStr
* @return
* @throws Exception
*/
public static String privateEncrpyt(String content, String privateKeyStr) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(1, getPrivateKey(privateKeyStr));
byte[] bytes = content.getBytes(DEFAULT_CHARSET);
int inputLen = bytes.length;
int offSet = 0;
byte[] cache;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int i = 0;
// 对数据分段加密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(bytes, offSet, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(bytes, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return Base64.getEncoder().encodeToString(encryptedData);
}
/**
* 私钥分段解密
*
* @param content
* @param privateKeyStr
* @return
* @throws Exception
*/
public static String privateDecrypt(String content, String privateKeyStr) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(2, getPrivateKey(privateKeyStr));
byte[] bytes = Base64.getDecoder().decode(content);
int inputLen = bytes.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(bytes, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(bytes, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return new String(decryptedData);
}
/**
* 公钥分段解密
*
* @param content
* @param publicKeyStr
* @return
* @throws Exception
*/
public static String publicDecrypt(String content, String publicKeyStr) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(2, getPublicKey(publicKeyStr));
byte[] bytes = Base64.getDecoder().decode(content);
int inputLen = bytes.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(bytes, offSet, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(bytes, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return new String(decryptedData);
}
public static void main(String[] args) throws Exception {
String content = "SYxxkj123@";
// 公钥216
String publicKey ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDaLRe493205T6+xpTqD3q2xtLEut8GPS6ter1mp507S8MutmRJ8mn8oBykNVFz8ZKCsOGI1UJZS2jNpEBOuCTu7GXZ/uTCBmLDigS5rG3YzlMkg0EdnyKxTRRLjoPUyIWSVKGZlU5VC/zBvB03qKZ3HpzHZHxGG/3z6E8aoXoPRwIDAQAB";
//私钥844
// String privateKey ="";
String s = publicEncrpyt(content, publicKey);
System.out.println("公钥加密后"+s);
}
}
3 分段加密
package com.xiaolu;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class Demo {
public static BASE64Encoder base64Encoder = new BASE64Encoder();
public static BASE64Decoder base64Decoder = new BASE64Decoder();
// 公钥216
private static String publicKeyStr ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCaGn5+Yq9o7kxmDS2zwIREIEkcpKbx44uPT/PX22XOjgzkMvbCEamu7KyatNhkrrSH6E14l1F739WztXffJ/OCXUmgF4egMedyGCA8ymZWbzyhwnZcywa8me8S2rjb3gkCD6VFq8BSPJx9vaMvlR7eZxe1O5lllaV6tE6SlNY7QIDAQAB";
//私钥844
private static String privateKeyStr =
"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIJoafn5ir2juTGYNLbPAhEQgSRy" +
"kpvHji49P89fbZc6ODOQy9sIRqa7srJq02GSutIfoTXiXUXvf1bO1d98n84JdSaAXh6Ax53IYIDz" +
"KZlZvPKHCdlzLBryZ7xLauNveCQIPpUWrwFI8nH29oy+VHt5nF7U7mWWVpXq0TpKU1jtAgMBAAEC" +
"gYBOzKsk+s4EM5dnSXKo+ENmblOq43SFnzrh4+7X7vD4zZxCRH96NfEDNS4Qs45RSmmLKMOwHL2B" +
"0etfWBJSPisNZomhOBgTSfvHDSIFNbvVFqQBH5zEtxIB3bPrkW5X6EDF2BYCatC70Al21kmzHr2d" +
"BaL/c2p/4ueTdna/zKSKwQJBAMZLvFeyLi9jiIXXUzv7AkxF4SICtOOL56mKMkVlAXOWQgQe9FVs" +
"O8lTTK6GqqLrYe5cqgTiQqcLCP8/5n33oF0CQQCoW0ospTg8JEqStTmwemF8HfgxLU6YGj5kegD2" +
"Fq6HfCTq/z59THohbq6l+yoSxO3mjZb+7Zv1zu6oDR7YXFHRAkB66dEaDtFAAJNMWxc107Yt7xbI" +
"zSKw9TSoy4ezqhNHQXk0MrfDB27bsS2T9NdqWzr91CRzGIi2IEn4ZfSKWmblAkB2S1bOEfV2hMWF" +
"WiND9mnDDUfUPhKIW4BVl0hPodZWSouiN2DQJ8l07lF3PQjuEUNcCUb8rzYzvIgCut1eh1fRAkEA" +
"qBOEqBuRq9W7vrBlHbqEk1EXu2MfeRLZWljZn6uoyedJCdSh+6neUUvqHDGi3DNdss/ByWHI9e+9" +
"zOXTURmMaQ==";
private static RSAUtil ourInstance = new RSAUtil();
public static RSAUtil getInstance() {
return ourInstance;
}
// 生成密钥对
private static void generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator;
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 获取公钥,并以base64格式打印出来
PublicKey publicKey = keyPair.getPublic();
publicKeyStr = new String(base64Encoder.encode(publicKey.getEncoded()));
System.out.println("公钥:"+publicKeyStr);
// 获取私钥,并以base64格式打印出来
PrivateKey privateKey = keyPair.getPrivate();
privateKeyStr = new String(base64Encoder.encode(privateKey.getEncoded()));
System.out.println("私钥:"+privateKeyStr);
}
// 将base64编码后的公钥字符串转成PublicKey实例
private static PublicKey getPublicKey(String publicKey) throws Exception {
byte[] keyBytes = base64Decoder.decodeBuffer(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
}
// 将base64编码后的私钥字符串转成PrivateKey实例
private static PrivateKey getPrivateKey(String privateKey) throws Exception {
byte[] keyBytes = base64Decoder.decodeBuffer(privateKey);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
// 私钥加密
public static String encryptByPrivateKey(String content) throws Exception {
// 获取私钥
PrivateKey privateKey = getPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] cipherText = cipher.doFinal(content.getBytes());
String cipherStr = base64Encoder.encode(cipherText);
return cipherStr;
}
// 私钥解密
public static String decryptByPrivateKey(String content) throws Exception {
// 获取私钥
PrivateKey privateKey = getPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cipherText = base64Decoder.decodeBuffer(content);
byte[] decryptText = cipher.doFinal(cipherText);
return new String(decryptText);
}
// 公钥加密
public static String encryptByPublicKey(String content) throws Exception {
// 获取公钥
PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(content.getBytes());
String cipherStr = base64Encoder.encode(cipherText);
return cipherStr;
}
// 公钥解密
public static String decryptByPublicKey(String content) throws Exception {
// 获取公钥
PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] cipherText = base64Decoder.decodeBuffer(content);
byte[] decryptText = cipher.doFinal(cipherText);
return new String(decryptText);
}
public static void main(String[] args) throws Exception {
String data="SYxxkj123@";
System.out.println("初始数据:"+data);
// 公钥加密
String encryptedBytes = encryptByPublicKey(data);
System.out.println("公钥加密后:" + encryptedBytes);
// // 私钥解密
// String decryptedBytes = decryptByPrivateKey(encryptedBytes);
//
// System.out.println("私钥解密后:" + decryptedBytes);
// // 私钥加密
// String encryptedBytes2 = encryptByPrivateKey(data);
// System.out.println("私钥加密后:" + encryptedBytes2);
// // 公钥解密
// String decryptedBytes2 = decryptByPublicKey(encryptedBytes2);
// System.out.println("公钥解密后:" + decryptedBytes2);
generateKeyPair();
}
}
到此结束,下班咯!