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;
}

}

}
 

                                        

 

RSA 对称加密,对称解密----公钥私钥加密解密过程的更多相关文章

  1. RSA不对称加密,公钥加密私钥解密,私钥加密公钥解密

    RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一 ...

  2. WebAPi接口安全之公钥私钥加密

    WebAPi使用公钥私钥加密介绍和使用 随着各种设备的兴起,WebApi作为服务也越来越流行.而在无任何保护措施的情况下接口完全暴露在外面,将导致被恶意请求.最近项目的项目中由于提供给APP的接口未对 ...

  3. 使用公钥私钥加密实现单点登录(SSO)

    Oauth2+Gateway+springcloud+springcloud-alibaba-nacos+jwt ,使用公钥私钥加密实现单点登录(OSS) github地址点这里 注意事项 GET: ...

  4. NetCore 生成RSA公私钥对,公钥加密私钥解密,私钥加密公钥解密

    using Newtonsoft.Json; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Encodings; using ...

  5. RSA 加密算法 Java 公钥加密私钥解密 和 私钥加密公钥解密 的特点

    package com.smt.cipher.unsymmetry; import org.apache.commons.codec.binary.Base64; import org.apache. ...

  6. C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)

    但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...

  7. 对加密方式(公钥私钥)的形象理解(以http和https为例)

    https其实就是建构在SSL/TLS之上的 http协议,所以要比较https比http多用多少服务器资源,主要看SSL/TLS本身消耗多少服务器资源. http使用TCP 三次握手建立连接,客户端 ...

  8. 支付宝开放平台 配置RSA(SHA1)密钥 OpenSSL配置公钥私钥对

    进入到第一次配置支付宝支付服务了 配置支付宝服务,需要去支付宝的开放平台申请服务 需要设置一些参数 其中需要在后台设置配置RSA(SHA1)密钥(公钥(注意这个子读"yao")) ...

  9. RSA 加密 解密 公钥 私钥 签名 加签 验签

    http://blog.csdn.net/21aspnet/article/details/7249401# http://www.ruanyifeng.com/blog/2013/06/rsa_al ...

  10. 【Azure API 管理】APIM 配置Validate-JWT策略,验证RS256非对称(公钥/私钥)加密的Token

    问题描述 在APIM中配置对传入的Token进行预验证,确保传入后端被保护的API的Authorization信息正确有效,可以使用validate-jwt策略.validate-jwt 策略强制要求 ...

随机推荐

  1. XAMPP的mysql启动失败:Plugin ‘FEEDBACK‘ is disabled

    安装完XAMPP后启动mysql,发现启动失败也没有任何提示,通过查看mysql_error日志,描述: 2021-08-11 18:56:53 0 [Note] InnoDB: Mutexes an ...

  2. 如何获取安全获取苹果udid,imei

    [点击测试 https://authapi.applekuid.com](https://authapi.applekuid.com/) 目前国内有很多获取udid的方法,例如蒲公英 还有其他的网站都 ...

  3. CF301B Yaroslav and Time 题解

    CF301B 这不最短路的板子题吗? 思路 用 \(ak\) 代表走到第 \(k\) 点时的可恢复单位时间的值. \(i\) 到 \(j\) 的距离是 \(\left ( \left | xi-xj ...

  4. 机器学习:详解多任务学习(Multi-task learning)

    详解多任务学习 在迁移学习中,步骤是串行的,从任务\(A\)里学习只是然后迁移到任务\(B\).在多任务学习中,是同时开始学习的,试图让单个神经网络同时做几件事情,然后希望这里每个任务都能帮到其他所有 ...

  5. 【JavaScript】精度损失问题

    参卡博客: https://blog.csdn.net/azl397985856/article/details/99148969/

  6. 【BatchProgram】 读取文本批量创建目录

    NameList.txt文件内容 FILE-NAME-A FILE-NAME-B FILE-NAME-C ... 根据上面文件批量创建对应的目录,且附加序号 CMD代码: @ECHO OFF SETL ...

  7. wandb原来是可以网络直连的,国内可以无障碍使用

    一直不是很常使用神经网络训练可视化的工具,包括:tensorboard,等等,wandb平时也是直接就忽略,不过最近无意间看了看这个效果,感觉还是不错的,于是尝试了一下. 网上很多人说这个工具服务器在 ...

  8. ROS(机器人操作系统)的基本了解

    参考: https://blog.csdn.net/qq_51963216/article/details/125754175 https://zhuanlan.zhihu.com/p/5956062 ...

  9. UE Websocket 通信

    项目中遇到UE需要对接Websocket协议接收实时数据. 所以需要实现一个Websocket Client的功能. 由于UE引擎已经集成了Websocket库(基于libwebsocket),可以通 ...

  10. Java数组小白版

    一.数组概念 一.数组定义 数组就是指在计算机内存中开辟的连续存储空间,用于存放程序运行中需要用到的一组相同类型数据的容器. 二.数组的声明 +数组的长度 定义数组时需要确定数组的长度(元素的个数), ...