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 ...
随机推荐
- Summer training #8
A: B:按题意直接暴力找符合题意的数的个数 #include <bits/stdc++.h> #include <cstring> #include <iostream ...
- Django学习系列20:改进功能测试
隐示等待和显示等待 我们看看在功能测试中function_tests.py中的 time.sleep inputbox.send_keys(Keys.ENTER) time.sleep(1) self ...
- k8sStatefulSet控制器
一.StatefulSet概述 应用程序存在有状态和无状态两种类别,因为无状态类应用的pod资源可按需增加.减少或重构,而不会对由其提供的服务产生除了并发相应能力之外的其他严重影响.pod资源的常用控 ...
- 【备忘录】ORACLE数据库每日计划EXPDP备份
1.OracleBackup_expdp版本|oracle.bat文件 还需手动更改的内容如下: 调用格式需改成call %~dp0\OracleBackup 数据库 用户名 密码 文件夹名称 ...
- Mac修改显示器使支持原生缩放
教程 直接搬运没有意义,直接放链接.地址:https://bbs.feng.com/read-htm-tid-11677019.html 若无法访问请使用网页截图备份.地址:https://img20 ...
- 详解 @MapperScan 注解和 @Mapper 注解
实际上,这是一个非常简单的问题.我并没有一口回绝他,让他去百度.因为,新人都会经历这个过程.好不容易,问你一次,你直接让他百度,会打击到他的.而且,别人会觉得你摆架子. @Mapper 这个注解的定义 ...
- ElementUI datepicker日期选择器时间选择范围限制
ElementUI是饿了么推出的一套基于vue2.x的一个ui框架.官方文档也很详细,这里做一个element-ui日期插件的补充. 最近做项目用到了datepicker,需要对日期选择做一些限制, ...
- Gym 100971D 单调栈
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- 7.Java Web的数据库操作
一.环境配置(基于MySQL数据库) 1.下载MySQL数据库 2.下载安装 Navicat,破解方法去吾爱破解网站查询 第一次连接mysql时可能会出现错误,可能是因为二者对密码的编码方法不一致,可 ...
- HYSBZ - 3813 奇数国 欧拉函数+树状数组(线段树)
HYSBZ - 3813奇数国 中文题,巨苟题,巨无敌苟!!首先是关于不相冲数,也就是互质数的处理,欧拉函数是可以求出互质数,但是这里的product非常大,最小都2100000,这是不可能实现的.所 ...