赞
踩
pom
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-rsa</artifactId>
- <version>1.0.9.RELEASE</version>
- </dependency>
RsaUtils
- package com.example.demo.util;
-
- import org.apache.tomcat.util.codec.binary.Base64;
-
- import javax.crypto.Cipher;
- import java.security.*;
- import java.security.spec.PKCS8EncodedKeySpec;
- import java.security.spec.X509EncodedKeySpec;
-
- /**
- * Created by Eleven on 2022/10/28
- */
- public class RsaUtils {
-
- private static final String algorithm = "RSA";
-
- /**
- * 构建RSA密钥对
- *
- * @return 生成后的公私钥信息
- */
- public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
- KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
- keyPairGenerator.initialize(1024);
- KeyPair keyPair = keyPairGenerator.generateKeyPair();
- PublicKey publicKey = keyPair.getPublic();
- PrivateKey privateKey = keyPair.getPrivate();
- String publicKeyString = Base64.encodeBase64String(publicKey.getEncoded());
- String privateKeyString = Base64.encodeBase64String(privateKey.getEncoded());
- return new RsaKeyPair(publicKeyString, privateKeyString);
- }
-
- /**
- * 公钥加密
- *
- * @param publicKeyString 公钥
- * @param text 待加密的文本
- * @return 加密后的文本
- */
- public static String encryptByPublicKey(String publicKeyString, String text) throws Exception {
- X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
- KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
- PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
- Cipher cipher = Cipher.getInstance(algorithm);
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
- byte[] result = cipher.doFinal(text.getBytes());
- return Base64.encodeBase64String(result);
- }
-
- /**
- * 公钥解密
- *
- * @param publicKeyString 公钥
- * @param text 待解密的信息
- * @return 解密后的文本
- */
- public static String decryptByPublicKey(String publicKeyString, String text) throws Exception {
- X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
- KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
- PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
- Cipher cipher = Cipher.getInstance(algorithm);
- cipher.init(Cipher.DECRYPT_MODE, publicKey);
- byte[] result = cipher.doFinal(Base64.decodeBase64(text));
- return new String(result);
- }
-
- /**
- * 私钥加密
- *
- * @param privateKeyString 私钥
- * @param text 待加密的信息
- * @return 加密后的文本
- */
- public static String encryptByPrivateKey(String privateKeyString, String text) throws Exception {
- PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
- KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
- PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
- Cipher cipher = Cipher.getInstance(algorithm);
- cipher.init(Cipher.ENCRYPT_MODE, privateKey);
- byte[] result = cipher.doFinal(text.getBytes());
- return Base64.encodeBase64String(result);
- }
-
-
- /**
- * 私钥解密
- *
- * @param privateKeyString 私钥
- * @param text 待解密的文本
- * @return 解密后的文本
- */
- public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception {
- PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
- KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
- PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
- Cipher cipher = Cipher.getInstance(algorithm);
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
- byte[] result = cipher.doFinal(Base64.decodeBase64(text));
- return new String(result);
- }
-
-
- public static void main(String[] args) throws Exception {
- RsaUtils.RsaKeyPair rsaKeyPair = null;
- try {
- rsaKeyPair = RsaUtils.generateKeyPair();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- System.out.println("公钥:");
- System.out.println(rsaKeyPair.getPublicKey());
- System.out.println("私钥:");
- System.out.println(rsaKeyPair.getPrivateKey());
- String phone = "aaaaaaaaaaaaaaa";
- //公钥加密,私钥解密
- System.out.println("公钥加密,私钥解密");
- String phoneRsa = RsaUtils.encryptByPublicKey(rsaKeyPair.getPublicKey(), phone);
- System.out.println("加密后:" + phoneRsa);
- String phoneRsa2 = RsaUtils.decryptByPrivateKey(rsaKeyPair.getPrivateKey(), phoneRsa);
- System.out.println("解密后:" + phoneRsa2);
- //私钥加密,公钥解密
- System.out.println("私钥加密,公钥解密");
- String phoneRsa3 = RsaUtils.encryptByPrivateKey(rsaKeyPair.getPrivateKey(), phone);
- System.out.println("加密后:" + phoneRsa3);
- // phoneRsa3 = phoneRsa3.substring(0, phoneRsa3.length() - 2);// 测试完整性, 删除2字符 是否还能解密
- String phoneRsa4 = RsaUtils.decryptByPublicKey(rsaKeyPair.getPublicKey(), phoneRsa3);
- System.out.println("解密后:" + phoneRsa4);
- }
-
- static class RsaKeyPair {
- private String publicKey;
- private String privateKey;
-
- public RsaKeyPair(String publicKey, String privateKey) {
- this.publicKey = publicKey;
- this.privateKey = privateKey;
- }
-
- public String getPublicKey() {
- return publicKey;
- }
-
- public void setPublicKey(String publicKey) {
- this.publicKey = publicKey;
- }
-
- public String getPrivateKey() {
- return privateKey;
- }
-
- public void setPrivateKey(String privateKey) {
- this.privateKey = privateKey;
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。