<经验杂谈>C#对CA证书加密解密的简单介绍
最近做项目接触了一些关于用CA证书加密解密的知识,现在分享一下,加密主要分为对称加密和非对称加密以及单项加密这三种,CA是一个权威的第三方认证机构,CA加密有公钥和私钥之分。
以下是C#读取证书文件进行加密解密的Code,供各位参考
CA 加密:
public static string CAEncryption(string xml)
{
X509Certificate2 pubcrt =new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + BaseConfig.CaPubkey);
return Core.CaUtilHelper.Encrypt(xml, pubcrt);
} public static String Encrypt(String plaintext, X509Certificate2 pubcrt)
{
X509Certificate2 _X509Certificate2 = pubcrt;
using (RSACryptoServiceProvider RSACryptography = _X509Certificate2.PublicKey.Key as RSACryptoServiceProvider)
{
Byte[] PlaintextData = Encoding.UTF8.GetBytes(plaintext);
int MaxBlockSize = RSACryptography.KeySize / - ; //加密块最大长度限制
if (PlaintextData.Length <= MaxBlockSize)
return Convert.ToBase64String(RSACryptography.Encrypt(PlaintextData, false));
using (MemoryStream PlaiStream = new MemoryStream(PlaintextData))
using (MemoryStream CrypStream = new MemoryStream())
{
Byte[] Buffer = new Byte[MaxBlockSize];
int BlockSize = PlaiStream.Read(Buffer, , MaxBlockSize);
while (BlockSize > )
{
Byte[] ToEncrypt = new Byte[BlockSize];
Array.Copy(Buffer, , ToEncrypt, , BlockSize);
Byte[] Cryptograph = RSACryptography.Encrypt(ToEncrypt, false);
CrypStream.Write(Cryptograph, , Cryptograph.Length);
BlockSize = PlaiStream.Read(Buffer, , MaxBlockSize);
}
return Convert.ToBase64String(CrypStream.ToArray(), Base64FormattingOptions.None);
}
}
}
CA 解密:
public static string CADecrypt(string content)
{
X509Certificate2 prvcrt = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + BaseConfig.CaPrvkey,BaseConfig.CaPwd, X509KeyStorageFlags.Exportable);
return Core.CaUtilHelper.Decrypt(content, prvcrt);
} public static String Decrypt(String ciphertext, X509Certificate2 prvpfx)
{
X509Certificate2 _X509Certificate2 = prvpfx;
using (RSACryptoServiceProvider RSACryptography = _X509Certificate2.PrivateKey as RSACryptoServiceProvider)
{
Byte[] CiphertextData = Convert.FromBase64String(ciphertext);
int MaxBlockSize = RSACryptography.KeySize / ; //解密块最大长度限制
if (CiphertextData.Length <= MaxBlockSize)
return Encoding.UTF8.GetString(RSACryptography.Decrypt(CiphertextData, false));
using (MemoryStream CrypStream = new MemoryStream(CiphertextData))
using (MemoryStream PlaiStream = new MemoryStream())
{
Byte[] Buffer = new Byte[MaxBlockSize];
int BlockSize = CrypStream.Read(Buffer, , MaxBlockSize);
while (BlockSize > )
{
Byte[] ToDecrypt = new Byte[BlockSize];
Array.Copy(Buffer, , ToDecrypt, , BlockSize);
Byte[] Plaintext = RSACryptography.Decrypt(ToDecrypt, false);
PlaiStream.Write(Plaintext, , Plaintext.Length);
BlockSize = CrypStream.Read(Buffer, , MaxBlockSize);
}
return Encoding.UTF8.GetString(PlaiStream.ToArray());
}
}
}
<经验杂谈>C#对CA证书加密解密的简单介绍的更多相关文章
- <经验杂谈>C#使用AES加密解密的简单介绍
AES 算法是基于置换和代替的.置换是数据的重新排列,而代替是用一个单元数据替换另一个.AES 使用了几种不同的技术来实现置换和替换. 以下是我自己用c#研究出来算法Code: /// <sum ...
- 使用X.509数字证书加密解密实务(二)-- 使用RSA证书加密敏感数据
一. 使用RSA证书加.解密敏感数据 X.509证书标准支持三种不对称加密算法:RSA, DSA, Diffie-Hellman algorithms.最常用的是RSA算法.所以本文就以前面章节使用 ...
- 使用X.509数字证书加密解密实务(三)-- 使用RSA证书结合对称加密技术加密长数据
一. 使用证书结合对称加密算法加.解密长数据 上一章节讨论了如何使用RSA证书加密数据,文中提到:“Dotnet的RSA实现有个特点,它必须要在明文中添加一些随机数,所以明文不能把128字节占满,实 ...
- CA数字加密解密Demo
package aisin.text; import com.google.common.collect.Maps; import sun.misc.BASE64Decoder; impor ...
- 使用X.509数字证书加密解密实务(一)-- 证书的获得和管理
http://www.cnblogs.com/chnking/archive/2007/08/18/860983.html
- DESCryptoServiceProvider加密解密的简单使用例子
DES.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- 提供openssl -aes-256-cbc兼容加密/解密的简单python函数
原文链接:http://joelinoff.com/blog/?p=885 这里的示例显示了如何使用python以与openssl aes-256-cbc完全兼容的方式加密和解密数据.它是基于我在本网 ...
- RSA加密解密及RSA签名和验证及证书
RSA加密解密及RSA签名和验证及证书 公钥是给别人的 发送密文使用公钥加密 验证签名使用公钥验证 私钥是自己保留的 接受密文使用私钥解密 发送签名使用私钥签名 上述过程逆转是不行的,比如使用私钥加密 ...
- ca 证书、签名
1.我现在没有个人CA证书,使用.中信建投网上交易,是如何保障安全的呢? 如果您目前没有个人CA证书,使用.中信建投网上交易,系统其实也是用CA证书的RSA体系进行加密的. 您在输入账户和密码进行登录 ...
随机推荐
- Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.commons.EmptyVisitor
1.错误描述 2014-7-13 1:45:53 org.apache.struts2.spring.StrutsSpringObjectFactory info 信息: ... initialize ...
- 把连续动态bmp转换为avi
把动态bmp24转换为avi BYTE tmp_buf[1024*768*4]; //生成avi void BMPtoAVI(CString szAVIName, CString strBmpDir) ...
- 优先队列运用 TOJ 4123 Job Scheduling
链接:http://acm.tju.edu.cn/toj/showp4123.html 4123. Job Scheduling Time Limit: 1.0 Seconds Memory ...
- springtest-junit-jidi--测试接口
一,问题分析 假如在一个项目中要测试一个接口,但是这个项目没有对外开放url地址,话句话就是说浏览器访问不了里面的资源,自己造的数据不能测试接口,那么只能等别人来调用自己的接口,而且自己也不能测试,假 ...
- npm包管理器小节一下
淘宝npm镜像cnpm设置 npm install -g cnpm --registry=https://registry.npm.taobao.org 更新npm的版本 npm install np ...
- order by group by
order by 后 group by连用, mysql好像 >5.4不起作用 通过 explain 查看执行计划,可以看到没有 limit 的时候,少了一个 DERIVED 操作 估计是内部优 ...
- Java汉字乱码问题
window->preferences->输入框输入"encod" 将text file encoding 从default改成other utf-8 同理,css,H ...
- 【Luogu3398】仓鼠找sugar(树链剖分)
[Luogu3398]仓鼠找sugar(树链剖分) 题面 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他 ...
- tomcat环境配置 Linux 与 Windows
Windows:例如tomcat安装在 E:\tomcat 需要在环境变量配置:CATALINA_HOME=E:\tomcat CATALINA_BASE= E:\tomcat Linux:例如tom ...
- C#服务器端生成报告文档:使用帆软报表生成Word、Pdf报告
一.帆软报表简介 报表工具中,帆软报表相比Crystal Report(水晶报表).SQL Server Report Service(SSRS)等报表工具来说算是佼佼者,此外帆软报表在统计图表.数据 ...