C#.NET 字串加解密程式
昨天韓國總部來人對網站進行了Security Review, 看到很多連結字串我們都沒有加密,批評了一下。
要把之前用的字串加解密程式拿出來用用了,順便共享,希望能幫助需要的朋友:
加密演算法使用3DES,key經過MD5雜湊過,強度應該能滿足一般需要了,程式碼很少,很方便:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace NOP.Security
{
public class Encrypt
{
public static string DecryptString(string strText, string key)
{
byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(key));
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.Key = buffer;
provider.Mode = CipherMode.ECB;
byte[] inputBuffer = Convert.FromBase64String(strText);
return Encoding.ASCII.GetString(provider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
}
public static string DecryptUTF8String(string strText, string key)
{
byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.Key = buffer;
provider.Mode = CipherMode.ECB;
byte[] inputBuffer = Convert.FromBase64String(strText);
return Encoding.UTF8.GetString(provider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
}
public static string EncryptString(string strText, string key)
{
byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(key));
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.Key = buffer;
provider.Mode = CipherMode.ECB;
byte[] bytes = Encoding.ASCII.GetBytes(strText);
string str = Convert.ToBase64String(provider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length));
provider = null;
return str;
}
public static string EncryptUTF8String(string strText, string key)
{
byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.Key = buffer;
provider.Mode = CipherMode.ECB;
byte[] bytes = Encoding.UTF8.GetBytes(strText);
string str = Convert.ToBase64String(provider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length));
provider = null;
return str;
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace NOP.Security
{
public class Encrypt
{
public static string DecryptString(string strText, string key)
{
byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(key));
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.Key = buffer;
provider.Mode = CipherMode.ECB;
byte[] inputBuffer = Convert.FromBase64String(strText);
return Encoding.ASCII.GetString(provider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
}
public static string DecryptUTF8String(string strText, string key)
{
byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.Key = buffer;
provider.Mode = CipherMode.ECB;
byte[] inputBuffer = Convert.FromBase64String(strText);
return Encoding.UTF8.GetString(provider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
}
public static string EncryptString(string strText, string key)
{
byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(key));
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.Key = buffer;
provider.Mode = CipherMode.ECB;
byte[] bytes = Encoding.ASCII.GetBytes(strText);
string str = Convert.ToBase64String(provider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length));
provider = null;
return str;
}
public static string EncryptUTF8String(string strText, string key)
{
byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.Key = buffer;
provider.Mode = CipherMode.ECB;
byte[] bytes = Encoding.UTF8.GetBytes(strText);
string str = Convert.ToBase64String(provider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length));
provider = null;
return str;
}
}
}
作者:Lance Zhang(Lance Zhang's Tech
Blog)
出處:http://blodfox777.cnblogs.com/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-617465/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [C#.NET拾遺補漏]01:字串操作C#字串
- bash對字串做簡單加解密及應用字串解密
- C#.Net築基-String字串超全總結 [深度好文]C#字串
- 【Java加解密系列】- SM2加解密Java解密
- 前端加解密前端解密
- C#.NET擴充套件方法C#套件
- 客戶端加解密客戶端解密
- 遊戲異或加解密遊戲解密
- 加解密演算法解密演算法
- Java RSA 分段加解密Java解密
- 移動端加解密解密
- 介面加密傳輸設計及AES加解密程式碼DEMO加密解密
- C#.net winform skin 皮膚大全C#ORM
- 加解密演算法分析解密演算法
- 對稱EDS加解密方法解密
- NodeJS加解密之CryptoNodeJS解密
- Java之DES加解密解析Java解密
- Java之RSA加解密解析Java解密
- C# RSA 分段加解密C#解密
- 3DES加解密-EncryptAndDecryptFile3D解密
- Openssl RSA基本加解密操作解密
- c#.net常用函式和方法集C#函式
- C#.Net築基-基礎知識C#
- C#.Net築基-模式匹配彙總C#模式
- vue 核心加解密工具類 方法Vue解密
- AES加解密使用總結解密
- PHP 支援加解密的函式PHP解密函式
- C語言(檔案加解密)C語言解密
- .NET AES加解密(128位)解密
- jive對密碼的加解密?密碼解密
- python各種加解密方法Python解密
- 安全閘道器 透明加解密解密
- 字串的過載程式字串
- C#.net 獲取當前應用程式所在路徑及環境變數C#變數
- C#.NET 中你必須知道的反射C#反射
- C#.NET 中的型別轉換 ztC#型別
- c#.net常用的小函式集1C#函式
- C#.Net築基-解密委託與事件C#解密事件