小弟目前在作一个项目,要求是客户端用C++的RSA库指定的公钥加密一串字符(非微软cryptapi),服务器端用对应的私匙解密,但是小弟目前不知道如何将私匙的信息导入到rsaparameters里,密钥
对是用rsatools 生成的,我也不知道D,DP等对应的是rsatools里的哪个参数,请问如何解决这个问题?就是用RSACrypttoServiceProvider同时指定私匙作解密,代码如何写?私匙已知,生成私匙的P,Q已知,请高手指点。
对是用rsatools 生成的,我也不知道D,DP等对应的是rsatools里的哪个参数,请问如何解决这个问题?就是用RSACrypttoServiceProvider同时指定私匙作解密,代码如何写?私匙已知,生成私匙的P,Q已知,请高手指点。
public string DesEncrypt(string strText, string strEncrKey)
{
if(strEncrKey == String.Empty)
{
strEncrKey = "songxiud";
}
byte[] byKey=null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,strEncrKey.Length));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(System.Exception error)
{
return "error:" +error.Message+"\r";
}
} //解密一个给定的字符串,sDecrKey是一个字符串做为解密算子,
public string DesDecrypt(string strText,string sDecrKey)
{
if(sDecrKey == "")
{
sDecrKey = "songxiud";
}
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byte[] inputByteArray = Convert.FromBase64String(strText);
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception error)
{
return "error:"+error.Message+"\r";
}
}
ref:
http://www.codeproject.com/dotnet/RSACryptoPad.asp
namespace System.Security.Cryptography
{
using System;
using System.Runtime.InteropServices; [Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
public struct RSAParameters
{
public byte[] Exponent;
public byte[] Modulus;
[NonSerialized]
public byte[] P;
[NonSerialized]
public byte[] Q;
[NonSerialized]
public byte[] DP;
[NonSerialized]
public byte[] DQ;
[NonSerialized]
public byte[] InverseQ;
[NonSerialized]
public byte[] D;
}
}