小弟目前在作一个项目,要求是客户端用C++的RSA库指定的公钥加密一串字符(非微软cryptapi),服务器端用对应的私匙解密,但是小弟目前不知道如何将私匙的信息导入到rsaparameters里,密钥
对是用rsatools 生成的,我也不知道D,DP等对应的是rsatools里的哪个参数,请问如何解决这个问题?就是用RSACrypttoServiceProvider同时指定私匙作解密,代码如何写?私匙已知,生成私匙的P,Q已知,请高手指点。

解决方案 »

  1.   

    //加密一个给定的字符串,strEncrKey是一个字符串做为加密算子
    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";
    }
    }
      

  2.   

    动作好快啊,不过2楼好象给得是DES加密解密啊。
      

  3.   

    RSACrypttoServiceProvider有提供ImportParameters方法,可将密钥传进去
      

  4.   

    Public Key RSA Encryption in C# .NET
    ref:
    http://www.codeproject.com/dotnet/RSACryptoPad.asp
      

  5.   

    RSACrypttoServiceProvider有提供ImportParameters方法,可将密钥传进去是这样,不过rsaparameters需要的各属性我不是很懂,能否举个例子。
      

  6.   

    另外,愚翁的例子我也看了,只是生成密钥对,保存到XML文件中,再取出来加密解密,没有解决我的VC++的密钥对在C#上使用的问题。
      

  7.   

    自己去看看有关DES类吧  有你要的东西
      

  8.   

    反编译.net framework 2.0的mscorlib.dll得到的RSAParameters结构类型定义:
    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;
        }
    }