您的位置:首页 > 其它

谈谈数据加密的处理--提供各种算法处理

2011-09-02 10:46 295 查看
[1] 谈谈数据加密的处理--提供各种算法处理

[2] 谈谈数据加密的处理--提供各种算法处理

[3] 谈谈数据加密的处理--提供各种算法处理

[4] 谈谈数据加密的处理--提供各种算法处理

  数据的加密重要性大家皆知,很多情况下需要对数据进行加密处理,但各种重要数据的加密要求不一样,有些需要时可逆的,有些是不要求可逆的,可逆的一般称之为对称加密算法,不可逆的一般可以成为非对称加密算法。如登录密码,一般较好的方式是采用不可逆的加密算法,如MD5、SHA256、哈希数值等,当然也有的采用可逆的强度好一些的加密方式,在选择加密键值的时候,变化一下也算是比较不错的选择。另外一些为了掩人耳目或者不让别人直接可以查看到,就采用其他的加密算法,如DES加密算法、AES的RijndaelManaged加密算法,或者也可以采用Base64加密,甚至我看到一个很变态的方式,就是用MD5加密的头,内容则采用Base64的方式加密,一般不知道内情的还真的不好处理。

  在吉日《[走火入魔失眠夜]浅谈管理软件信息安全,用户名、密码的加密解密【附C#配套加密解密源码】》 的文章中也对加密做了一些介绍和分析,并贴出了MD5、DES的加密方式代码,吉日的文章图文并茂、哲理及诙谐例子并存,本文有感而发,做一些补充,希望园子同行共通过切磋,交流心得。

  加密字符串的方式有很多,也都可以通过这样的加密文件内容,不过较好的方式可能是采用DES及AES两种方式来加密文件了,下面贴出的代码中包含了加解密文件的算法。下面具体贴出本人收藏的各种加密算法代码。

  1、DES加密字符串及文件等

  如果想可逆的算法,这种方式一般不错,只要结合动态密钥,就可以做出强度比较高的加密应用了。

public const string DEFAULT_ENCRYPT_KEY = "12345678";

/// <summary>

/// 使用默认加密

/// </summary>

/// <param name="strText"></param>

/// <returns></returns>

public static string DesEncrypt(string strText)

{

try

{

return DesEncrypt(strText, DEFAULT_ENCRYPT_KEY);

}

catch

{

return "";

}

}

/// <summary>

/// 使用默认解密

/// </summary>

/// <param name="strText"></param>

/// <returns></returns>

public static string DesDecrypt(string strText)

{

try

{

return DesDecrypt(strText, DEFAULT_ENCRYPT_KEY);

}

catch

{

return "";

}

}

/// <summary>

/// Encrypt the string

/// Attention:key must be 8 bits

/// </summary>

/// <param name="strText">string</param>

/// <param name="strEncrKey">key</param>

/// <returns></returns>

public static string DesEncrypt(string strText, string strEncrKey)

{

byte[] byKey = null;

byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

byKey = Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));

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());

}

/// <summary>

/// Decrypt string

/// Attention:key must be 8 bits

/// </summary>

/// <param name="strText">Decrypt string</param>

/// <param name="sDecrKey">key</param>

/// <returns>output string</returns>

public static string DesDecrypt(string strText, string sDecrKey)

{

byte[] byKey = null;

byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

byte[] inputByteArray = new Byte[strText.Length];

byKey = 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();

Encoding encoding = new UTF8Encoding();

return encoding.GetString(ms.ToArray());

}

/// <summary>

/// Encrypt files

/// Attention:key must be 8 bits

/// </summary>

/// <param name="m_InFilePath">Encrypt file path</param>

/// <param name="m_OutFilePath">output file</param>

/// <param name="strEncrKey">key</param>

public static void DesEncrypt(string m_InFilePath, string m_OutFilePath, string strEncrKey)

{

byte[] byKey = null;

byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

byKey = Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));

FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);

FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);

fout.SetLength(0);

//Create variables to help with read and write.

byte[] bin = new byte[100]; //This is intermediate storage for the encryption.

long rdlen = 0; //This is the total number of bytes written.

long totlen = fin.Length; //This is the total length of the input file.

int len; //This is the number of bytes to be written at a time.

DES des = new DESCryptoServiceProvider();

CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);

//Read from the input file, then encrypt and write to the output file.

while (rdlen < totlen)

{

len = fin.Read(bin, 0, 100);

encStream.Write(bin, 0, len);

rdlen = rdlen + len;

}

encStream.Close();

fout.Close();

fin.Close();

}

/// <summary>

/// Decrypt files

/// Attention:key must be 8 bits

/// </summary>

/// <param name="m_InFilePath">Decrypt filepath</param>

/// <param name="m_OutFilePath">output filepath</param>

/// <param name="sDecrKey">key</param>

public static void DesDecrypt(string m_InFilePath, string m_OutFilePath, string sDecrKey)

{

byte[] byKey = null;

byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

byKey = Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));

FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);

FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);

fout.SetLength(0);

//Create variables to help with read and write.

byte[] bin = new byte[100]; //This is intermediate storage for the encryption.

long rdlen = 0; //This is the total number of bytes written.

long totlen = fin.Length; //This is the total length of the input file.

int len; //This is the number of bytes to be written at a time.

DES des = new DESCryptoServiceProvider();

CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);

//Read from the input file, then encrypt and write to the output file.

while (rdlen < totlen)

{

len = fin.Read(bin, 0, 100);

encStream.Write(bin, 0, len);

rdlen = rdlen + len;

}

encStream.Close();

fout.Close();

fin.Close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: