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. Python按条件筛选、剔除表格数据并绘制剔除前后的直方图

      本文介绍基于Python语言,读取Excel表格文件数据,以其中某一列数据的值为标准,对于这一列数据处于指定范围的所有行,再用其他几列数据的数值,加以数据筛选与剔除:同时,对筛选前.后的数据分别绘 ...

  2. OpenAI深夜丢炸弹硬杠谷歌搜索

    这几年科技变革太快,AI更是飞速发展,作为一名IT老兵,使用过的搜索引擎也是一换再换.这不,刚消停了一段时间的OpenAI又丢出一个炸弹SearchGPT,直接跟谷歌掀桌子了. 1.谷歌搜索的无奈 早 ...

  3. 【SpringCloud】Re03 Feign

    Feign是一个声明式的HttpClient?更简洁的实现Http请求发送 安装Feign组件: 配置Feign的依赖坐标: <?xml version="1.0" enco ...

  4. windows10操作系统QQ音乐开全局音效后频繁出现报错,鼠标卡顿,系统死机等问题——解决方法

    如题: windows10操作系统QQ音乐开全局音效后频繁出现报错,鼠标卡顿,系统死机等问题. QQ音乐,开启全局音效,提示需要重启: 重启电脑后发现出现频繁卡机,鼠标卡顿,甚至短暂的死机现象,查看控 ...

  5. 【转载】WSL 的基本命令

    参考: https://learn.microsoft.com/zh-cn/windows/wsl/basic-commands https://blog.csdn.net/u010099177/ar ...

  6. Inno Setup 寻找 AppId 的方法

    背景 有时候打包后,会遗失AppId.这样会导致下一次打包时没办法和之前统一.为了避免这个问题,所以最好是打包时记下来,可以根据注册表去查 解决办法 可以根据任意查找注册表的工具,我这里使用 Regi ...

  7. 9组-Alpha冲刺-5/6

    一.基本情况 队名:不行就摆了吧 组长博客: https://www.cnblogs.com/Microsoft-hc/p/15546711.html 小组人数: 8 二.冲刺概况汇报 谢小龙 过去两 ...

  8. MFC制作带界面的DLL库

    ## MFC如何创建一个带界面的DLL(动态链接库) 1.创建项目 打开VS,文件->新建->项目: 点击确定之后弹出来的界面,点击下一步->选择"使用共享MFC DLL的 ...

  9. Synology NAS GitLab 配置

    安装 安装的时候会提示服务器名.root用户名等,这步服务器名千万不要写错,不然会登不上去,提示 502. root 密码 网上有很多说 root 密码怎么获取的,但是都不适用. 实际上是第一个访问 ...

  10. 网卡DM9000裸机驱动详解

    一.网卡 1. 概念 网卡是一块被设计用来允许计算机在计算机网络上进行通讯的计算机硬件.由于其拥有MAC地址,因此属于OSI模型的第2层.它使得用户可以通过电缆或无线相互连接. 每一个网卡都有一个被称 ...