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. 第二章 Vue快速入门--13 讲解v-model实现表单元素的数据双向绑定

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  2. SqlServer获取当前日期

    1. 获取当前日期 select GETDATE() 格式化: ) ---- :: 2. 获取当前年  --2017 3.获取当前月 --05或5 4.获取当前日期 --07或7 select DAY ...

  3. spring boot 导出 jar 配置文件的问题

    网上有很多关联映射及讲解,想要说的是   主要就是 classpath  加上的话   jar就可以找到了

  4. 第十一章 前端开发-bootstrap

    11.5.0 bootstrap 11.5.1 bootstrap的介绍和响应式 http://book.luffycity.com/python-book/95-bootstrap/951-boot ...

  5. [TypeScript] @OnChange for ngOnChanges

    Take away from NGCONF talk. It is a good show case to how to use decorator. export interface SimpleC ...

  6. Python之threading模块的使用

    作用:同一个进程空间并发运行多个操作,专业术语简称为:[多线程] 1.任务函数不带参数多线程 #!/usr/bin/env python # -*- coding: utf-8 -*- import ...

  7. 微信小程序_(组件)可拖动movable-view

    微信小程序movable-view组件官方文档 传送门 Learn 一.moveable-view组件 一.movable-view组件 direction:movable-view的移动方向,属性值 ...

  8. 51nod 1165 整边直角三角形的数量(两种解法)

    链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1165 直角三角形,三条边的长度都是整数.给出周长N,求符合条件的三角形数量. ...

  9. Http请求优化

    Http请求优化 我们在做项目开发或多或少的都会使用SpringCloud,其中做远程调度的时候会将HTTP请求Http请求优化. HTTP请求Client存在很多种. JDK原生的URLConnec ...

  10. 优雅的退出asyncio事件循环

    import asyncio import functools import os import signal """ 信号值 符号 行为 2 SIGINT 进程终端,C ...