MSMQ消息加密
证书实现非对称加密/解密的代码如下
//非对称加密密钥
static byte[] RSAEncrypt(byte[] enkey, X509Certificate2 Certificate)
{
RSACryptoServiceProvider RSA = Certificate.PublicKey.Key as RSACryptoServiceProvider;
return RSA.Encrypt(enkey, false);
}
//非对成解密密钥
static byte[] RSADecrypt(byte[] context, X509Certificate2 Certificate)
{
RSACryptoServiceProvider RSA = Certificate.PrivateKey as RSACryptoServiceProvider;
return RSA.Decrypt(context, false);
}
使用证书的公钥加密,使用证书的私钥解密
对称加密算法进行消息的加密和解密,代码如下:
//对称加密消息内容
static byte[] Encrypt(SymmetricAlgorithm RC2, string bodystring)
{
MemoryStream ms = new MemoryStream();
CryptoStream encStream = new CryptoStream(ms, RC2.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(encStream);
sw.WriteLine(bodystring);
sw.Close();
encStream.Close();
byte[] buffer = ms.ToArray();
ms.Close();
return buffer;
}
//对称解密消息内容
static string Decrypt(byte[] CypherText, SymmetricAlgorithm RC2)
{
MemoryStream ms = new MemoryStream(CypherText);
CryptoStream encStream = new CryptoStream(ms, RC2.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(encStream);
string val = sr.ReadLine();
sr.Close();
encStream.Close();
ms.Close();
return val;
}
发送加密消息
static void Send()
{
MessageQueue mq = new MessageQueue(DestinationQueue);
//mq.EncryptionRequired = EncryptionRequired.Body;
//mq.FormatName = new BinaryMessageFormatter();
Message message = new Message();
//采用二进制序列化
message.Formatter = new BinaryMessageFormatter();// new XmlMessageFormatter(new Type[] { typeof(string) });
//获取x509证书
X509Certificate2 certificate = GetCertificate();
//使用x509证书非对称加密对称加密密钥
RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider();
byte[] key=RSAEncrypt(RC2.Key, certificate);
byte[] iv = RSAEncrypt(RC2.IV, certificate);
byte[] extarry= new byte[256];
key.CopyTo(extarry, 0);
iv.CopyTo(extarry, 128);
//保存使用非对称加密后的对称加密密钥
message.Extension = extarry;
//message.DestinationSymmetricKey = RSAEncrypt(RC2.Key, certificate);
//设定使用非对称加密的证书
//message.DigitalSignature = certificate.RawData;
message.SenderCertificate = certificate.RawData;
message.UseEncryption = false;
//message.AcknowledgeType = AcknowledgeTypes.PositiveReceive | AcknowledgeTypes.PositiveArrival;
//message.AdministrationQueue = new MessageQueue(@"thinkpad-t400\private$\myAdministrationQueue");
//message.UseJournalQueue = true;
message.UseDeadLetterQueue = true;
//设定对消息体对称加密算法
message.EncryptionAlgorithm = EncryptionAlgorithm.Rc2;
//message.ConnectorType = new Guid("1E9A03C5-A9B5-4BF6-B0CB-CCB313275285");
message.Label = Guid.NewGuid().ToString();
//生成同步加密key
//MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//SHA256CryptoServiceProvider hsa = new SHA256CryptoServiceProvider();
//byte[] keyArray = hsa.ComputeHash(System.Text.Encoding.ASCII.GetBytes(DestinationSymmetricKey));
//message.HashAlgorithm = System.Messaging.HashAlgorithm.Sha;
// RC2.Key = keyArray;
//使用RC2算法进行加密
byte[] enarry = Encrypt(RC2, BodyString);
string base64 = Convert.ToBase64String(enarry);
message.Body = enarry;
//message.SecurityContext = new SecurityContext();
Console.WriteLine("send encrypt message \r\n" + BodyString);
mq.Send(message, MessageQueueTransactionType.Single);
}
接收加密的消息
static void Receive()
{
MessageQueue mq = new MessageQueue(DestinationQueue);
//设定读取消息中证书,扩展属性中加密过的解密密钥
mq.MessageReadPropertyFilter.DestinationSymmetricKey = true;
mq.MessageReadPropertyFilter.Extension = true;
mq.MessageReadPropertyFilter.SenderCertificate = true;
mq.MessageReadPropertyFilter.DigitalSignature = true;
Message message=mq.Receive(MessageQueueTransactionType.Single);
message.Formatter = new BinaryMessageFormatter();
//获取证书
byte[] cert = message.SenderCertificate;
X509Certificate2 x509 = new X509Certificate2(cert);
x509 = GetCertificateBySubject(x509.Subject);
Console.WriteLine(x509.Thumbprint.ToString());
byte[] key = new byte[128];
byte[] iv = new byte[128];
for(int i=0;i<message.Extension.Length;i++)
{
if(i<128)
key[i] = message.Extension[i];
else
iv[i - 128] = message.Extension[i];
}
//还原对称加密密钥
key = RSADecrypt(key, x509);
iv = RSADecrypt(iv, x509);
//解密消息
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
rc2.Key = key;
rc2.IV = iv;
byte[] body = message.Body as byte[];
string bodystring= Decrypt(body, rc2);
Console.WriteLine("receive message " + bodystring);
}
证书实现非对称加密/解密的代码如下
//非对称加密密钥
static byte[] RSAEncrypt(byte[] enkey, X509Certificate2 Certificate)
{
RSACryptoServiceProvider RSA = Certificate.PublicKey.Key as RSACryptoServiceProvider;
return RSA.Encrypt(enkey, false);
}
//非对成解密密钥
static byte[] RSADecrypt(byte[] context, X509Certificate2 Certificate)
{
RSACryptoServiceProvider RSA = Certificate.PrivateKey as RSACryptoServiceProvider;
return RSA.Decrypt(context, false);
}
使用证书的公钥加密,使用证书的私钥解密
MSMQ消息加密的更多相关文章
- 【转】MSMQ消息队列安装
一.Windows 7安装.管理消息队列1.安装消息队列 执行用户必须要有本地 Administrators 组中的成员身份,或等效身份. 具体步骤: 开始—>控制面板—>程 ...
- MSMQ消息队列安装
一.Windows 7安装.管理消息队列1.安装消息队列 执行用户必须要有本地 Administrators 组中的成员身份,或等效身份. 具体步骤: 开始—>控制面板—>程 ...
- 即时通信系统中如何实现:聊天消息加密,让通信更安全? 【低调赠送:QQ高仿版GG 4.5 最新源码】
加密重要的通信消息,是一个常见的需求.在一些政府部门的即时通信软件中(如税务系统),对聊天消息进行加密是非常重要的一个功能,因为谈话中可能会涉及到机密的数据.我在最新的GG 4.5中,增加了对聊天消息 ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(十五):消息加密
前不久,微信的企业号使用了强制的消息加密方式,随后公众号也加入了可选的消息加密选项.目前企业号和公众号的加密方式是一致的(格式会有少许差别). 加密设置 进入公众号后台的“开发者中心”,我们可以看到U ...
- 微软MSMQ消息队列的使用
首先在windows系统中安装MSMQ 一.MSMQ交互 开发基于消息的应用程序从队列开始.MSMQ包含四种队列类型: 外发队列:消息发送到目的地之前,用它来临时存储消息. 公共队列:在主动目录中公布 ...
- C# 将MSMQ消息转换成Json格式 【优化】
C# 将MSMQ消息转换成Json格式 [优化] 转换函数: private string ConvertToJSON(string label, string body) { //TODO: co ...
- 即时通信系统中实现聊天消息加密,让通信更安全【低调赠送:C#开源即时通讯系统(支持广域网)——GGTalk4.5 最新源码】
在即时通讯系统(IM)中,加密重要的通信消息,是一个常见的需求.尤其在一些政府部门的即时通信软件中(如税务系统),对即时聊天消息进行加密是非常重要的一个功能,因为谈话中可能会涉及到机密的数据.我在最新 ...
- WCF使用安全证书验证消息加密
首先安装 服务端安全证书 代码如下: // 下面第一行是安装证书,第二行是将证书列入信任 makecert.exe -sr LocalMachine -ss MY -a sha1 -n CN=lo ...
- 【6】.net msmq消息队列实例
1.msmq消息队列windows环境安装 控制面板---->程序和功能---->启用或关闭Windows程序---->Microsoft Message Queue(MSMQ)服务 ...
随机推荐
- 在Visual Studio中直接编译Fluent的UDF
VS版本:Visual Studio 2013 Fluent版本:Fluent18.2 首先我们启动VS Studio中直接编译Fluent的UDF" title="在Visual ...
- rust所有权
所有权与函数 fn main() { let s = String::from("hello"); takes_ownership(s); //s的值移动到函数里 let x = ...
- 去掉 vue 的 "You are running Vue in development mode" 提示
去掉 vue 的 "You are running Vue in development mode" 提示 在项目的 main.js 中已经配置了 Vue.config.produ ...
- Linux:读取文件,每行拆分,并比较拆分数组长度
读取文件,每行拆分,并比较拆分数组长度 #!/bin/bash FILENAME=./.txt function While_read_LINE(){ cat $FILENAME | while re ...
- git 如何同步本地tag与远程tag
问题场景:同事A在本地创建tagA并push同步到了远程->同事B在本地拉取了远程tagA(git fetch)->同事A工作需要将远程标签tagA删除->同事B用git fetch ...
- oracle sequnece 介绍以及 监控
###sequnece 介绍 http://www.dba-oracle.com/t_rac_tuning_sequence_order_parameter.htm order by 可能会影响性能, ...
- Swift编码总结6
1.UILabel的minimumScaleFactor: 需要UIlabel根据字数多少来减小字体大小,使得UIlabel能够显示全所有的文字.你需要做的就是设置minimumScaleFactor ...
- LinQ中合并、连接、相交、与非查询
LinQ中Union合并查询:连接不同的集合,自动过滤相同项:延迟.即是将两个集合进行合并操作,过滤相同的项 var cities = (from p in mylinq.System_Places ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...