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. wangEditor增加源码模式,添加查看源码功能

    wangEditor是一款轻量级的富文本编辑器.使用还比较方便,但是缺少查看源码模式,需要我们自定义一个menu给增加查看源码模式 下面是wangEditor增加源码模式的代码: <!DOCTY ...

  2. 胜未来:国内大模型+Agent应用案例精选,以及主流Agent框架开源项目推荐

    智胜未来:国内大模型+Agent应用案例精选,以及主流Agent框架开源项目推荐 Agent是以大模型为核心的智能体,通过与用户对话的形式,来完成各种任务,它很像一个"人".如果和 ...

  3. 对比python学julia(第一章)--(第四节)冰雹猜想

    4.1 依葫芦画瓢 冰雹猜想是一种非常有趣的数字黑洞,曾让无数的数学爱好者为之痴迷.它有一个非常简单的变换规则,具体来说就是:任意取一个正整数n,如果n是偶数,就把n变成n/2;如果n是奇数,就把n变 ...

  4. MIT6.1810の学习笔记

    webliuのmit.6.828学习笔记 写在前面 本文基于mit/6.828课程,附官方网址. 本文采用的实验环境为2020年版的xv6系统,需要wsl,vscode,docker工具.附环境配置教 ...

  5. ( Ubuntu环境 ) Vim下一键运行python代码

    使用一个统一的顺手的编辑&编译环境对于写代码是一件事半功倍的事情, 说白了就是有个顺手的写代码的工具很重要,这里要说的工具就是Vim. 假设   Ubuntu 系统中已经安装   Vim  : ...

  6. 【转载】 校正Ubuntu时间为北京时间

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_37421762/article/de ...

  7. mpi4py和cupy的联合应用(anaconda环境):GPU-aware MPI + Python GPU arrays

    Demo代码: from mpi4py import MPI import cupy as cp comm = MPI.COMM_WORLD size = comm.Get_size() rank = ...

  8. C# 使用特性的方式封装报文

    在编写上位机软件时,需要经常处理命令拼接与其他设备进行通信,通常对不同的命令封装成不同的方法,扩展稍许麻烦. 本次拟以特性方式实现,以兼顾维护性与扩展性. 思想: 一种命令对应一个类,其类中的各个属性 ...

  9. Telegram第三方登录步骤

    网上看到的不多,简单做个记录,本文主要对下面的文章做补充 Telegram(电报)授权登录(web)第三方 整体步骤为: 找 botFather 创建机器人 给机器人设置域名白名单,用于设置回调地址, ...

  10. QTreeWidget折叠子项(折叠当前项的所有子项)

    QTreeWidget折叠子项(折叠当前项的所有子项) 本文仅供本人知识总结使用,所以内容会比较浅显,不喜勿喷. 文章目录 QTreeWidget折叠子项(折叠当前项的所有子项) 一.仅折叠子项 二. ...