RSA分段加密解密

Wesley13
• 阅读 697

RSA分段加密解密 RSA分段加密解密

 1 private static readonly Encoding Encoder = Encoding.UTF8;
 2 
 3         public static String Encrypt(this String plaintext)
 4         {
 5             X509Certificate2 _X509Certificate2 = RSAHelper.RetrieveX509Certificate();
 6             string publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
 7             using (RSACryptoServiceProvider RSACryptography = /*_X509Certificate2.PublicKey.Key as*/ new RSACryptoServiceProvider())
 8             {
 9                 RSACryptography.FromXmlString(publickey);
10                 Byte[] PlaintextData = RSAHelper.Encoder.GetBytes(plaintext);
11                 int MaxBlockSize = RSACryptography.KeySize / 8 - 11;    //加密块最大长度限制
12 
13                 if (PlaintextData.Length <= MaxBlockSize)
14                     return Convert.ToBase64String(RSACryptography.Encrypt(PlaintextData, false));
15 
16                 using (MemoryStream PlaiStream = new MemoryStream(PlaintextData))
17                 using (MemoryStream CrypStream = new MemoryStream())
18                 {
19                     Byte[] Buffer = new Byte[MaxBlockSize];
20                     int BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);
21 
22                     while (BlockSize > 0)
23                     {
24                         Byte[] ToEncrypt = new Byte[BlockSize];
25                         Array.Copy(Buffer, 0, ToEncrypt, 0, BlockSize);
26 
27                         Byte[] Cryptograph = RSACryptography.Encrypt(ToEncrypt, false);
28                         CrypStream.Write(Cryptograph, 0, Cryptograph.Length);
29 
30                         BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);
31                     }
32 
33                     return Convert.ToBase64String(CrypStream.ToArray(), Base64FormattingOptions.None);
34                 }
35             }
36         }
37 
38         public static String Decrypt(this String ciphertext)
39         {
40             X509Certificate2 _X509Certificate2 = RSAHelper.RetrieveX509Certificate();
41             string privatekey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>";
42             using (RSACryptoServiceProvider RSACryptography = /*_X509Certificate2.PrivateKey as*/ new RSACryptoServiceProvider())
43             {
44                 RSACryptography.FromXmlString(privatekey);
45                 Byte[] CiphertextData = Convert.FromBase64String(ciphertext);
46                 int MaxBlockSize = RSACryptography.KeySize / 8;    //解密块最大长度限制
47 
48                 if (CiphertextData.Length <= MaxBlockSize)
49                     return RSAHelper.Encoder.GetString(RSACryptography.Decrypt(CiphertextData, false));
50 
51                 using (MemoryStream CrypStream = new MemoryStream(CiphertextData))
52                 using (MemoryStream PlaiStream = new MemoryStream())
53                 {
54                     Byte[] Buffer = new Byte[MaxBlockSize];
55                     int BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);
56 
57                     while (BlockSize > 0)
58                     {
59                         Byte[] ToDecrypt = new Byte[BlockSize];
60                         Array.Copy(Buffer, 0, ToDecrypt, 0, BlockSize);
61 
62                         Byte[] Plaintext = RSACryptography.Decrypt(ToDecrypt, false);
63                         PlaiStream.Write(Plaintext, 0, Plaintext.Length);
64 
65                         BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);
66                     }
67 
68                     return RSAHelper.Encoder.GetString(PlaiStream.ToArray());
69                 }
70             }
71         }
72 
73         private static X509Certificate2 RetrieveX509Certificate()
74         {
75 
76             return null;    //检索用于 RSA 加密的 X509Certificate2 证书
77         }

View Code

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
3年前
vue element table 表头添加图标
!(https://oscimg.oschina.net/oscnet/189d567e07e0844612470867b06a35c0aaf.jpg)!(https://oscimg.oschina.net/oscnet/dc3b454d663d8618924b78796345a145b3f.jpg)1<template
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
3年前
mysql系列(三)——mysql架构与存储引擎
一、MySql逻辑架构!(https://oscimg.oschina.net/oscnet/up35e1824330cd1df7ac66652b9b1dbc41965.png)1.连接层!(https://oscimg.oschina.net/oscnet/up272cb2634e262cc2df52220a063
Wesley13 Wesley13
3年前
Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
!(https://oscimg.oschina.net/oscnet/3e08a942dd884e9ab82b63a1f3c4aada.jpg"未命名文件.jpg")Java技术栈不可错过的Java 技术公众号!(https://oscimg.oschina.net/oscnet/00fcff52518e
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Js使用面向对象和面向过程的方法实现拖拽物体的效果
1.面向过程的拖拽实现代码:!(https://oscimg.oschina.net/oscnet/d680c759957babef2fec0902676eaa35ad9.gif)<!DOCTYPEhtml<html<head<titledragDiv</title
Wesley13 Wesley13
3年前
35岁是技术人的天花板吗?
35岁是技术人的天花板吗?我非常不认同“35岁现象”,人类没有那么脆弱,人类的智力不会说是35岁之后就停止发展,更不是说35岁之后就没有机会了。马云35岁还在教书,任正非35岁还在工厂上班。为什么技术人员到35岁就应该退役了呢?所以35岁根本就不是一个问题,我今年已经37岁了,我发现我才刚刚找到自己的节奏,刚刚上路。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这