首页 > 其他分享 >RSA 对称加密,对称解密----公钥私钥加密解密过程

RSA 对称加密,对称解密----公钥私钥加密解密过程

时间:2022-09-26 11:36:35浏览次数:58  
标签:加密 String System 解密 println 对称 私钥

RSA 对称加密,对称解密----公钥私钥加密解密过程(Java)     公司说不能传铭文密码,所以只能加密,再解密;麻烦事,其实这在需求文档没有,开发时间点也没有,浪费了了一上午的时间,还占用了公司给的开发计划时间,搞不好又怪我们偷懒,没按照时间完成;                就自己加了加密解密;  1、公钥就是大家都知道的公开的,私钥就是自己知道,不公开的;所以这里有两种方式1、公钥对明文加密,私钥进行解密;2、私钥对明文加密,公钥进行解密;一下直接贴代码吧;      


import org.springframework.util.Base64Utils;
import javax.crypto.Cipher;
import java.net.URLDecoder;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.*;

import org.apache.commons.codec.binary.Base64;
import java.util.concurrent.ConcurrentHashMap;

/**
*
*/
public class RsaUtilClient {
public static final String KEY_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
private static final String PUBLIC_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
private static final String PRIVATE_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private static volatile ConcurrentHashMap<String,Key> KEY_MAP = new ConcurrentHashMap<>(2);

private static final String src = "admin4564";

public static void main(String[] args) throws Exception {

//公钥加密私钥解密
String text1 = encryptByPublicKey(PUBLIC_KEY, src);
String text11 = "BSEaCC/B0C9DRxkFXXg1jCuKvsE6TtLhkv3vb/B3WWHBr2uoBci2+WB2ge2ZuBAvU2R5HZCYPvQubCQiioQn0Q==";
String text2 = decryptByPrivateKey(PRIVATE_KEY, text11);
System.out.println(text1);
//私钥加密公钥解密
// String text1 = encryptByPrivateKey(PRIVATE_KEY, src);
/* String text2 = decryptByPublicKey(PUBLIC_KEY, text11);
System.out.println(text2);*/
}

/**
* 公钥加密私钥解密
*/
private static void test1(RSAKeyPair keyPair, String source) throws Exception {
System.out.println("***************** 公钥加密私钥解密开始 *****************");
String text1 = encryptByPublicKey(keyPair.getPublicKey(), source);
String text2 = decryptByPrivateKey(keyPair.getPrivateKey(), text1);
System.out.println("加密前:" + source);
System.out.println("加密后:" + text1);
System.out.println("解密后:" + text2);
if (source.equals(text2)) {
System.out.println("解密字符串和原始字符串一致,解密成功");
} else {
System.out.println("解密字符串和原始字符串不一致,解密失败");
}
System.out.println("***************** 公钥加密私钥解密结束 *****************");
}

/**
* 私钥加密公钥解密
*
* @throws Exception
*/
private static void test2(RSAKeyPair keyPair, String source) throws Exception {
System.out.println("***************** 私钥加密公钥解密开始 *****************");
String text1 = encryptByPrivateKey(keyPair.getPrivateKey(), source);
String text2 = decryptByPublicKey(keyPair.getPublicKey(), text1);
System.out.println("加密前:" + source);
System.out.println("加密后:" + text1);
System.out.println("解密后:" + text2);
if (source.equals(text2)) {
System.out.println("解密字符串和原始字符串一致,解密成功");
} else {
System.out.println("解密字符串和原始字符串不一致,解密失败");
}
System.out.println("***************** 私钥加密公钥解密结束 *****************");
}

/**
* 公钥解密
*
* @param publicKeyText
* @param text
* @return
* @throws Exception
*/
public static String decryptByPublicKey(String publicKeyText, String text) throws Exception {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
return new String(result);
}

/**
* 私钥加密
*
* @param privateKeyText
* @param text
* @return
* @throws Exception
*/
public static String encryptByPrivateKey(String privateKeyText, String text) throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(text.getBytes());
return Base64.encodeBase64String(result);
}

/**
* 私钥解密
*
* @param privateKeyText
* @param text
* @return
* @throws Exception
*/
public static String decryptByPrivateKey(String privateKeyText, String text) throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(PRIVATE_KEY));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
return new String(result);
}

/**
* 公钥加密
*
* @param publicKeyText
* @param text
* @return
*/
public static String encryptByPublicKey(String publicKeyText, String text) throws Exception {
X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(text.getBytes());
return Base64.encodeBase64String(result);
}

/**
* 构建RSA密钥对
*
* @return
* @throws NoSuchAlgorithmException
*/
public static RSAKeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
RSAKeyPair rsaKeyPair = new RSAKeyPair(publicKeyString, privateKeyString);
return rsaKeyPair;
}


/**
* RSA密钥对对象
*/
public 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 String getPrivateKey() {
return privateKey;
}

}


}
 

        

                                                                                                             写作不易,如果对你有用,觉得好可以给小编打个赏: 

                                                 

 

标签:加密,String,System,解密,println,对称,私钥
From: https://www.cnblogs.com/luojiesheng/p/16730222.html

相关文章

  • 传统加密技术:代替技术、置换技术
    2.传统加密技术2.1对称密码模型明文Messege\X加密算法Encrypt密钥Key密文Cyphertext\Y解密算法Decrypt\[c=E(k,m)\\m=D(k,c)\\D(k,E(k,m))=m\\\]2......
  • AES 加盐加密明文传输数据
    可以直接使用的工具类publicclassDataEncryptUtils{/**......
  • bcrypt 密码加密
    哈希加密是单程加密方式:1234=>abcd  在加密的密码中加入随机字符串可以增加密码被破解的难度//导入bcrypt模块constbcrypt=require('bcrypt')//生成随机字符......
  • AES加密的两种方式ECB与CBC
    AES为对称加密ECB只需要秘钥key、而CBC需要秘钥key和iv,相当于需要两把钥匙。本文使用的是JavaScript代码实现CBC实现代码如下functiongetAesString(data,key,iv){//加......
  • 力扣101 对称二叉树
        class Solution {public:    bool isSymmetric(TreeNode* root) {    if (root == nullptr)        return true;    retur......
  • python解决b 视频加密
    代码很简单:1importsys2frompathlibimportPath3fromnumpyimportfromfile,uint8#p......
  • C#:多态之虚方法、抽象类、接口、 类的序列化、MD5加密。
     (总的来说多态的作用便是解决代码的冗余问题,但代码更加具有可读性,更加的简洁)多态的第一种表现形式:虚方法usingSystem;usingSystem.Collections.Generic;usingSystem......
  • CAS5.3自定义加密方法获取用户名
    CAS5自定义加密方法获取用户名项目用到CAS单点登录,根据用户名自定义加密方式1.添加pom.xml依赖<dependency><groupId>javax.servlet</......
  • 【Springboot之切面编程】注解实现敏感字段加解密
    当你的项目如果不允许明文存储敏感数据(例如身份证号、银行卡号,手机号等),那么每次存之前都要先将相关敏感字段数据加密、读取出来都要将相应敏感字段的数据解密,这种方式低效......
  • navicat解密mysql
    先获取mysql加密密码,在使用php解密程序解出密码,1.使用navicat导出连接,文件中有加密密码,2.在注册表中,计算机\HKEY_CURRENT_USER\SOFTWARE\PremiumSoft\Navicat\Servers\{连......