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# 加密 解密的更多相关文章

  1. PHP的学习--RSA加密解密

    PHP服务端与客户端交互或者提供开放API时,通常需要对敏感的数据进行加密,这时候rsa非对称加密就能派上用处了. 举个通俗易懂的例子,假设我们再登录一个网站,发送账号和密码,请求被拦截了. 密码没加 ...

  2. 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输

    Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...

  3. .NET和JAVA中BYTE的区别以及JAVA中“DES/CBC/PKCS5PADDING” 加密解密在.NET中的实现

    场景:java 作为客户端调用已有的一个.net写的server的webservice,输入string,返回字节数组. 问题:返回的值不是自己想要的,跟.net客户端直接调用总是有差距 分析:平台不 ...

  4. php使用openssl进行Rsa长数据加密(117)解密(128) 和 DES 加密解密

    PHP使用openssl进行Rsa加密,如果要加密的明文太长则会出错,解决方法:加密的时候117个字符加密一次,然后把所有的密文拼接成一个密文:解密的时候需要128个字符解密一下,然后拼接成数据. 加 ...

  5. c#和js互通的AES加密解密

    一.使用场景 在使用前后端分离的框架中常常会进行传输数据相互加密解密以确保数据的安全性,如web Api返回加密数据客户端或web端进行解密,或者客户端或web端进行加密提交数据服务端解密数据等等. ...

  6. PHP AES的加密解密

    AES加密算法 密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DE ...

  7. [PHP]加密解密函数

    非常给力的authcode加密函数,Discuz!经典代码(带详解) function authcode($string, $operation = 'DECODE', $key = '', $exp ...

  8. 非对称技术栈实现AES加密解密

    非对称技术栈实现AES加密解密 正如前面的一篇文章所述,https协议的SSL层是实现在传输层之上,应用层之下,也就是说在应用层上看到的请求还是明码的,对于某些场景下要求这些http请求参数是非可读的 ...

  9. 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解

    原文地址:http://docode.top/Article/Detail/10003 目录: 1..Net(C#)平台下Des加密解密源代码 2..Net(C#)平台下Aes加密解密源代码 3..N ...

随机推荐

  1. hive单机部署

    hadoop,hbase,zookeeper安装好了,现在来安装hive hadoop 版本:2.8.4 hbase 版本:2.1.3 hive 版本:2.3.4 zookeeper 版本:3.4.1 ...

  2. ACID理解

    数据库事物的4个特性. A原子性:多次操作要么全部成功,要么全部失败.undo日志是在事务执行失败的时候撤销对数据库的操作,保证了事务的原子性(Atomicity) C一致性:一致性这个最不好理解.数 ...

  3. Linux/Ubantu 安装 idea

    wget 使用 wget url (这里的url就是你要下载idea的网站) 在idea官网中 找到 direct link 右键复制链接 在 linux 中 打开 终端命令窗口 (Ctrl +Alt ...

  4. MySQL数据库MyISAM存储引擎转为Innodb

    MySQL数据库MyISAM存储引擎转为Innodb  之前公司的数据库存储引擎全部为MyISAM,数据量和访问量都不是很大,所以一直都没什么问题.但是最近出现了MySQL数据表经常被锁的情况,直接导 ...

  5. Educational Codeforces Round 26 [ D. Round Subset ] [ E. Vasya's Function ] [ F. Prefix Sums ]

    PROBLEM D - Round Subset 题 OvO http://codeforces.com/contest/837/problem/D 837D 解 DP, dp[i][j]代表已经选择 ...

  6. jquery reset选择器 语法

    jquery reset选择器 语法 作用::reset 选择器选取类型为 reset 的 <button> 和 <input> 元素.直线电机滑台 语法:$(":r ...

  7. 《剑指offer》算法题第十一天

    今日题目: 滑动窗口的最大值 扑克牌中的顺子 圆圈中最后剩下的数字 求1+2+3+...+n 不用加减乘除做加法 构建乘积数组 今天的题目比较有意思,可以学到很多知识,包括第1题中的数据结构——双向队 ...

  8. 打开图像文件失败汇总:“Could not load image... ...0x## 0x##”错误

    造冰箱的熊猫@cnblogs 2018/12/15 在Ubuntu上使用Image Viewer打开图片文件时,有时会遇到“Could not load image '001.jpg'. Error ...

  9. Python 简易Cmd控制

    Cmd控制 昨天看到了别的组的部署方案,使用python来控制的,我们是用shell 今天尝试了一下 code import os import sys from cmd import Cmd cla ...

  10. #if/#else/#endif

    在linux环境下写c代码时会尝试各种方法或调整路径,需要用到#if #include<stdio.h> int main(){ int i; #if 0 i = ; #else i = ...