已有56人关注
如何进行加密解密
发表在C#图书答疑 2011-06-27
是否精华
版块置顶:
MD5CryptoServiceProvider M5 = new MD5CryptoServiceProvider();
            textBox2.Text = ASCIIEncoding.ASCII.GetString(M5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(textBox1.Text)));

C#编程词典 提供了字符串的加密功能,而没有提供解密该字符串的功能啊............
请问解密的方法怎么解???
分享到:
精彩评论 1
学分: LV1
TA的每日心情
开心
2021-09-09 11:01:22
2011-06-27
沙发
读者你好:
    关于解密字符串您可以参考以下方法:
     /// <summary>
  /// 3des解密字符串
  /// </summary>
  /// <param name="a_strString">要解密的字符串</param>
  /// <param name="a_strKey">密钥</param>
  /// <returns>解密后的字符串</returns>
  /// <exception cref="">密钥错误</exception>
  /// <remarks>静态方法,采用默认ascii编码</remarks>
  public static string Decrypt3DES(string a_strString, string a_strKey)
  {
   TripleDESCryptoServiceProvider DES = new
    TripleDESCryptoServiceProvider();
   MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

   DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
   DES.Mode = CipherMode.ECB;

   ICryptoTransform DESDecrypt = DES.CreateDecryptor();

   string result = "";
   try
   {
    byte[] Buffer = Convert.FromBase64String(a_strString);
    result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock
     (Buffer, 0, Buffer.Length));
   }
   catch//(Exception e)
   {

   }

   return result ;
  }

  /// <summary>
  /// 3des解密字符串
  /// </summary>
  /// <param name="a_strString">要解密的字符串</param>
  /// <param name="a_strKey">密钥</param>
  /// <param name="encoding">编码方式</param>
  /// <returns>解密后的字符串</returns>
  /// <exception cref="">密钥错误</exception>
  /// <remarks>静态方法,指定编码方式</remarks>
  public static string Decrypt3DES(string a_strString, string a_strKey , Encoding encoding)
  {
   TripleDESCryptoServiceProvider DES = new
    TripleDESCryptoServiceProvider();
   MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

   DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
   DES.Mode = CipherMode.ECB;

   ICryptoTransform DESDecrypt = DES.CreateDecryptor();

   string result = "";
   try
   {
    byte[] Buffer = Convert.FromBase64String(a_strString);
    result = encoding.GetString(DESDecrypt.TransformFinalBlock
     (Buffer, 0, Buffer.Length));
   }
   catch //(Exception e)
   {
    //#if DEBUG
    //    Console.WriteLine("错误:{0}" , e) ;
    //#endif//DEBUG
//    throw(new Exception("Invalid Key or input string is not a valid base64 string" , e)) ;
   }
   return result ;
  }//end method
 }
}
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照