.net 加密方法记录

来源:互联网 发布:穿过的丝袜淘宝这么搜 编辑:程序博客网 时间:2024/06/05 19:45

为了安全起见,现在在很多地方要用到加密技术,现将之前用到的一些加密技术做以下归纳:

MD5:

USING system.web.security;

Class ... {

 

public string md5(string str,int code)
{
if(code==16) //16位MD5加密(取32位加密的9~25字符)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower().Substring(8,16) ;
}
else//32位加密
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower();
}
}

}
RSA:

1. encrypt string

using System.Security.Cryptography;

Class ...{

 

private const string strEncryptionPasswordPrivateKey = "";
  private const string strEncryptionPasswordPublicKey = "";
  private const string dbEnryptionPassPhraseStr = "5airf0925df";

 

  ebCryptLib.eb_c_RSAKeyClass rsaKey = null; // using ebCryptLib.dll

  rsaKey = new ebCryptLib.eb_c_RSAKeyClass();
    rsaKey.ImportPrivateKey(ebCryptLib.EB_CRYPT_EXPORT_FORMAT.EB_CRYPT_EXPORT_FORMAT_DER,    passPhraseStr, privateKeyStr);    

    byteArray = new byte[1];
    for (int i = 0; i < dataToEncrypt.Length; i++)
    {
     byteArray[0] = Convert.ToByte(dataToEncrypt[i]);
     tmpStr = string.Format("{0:x2}", byteArray[0]);

     if (tmpStr.Length == 1)
      ipStr = ipStr + "0" + tmpStr;
     else
      ipStr = ipStr + tmpStr;
    }

    encryptedData = rsaKey.PrivateEncrypt(ipStr);

}

 

2. encrypt file

public static bool EncryptFile(string inputFilePath, string outputFilePath)
  {
   try
   {
    ebCryptWrapper.classMainClass mainClass=new ebCryptWrapper.classMainClass();
    bool bVal=mainClass.EncryptFile(inputFilePath,outputFilePath,dbEnryptionPassPhraseStr);
    return bVal;
   }
   catch
   {
    return false;
   }
  }