RSA加密与解密
/// <summary>
/// RSA 公钥加密
/// </summary>
/// <param name="content">要加密的内容</param>
/// <param name="publickey">公钥</param>
/// <returns></returns>
public static string EncryptByPublicKey(string content, string publickey)
{
byte[] s = Convert.FromBase64String(publickey);
AsymmetricKeyParameter publicKey = PublicKeyFactory.CreateKey(s);
IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
//公钥加密
c.Init(true, publicKey);
byte[] byteData = Encoding.UTF8.GetBytes(content);
byteData = c.DoFinal(byteData, 0, byteData.Length);
return Convert.ToBase64String(byteData);
}
/// <summary>
/// RSA加密
/// </summary>
/// <param name="content">加密明文</param>
/// <param name="privatekey">私钥</param>
/// <returns>返回密文</returns>
public static string RSAEncry(string content, string privatekey)
{
AsymmetricKeyParameter privateKey = PrivateKeyFactory.CreateKey(Convert.FromBase64String(privatekey));
IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
//私钥加密
c.Init(true, privateKey);
byte[] byteData = Encoding.UTF8.GetBytes(content);
byteData = c.DoFinal(byteData, 0, byteData.Length);
return Convert.ToBase64String(byteData);
}
/// <summary>
/// RSA解密
/// </summary>
/// <param name="content">密文</param>
/// <param name="privatekey">私钥</param>
/// <returns>明文</returns>
public static string RSADeEncry(string content, string privatekey)
{
try
{
MemoryStream bufferStream = new MemoryStream();
byte[] bytData = Convert.FromBase64String(content);
int inputLength = bytData.Length;
AsymmetricKeyParameter privateKey = PrivateKeyFactory.CreateKey(Convert.FromBase64String(privatekey));
IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
cipher.Init(false, privateKey);
int offSet = 0;
byte[] cache;
int i = 0;
while (inputLength - offSet > 0)
{
if (inputLength - offSet > 128)
{
cache = cipher.DoFinal(bytData, offSet, 128);
}
else
{
cache = cipher.DoFinal(bytData, offSet, inputLength - offSet);
}
bufferStream.Write(cache, 0, cache.Length);
i++;
offSet = i * 128;
}
byte[] decryptedData = bufferStream.ToArray();
bufferStream.Close();
return Encoding.UTF8.GetString(bufferStream.ToArray());
}
catch (Exception e)
{
return e.Message;
}
}
RSA加密与解密的更多相关文章
- 通过ios实现RSA加密和解密
在加密和解密中,我们需要了解的知识有什么事openssl:RSA加密算法的基本原理:如何通过openssl生成最后我们需要的der和p12文件. 废话不多说,直接写步骤: 第一步:openssl来生成 ...
- C#实现RSA加密和解密详解
原文:C#实现RSA加密和解密详解 RSA加密解密源码: Code highlighting produced by Actipro CodeHighlighter (freeware) http:/ ...
- ASP.NET Core RSA加密或解密
前言 这两天主要是公司同事用到了RSA加密,事后也看了下,以为很简单,最终利用RSACryptoServiceProvider来实现RSA加密,然后大致了解到RSACryptoServiceProvi ...
- C#实现RSA加密与解密、签名与认证(转)
一.RSA简介 RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力 ...
- RSA加密和解密工具类
import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import java.security.*; i ...
- IOS, Android, Java Web Rest : RSA 加密和解密问题
IOS, Android, Java Web Rest : RSA 加密和解密问题 一对公钥私钥可以使用 OpenSSL创建, 通常 1024位长度够了. 注意: 1. 公钥私钥是BASE64编码的 ...
- C#实现RSA加密与解密、签名与认证
一.RSA简介 RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力 ...
- C# -- RSA加密与解密
1. RSA加密与解密 -- 使用公钥加密.私钥解密 public class RSATool { public string Encrypt(string strText, string st ...
- python RSA加密、解密、签名
python RSA加密.解密.签名 python中用于RSA加解密的库有好久个,本文主要讲解rsa.M2Crypto.Crypto这三个库对于RSA加密.解密.签名.验签的知识点. 知识基础 加密是 ...
- RSA加密、解密实现原理
RSA加密.解密实现原理 1.公钥.私钥
随机推荐
- 《Spring Cloud微服务 入门 实战与进阶》
很少在周末发文,还是由于昨晚刚收到实体书,还是耐不住性子马上发文了. 一年前,耗时半年多的时间,写出了我的第一本书<Spring Cloud微服务-全栈技术与案例解析>. 时至今日,一年的 ...
- centos安装nodejs并配置生产环境,基于pm2
安装nodejs和yarn的命令: curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum. ...
- 《细说PHP》第四版 样章 第18章 数据库抽象层PDO 4
18.4 创建PDO对象 使用PDO在与不同数据库管理系统之间交互时,PDO对象中的成员的方法是统一各种数据库的访问接口,所以在使用PDO与数据库交互之前,首先要创建一个PDO对象.在通过构造方法创 ...
- 使用csv模块读写csv格式文件
import csv class HandleCsv: ''' csv文件处理类 ''' def __init__(self, filename): ''' 构造器 :param filename: ...
- PHP】获取客户端(浏览器)信息、获取客户端系统信息、获取服务器信息
* 获取客户端浏览器信息 * @param null * @author https://blog.jjonline.cn/phptech/168.html * @return string */ f ...
- 转 SSD论文解读
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/u010167269/article/det ...
- 123: The filename, directory name, or volume label syntax is incorrect今天玩nginx的时候报错
今天在win下玩nginx的时候 提示500错误 看了下nginx的logs 提示 123: The filename, directory name, or volume label syntax ...
- 动软生成Model(dapper.common)
<#@ template language="c#" HostSpecific="True" #><#@ output extension= ...
- C#委托内部使用局部的变量的问题
一. 引子 先来看如下代码: ; Action action1 = () => { Console.WriteLine("打印一下i的值:" + i); }; i = ; A ...
- 异步和多线程Thread
刚接触线程的时候,感觉这个东西好神奇.虽然不是很明白,就感觉它很牛逼. 参考了一些大佬写的文章: https://www.cnblogs.com/yilezhu/p/10555849.html这个大佬 ...