| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3100 人关注过本帖
标题:关于RSA的私钥加密,公钥解密的问题
只看楼主 加入收藏
gsbe1987
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2007-4-14
结帖率:100%
收藏
已结贴  问题点数:80 回复次数:4 
关于RSA的私钥加密,公钥解密的问题
程序代码:
/// <summary>
        /// 对原始数据进行MD5加密
        /// </summary>
        /// <param name="key">待加密的注册码</param>
        /// <returns>加密后的注册码</returns>
        public static string GetMD5(string key)
        {
            HashAlgorithm algorithm = HashAlgorithm.Create("MD5");
            byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(key);
            byte[] inArray = (bytes);
            string strEncrypt = Convert.ToBase64String(inArray);
            return strEncrypt;

        }

/// <summary>
        /// 使用私钥加密客户注册码[注册码+GUID码]
        /// </summary>
        /// <param name="key">客户注册码[注册码+GUID码]</param>
        /// <param name="keyContainerName">密钥容器名称</param>
        /// <param name="keyPath">私钥文件地址</param>
        /// <returns>加密后的注册码</returns>
        public static string EncryptKey(string key,string keyContainerName, string keyPath)
        {
            CspParameters cspParameter = new CspParameters();
            StreamReader sr = new StreamReader(keyPath);
            cspParameter.KeyContainerName = keyContainerName;
            string strKey = sr.ReadLine();
            string strEncrypt = string.Empty;
            byte[] byteKeys = Convert.FromBase64String(GetMD5(key));

            RSACryptoServiceProvider provider = new RSACryptoServiceProvider(cspParameter);
            provider.FromXmlString(strKey);
            RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(provider);
            formatter.SetHashAlgorithm("MD5");
            byte[] byteEncrypt = formatter.CreateSignature(byteKeys);
            strEncrypt = Convert.ToBase64String(byteEncrypt);
            return strEncrypt;
        }

        /// <summary>
        /// 使用公钥解密客户注册码验证
        /// </summary>
        /// <param name="publicKey">公钥</param>
        /// <param name="key">授权文件信息</param>
        /// <param name="computeInfo">电脑硬件信息</param>
        public static bool Decrypt(string publicKey,string key,string computeInfo)
        {
            try
            {
                CspParameters cspParameter = new CspParameters();
                cspParameter.KeyContainerName = "public";
                RSACryptoServiceProvider provider = new RSACryptoServiceProvider(cspParameter);
                provider.FromXmlString(publicKey);
                RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(provider);
                deformatter.SetHashAlgorithm("MD5");

                //转换授权文件信息字符串
                byte[] byteAuthorization = Convert.FromBase64String(key);
                //转换当前电脑硬件信息
                byte[] byteComputeInfo = Convert.FromBase64String(GetMD5(computeInfo));
                if (deformatter.VerifySignature(byteAuthorization, byteComputeInfo))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                WriteSystemLog(5, 3, "000000", ex.Message);
                throw new Exception(ex.Message, ex);
            }
        }

执行以上代码之后提示“不正确的散列算法”,调试之后发现问题出在解密那里,到了
if (deformatter.VerifySignature(byteAuthorization, byteComputeInfo))
这里就出现异常,然后我把两个参数调换了一下位置之后就没出错了,但是结果总是false,不知道是哪里的数据出错了,我一步步地调,但是就是找不出来,快崩溃了~~有哪位大虾看出啥问题,帮忙指点一下迷津~~
搜索更多相关主题的帖子: RSA 
2009-08-24 16:16
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
收藏
得分:56 
你这不完整的代码怎么让人家给你看问题在哪啊?

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-08-24 18:18
gsbe1987
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2007-4-14
收藏
得分:0 
/// <summary>
        /// 生成公私钥文件
        /// </summary>
        /// <param name="isPrivate">是否为私钥</param>
        /// <param name="keyContainerName">密钥容器的名称</param>
        /// <param name="keyPath">保存公私钥文件的地址</param>
        public static void ParseKey(bool isPrivate, string keyContainerName,string keyPath)
        {
            CspParameters cspParameter = new CspParameters();
            cspParameter.KeyContainerName = keyContainerName;
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider(cspParameter);
            StreamWriter sw = new StreamWriter(keyPath);
            sw.WriteLine(provider.ToXmlString(isPrivate));
            sw.Close();
            sw.Dispose();
            provider.PersistKeyInCsp = false;
            provider.Clear();
        }

/// <summary>
        /// 生成授权KEY文件
        /// </summary>
        /// <param name="key">使用私钥加密后的KEY</param>
        /// <param name="keyPath">保存授权KEY文件的地址</param>
        public static void BuildKey(string key, string keyPath)
        {
            StreamWriter sw = new StreamWriter(keyPath);
            sw.WriteLine(key);
            sw.Close();
            sw.Dispose();
        }

主要入口从这个方法开始执行:
private static string machinePath = string.Format("{0}\\public.key", Directory.GetCurrentDirectory());
private static string keyPath = string.Format("{0}\\mange.key", Directory.GetCurrentDirectory());
        /// <summary>
        /// 授权是否通过
        /// </summary>
        /// <returns></returns>
        public static bool isPass()
        {
            try
            {
                if (File.Exists(machinePath) && File.Exists(keyPath))
                {
                    string strPublicKey = string.Empty;
                    string strKey = string.Empty;
                    StreamReader sr = new StreamReader(machinePath);
                    strPublicKey = sr.ReadLine();
                    sr = new StreamReader(keyPath);
                    strKey = sr.ReadLine();
                    if (FrameWork.Decrypt(strPublicKey, strKey, string.Format("{0}-{1}", Identity.GuidCode, FrameWork.GetMD5(GetComputeInfo()))))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                FrameWork.WriteSystemLog(5, 3, "000000", ex.Message);
                throw new Exception(ex.Message, ex);
            }
        }
调用以上各方法~~这是全部的代码了

便宜又好看的U盘,款式多多~~店铺地址:http://shop34991947.
2009-08-25 08:19
gsbe1987
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2007-4-14
收藏
得分:0 
另外GetComputeInfo()这个方法是获取电脑硬件信息(CPU,硬盘,MAC)

便宜又好看的U盘,款式多多~~店铺地址:http://shop34991947.
2009-08-25 08:21
gsbe1987
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2007-4-14
收藏
得分:0 
自己顶一下

便宜又好看的U盘,款式多多~~店铺地址:http://shop34991947.
2009-08-27 12:08
快速回复:关于RSA的私钥加密,公钥解密的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028284 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved