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 ...
随机推荐
- Django学习系列21:为每一个清单添加唯一URL
现在让我们来解决我们真正的问题,即我们的设计只允许一个全局列表. 我将演示一个关键的TDD技术:如何使用一个渐进的.循序渐进的过程来适应现有的代码,这些过程将您从工作状态转移到工作状态.测试山羊,而不 ...
- 浅入深出Vue:文章列表
终于到我们小项目的最后一个功能了,那就是列表页展示! 新建组件 先来新建组件 List.vue: <template> <div></div> </templ ...
- IQ基础
I: in-phase 表示同相Q: quadrature 表示正交,与I 相位差90 度. 现在来解释IQ信号的来源: 最早通讯是模拟通讯,假设载波为cos(a),信号为cos(b),那么通过相 ...
- JAVA 流与文件
流 InputStream和OutputStream是所有的输入流和输出流的超类.他们两个都是抽象类. read方法和write方法都是阻塞方法,这意味着如果不能里可以写入或者读取,比如因为网络问题, ...
- 第十一章 前端开发-bootstrap
11.5.0 bootstrap 11.5.1 bootstrap的介绍和响应式 http://book.luffycity.com/python-book/95-bootstrap/951-boot ...
- JS 实现复制一个或多个内容到剪贴板
需要实现的功能:点击button,复制如下值到剪贴板, 链接:http://192.168.0.203:7083/share/nRrDLqBBJFjXQ5lk9Nv60GV6 提取码: 3NmH 常用 ...
- redis配置主从备份以及主备切换方案配置(转)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/gsying1474/article/de ...
- 关于.Net中Process和ProcessStartInfor的使用
本文主要是介绍在.Net中System.Diagnostics命名空间下Process类和ProcessStartInfo类的使用 用于启动一个外部程序所使用的类是Process,至于ProcessS ...
- Windows系统下载地址
地址: https://msdn.itellyou.cn/ 里面给出的是迅雷下载链接,请提前安装好迅雷
- JVM(二),Java怎样实现一次编译到处运行(平台无关性)
二.Java怎样实现一次编译到处运行(平台无关性) 1.java平台无关性原理 Java源码首先被编译成字节码,再由不同平台的JVM进行解析,JAVA语言在不同的平台上运行时不需要进行重新编译,Jav ...