EncryptionAndDecryptionC# 加密 解密
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console; namespace RSA_Demo
{
class Program
{
static void Main(string[] args)
{
//生成公钥私钥
RSAKey rsaKey = RsaUtil.GetRSAKey();
WriteLine($"PrivateKey:{rsaKey.PrivateKey}");
WriteLine($"PublicKey:{rsaKey.PublicKey}");
ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace RSA_Demo
{
public static class RsaUtil
{
//https://www.cnblogs.com/revealit/p/6094750.html
const int DWKEYSIZE = ; public static RSAKey GetRSAKey()
{
RSACryptoServiceProvider.UseMachineKeyStore = true;
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(DWKEYSIZE);
RSAParameters paras = rsaProvider.ExportParameters(true); return new RSAKey()
{
PublicKey=ComponentKey(paras.Exponent,paras.Modulus),
PrivateKey=ComponentKey(paras.D,paras.Modulus)
};
} private static string ComponentKey(byte[] b1, byte[] b2)
{
List<byte> list = new List<byte>();
list.Add((byte)b1.Length);
list.AddRange(b1);
list.AddRange(b2);
byte[] b = list.ToArray<byte>(); return Convert.ToBase64String(b);
} private static void ResolveKey(string key,out byte[] b1,out byte[] b2)
{
byte[] b = Convert.FromBase64String(key); int b1Length = b[];
b1 = new byte[b1Length];
b2 = new byte[b.Length- b1Length - ]; for (int n=,i = ,j=; n < b.Length; n++)
{
if (n<= b1Length)
{
b1[i++] = b[n];
}
else
{
b2[j++] = b[n];
}
}
} public static string EncryptionString(string source, string key)
{
string encryptString = string.Empty;
byte[] d;
byte[] n; try
{
if (!CheckSourceValidate(source))
{
throw new Exception("source string too long");
/*为何还有限制
* https://blog.csdn.net/taoxin52/article/details/53782470
*如果source过长可以将source分段加密 追加到StringBuilder中
*source差分的时候,建议已35个字符为一组
* RSA 一次加密的byte数量是有限制的
* 一般中文转换成3个或者4个byte
* 如果某个中文转换成3个byte 前两个byte 与后一个byte被差分到
* 两个段里加密,解密的时候就会出现乱码
* 另外在两个加密段之间添加特殊符合@解密的时候先用@差分
* 分段解密,在拼接成解密后的字符串
*/
} //解析这个密钥
ResolveKey(key, out d, out n);
BigInteger biN = new BigInteger(n);
BigInteger biD = new BigInteger(d);
encryptString = EncryptionString(source,biD,biN);
}
catch (Exception)
{
encryptString = source;
} return encryptString;
} private static string EncryptionString(string source, BigInteger d, BigInteger n)
{
int len = source.Length;
int len1 = ;
int blockLen = ; if ((len%)==)
{
len1 = len / ;
}
else
{
len1 = len / +;
} string block = "";
StringBuilder result = new StringBuilder();
for (int i = ; i < len1; i++)
{
if (len>=)
{
blockLen = ;
}
else
{
blockLen = len;
} block = source.Substring(i * , blockLen); byte[] oText = System.Text.Encoding.UTF8.GetBytes(block);
BigInteger biText = new BigInteger(oText);
//BigInteger biEnText=biText.modPow() } return result.ToString().TrimEnd('@');
} /// <summary>
/// 检查明文的有效性 DWKEYSIZE/8-11 长度之内为有效 中英文都算一个字符
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
private static bool CheckSourceValidate(string source)
{
return (DWKEYSIZE / - ) >= source.Length;
}
} public struct RSAKey
{
public string PublicKey { get; set; }
public string PrivateKey { get; set; }
}
}
EncryptionAndDecryptionC# 加密 解密的更多相关文章
- PHP的学习--RSA加密解密
PHP服务端与客户端交互或者提供开放API时,通常需要对敏感的数据进行加密,这时候rsa非对称加密就能派上用处了. 举个通俗易懂的例子,假设我们再登录一个网站,发送账号和密码,请求被拦截了. 密码没加 ...
- 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输
Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...
- .NET和JAVA中BYTE的区别以及JAVA中“DES/CBC/PKCS5PADDING” 加密解密在.NET中的实现
场景:java 作为客户端调用已有的一个.net写的server的webservice,输入string,返回字节数组. 问题:返回的值不是自己想要的,跟.net客户端直接调用总是有差距 分析:平台不 ...
- php使用openssl进行Rsa长数据加密(117)解密(128) 和 DES 加密解密
PHP使用openssl进行Rsa加密,如果要加密的明文太长则会出错,解决方法:加密的时候117个字符加密一次,然后把所有的密文拼接成一个密文:解密的时候需要128个字符解密一下,然后拼接成数据. 加 ...
- c#和js互通的AES加密解密
一.使用场景 在使用前后端分离的框架中常常会进行传输数据相互加密解密以确保数据的安全性,如web Api返回加密数据客户端或web端进行解密,或者客户端或web端进行加密提交数据服务端解密数据等等. ...
- PHP AES的加密解密
AES加密算法 密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DE ...
- [PHP]加密解密函数
非常给力的authcode加密函数,Discuz!经典代码(带详解) function authcode($string, $operation = 'DECODE', $key = '', $exp ...
- 非对称技术栈实现AES加密解密
非对称技术栈实现AES加密解密 正如前面的一篇文章所述,https协议的SSL层是实现在传输层之上,应用层之下,也就是说在应用层上看到的请求还是明码的,对于某些场景下要求这些http请求参数是非可读的 ...
- 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解
原文地址:http://docode.top/Article/Detail/10003 目录: 1..Net(C#)平台下Des加密解密源代码 2..Net(C#)平台下Aes加密解密源代码 3..N ...
随机推荐
- spring RestTemplate 出现 NoHttpResponseException 和 ConnectionTimeoutException
使用 httpclient.4.5.6 springboot 2.0.8RELEASE RetryExec.java CloseableHttpResponse execute() try { ret ...
- Tomcat启动中文乱码,淇℃伅等
打开Tomcat根目录下的conf文件夹,打开logging.properties. 把 java.util.logging.ConsoleHandler.encoding = UTF-8 换成 ja ...
- Java的日期与时间 java.time.Duration (转)
一个Duration对象表示两个Instant间的一段时间,是在Java 8中加入的新功能. 一个Duration实例是不可变的,当创建出对象后就不能改变它的值了.你只能通过Duration的计算方法 ...
- 题解 小B的询问
题面 解析 这就是道莫队模板啊啊!! 因此,似乎并没有什么好讲的. 莫队算法传送门 我们只需要将询问存下来, 离线处理就行了. 还是上代码吧: #include<bits/stdc++.h> ...
- flask框架(十一): 蓝图
蓝图用于为应用提供目录划分: 一:上目录结构 二:上代码 <!DOCTYPE html> <html lang="en"> <head> < ...
- delphi请求http接口中文乱码问题
请求http接口的时候参数值是中文乱码: http接口一般都是由java,php以及C#开发而成的,乱码的原因也是由于编码的问题,一般传递数据的都是utf8,然后传递的时候都会urlEcode 那么d ...
- BZOJ 2734 [HNOI2012]集合选数 (状压DP、时间复杂度分析)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2734 题解 嗯早就想写的题,昨天因为某些不可告人的原因(大雾)把这题写了,今天再来写题解 ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
题目描述 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由 ...
- Linux 常用命令之df du
1.du 命令:显示每个文件或目录的磁盘使用空间 1) du -h --max-depth [root@ip101 app]# pwd /opt/app [root@ip101 app]# du -h ...
- python3.*之列表常用操作
首先定义一个列表:names= ["xiaoming","xiaogang","xiaomei","xiaohong"] ...