非对称加密RSA
非对称加密又叫现代加密算法
非对称加密算法必须有两个密钥 公钥和私钥对
公钥加密 私钥解密
私钥加密 公钥解密
常见的非对称加密算法
RSA、ECC
package t2;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.apache.commons.io.FileUtils;
public class RSADemo {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException, IOException, InvalidKeySpecException {
// 定义原文
String input = "科学";
// 算法
String algorithm = "RSA";
// 创建密鈅对生成器对象
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
// 生成密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 生成私钥
PrivateKey privateKey = keyPair.getPrivate();
// 生成公钥
PublicKey publicKey = keyPair.getPublic();
// 保存公钥和私钥到文件中
generateKeyToFile(algorithm, "a.pub", "a.pri", privateKey, publicKey);
// 读取私钥
PrivateKey privateKey2 = getPrivateKey("a.pri", algorithm);
// 读取公钥
PublicKey publicKey2 = getPublicKey("a.pub", algorithm);
// 私钥加密
String encryptRsa = encryptRsa(algorithm, privateKey2, input);
System.out.println(encryptRsa);
// 公钥解密
String decryptRsa = decryptRsa(algorithm, publicKey2, encryptRsa);
System.out.println(decryptRsa);
}
// 读取私钥
private static PrivateKey getPrivateKey(String priPath, String algorithm)
throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
String privateStr = FileUtils.readFileToString(new File(priPath), Charset.defaultCharset());
System.out.println(privateStr);
// 创建key工厂
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
// 创建私钥key的规则
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateStr));
// 返回私钥对象
return keyFactory.generatePrivate(pkcs8EncodedKeySpec);
}
// 读取公钥
private static PublicKey getPublicKey(String pubPath, String algorithm)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
String publicStr = FileUtils.readFileToString(new File(pubPath), Charset.defaultCharset());
System.out.println(publicStr);
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
// 创建公钥规则
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicStr));
return keyFactory.generatePublic(x509EncodedKeySpec);
}
private static String decryptRsa(String algorithm, Key publicKey, String encrypted)
throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
// 创建加密对象
Cipher cipher = Cipher.getInstance(algorithm);
// 公钥解密
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] decode = Base64.getDecoder().decode(encrypted.getBytes());
byte[] bytes1 = cipher.doFinal(decode);
return new String(bytes1);
}
// 加密
private static String encryptRsa(String algorithm, Key privateKey, String input) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// 创建加密对象
Cipher cipher = Cipher.getInstance(algorithm);
// 对加密进行初始化
// 参数1 加密模式
// 参数2 公钥加密还是私钥加密
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
// 使用私钥加密
byte[] bytes = cipher.doFinal(input.getBytes());
// System.out.println(new String(Base64.getEncoder().encode(bytes)));
return new String(Base64.getEncoder().encode(bytes));
}
private static void generateKeyToFile(String algorithm, String pubPath, String priPath, Key privateKey,
Key publicKey) throws IOException, NoSuchAlgorithmException {
// 生成私钥的字节数组
byte[] privateKeyBytes = privateKey.getEncoded();
// 生成公钥字节数组
byte[] publicKeyBytes = publicKey.getEncoded();
// 使用base64进行编码
String privateEncodeString = new String(Base64.getEncoder().encode(privateKeyBytes));
String publicEncodeString = new String(Base64.getEncoder().encode(publicKeyBytes));
// 打印公钥私钥
// System.out.println(privateEncodeString);
// System.out.println(publicEncodeString);
FileUtils.writeStringToFile(new File(pubPath), publicEncodeString, Charset.forName("UTF-8"));
FileUtils.writeStringToFile(new File(priPath), privateEncodeString, Charset.forName("UTF-8"));
}
}