加密算法是这样的
protected static string DesEncrypt(string aStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] encryData;
des.Key = m_DesKey;
des.IV = m_DesIV;
MemoryStream ms = new MemoryStream();
encryData = ASCIIEncoding.Default.GetBytes(aStr);
CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write );
encStream.Write(encryData, 0, encryData.Length );
encStream.Close();
ms.Flush();
encryData = ms.GetBuffer() ;
string result = ASCIIEncoding.Default.GetString(encryData); return result.Trim('\0');
}
其中m_DesKey和m_DesIV为成员变量,请指点对应的解密算法应该如何写?
protected static string DesEncrypt(string aStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] encryData;
des.Key = m_DesKey;
des.IV = m_DesIV;
MemoryStream ms = new MemoryStream();
encryData = ASCIIEncoding.Default.GetBytes(aStr);
CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write );
encStream.Write(encryData, 0, encryData.Length );
encStream.Close();
ms.Flush();
encryData = ms.GetBuffer() ;
string result = ASCIIEncoding.Default.GetString(encryData); return result.Trim('\0');
}
其中m_DesKey和m_DesIV为成员变量,请指点对应的解密算法应该如何写?
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string EncryptDES(string encryptString,string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream,dCSP.CreateEncryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
cStream.Write(inputByteArray,0,inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
} /// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DecryptDES(string decryptString,string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream,DCSP.CreateDecryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
cStream.Write(inputByteArray,0,inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
byte[] rgbIV = Keys;
这里好象不对吧
Keys是类呀
//默认密钥向量
private static byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};完整的代码:
//默认密钥向量
private static byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string EncryptDES(string encryptString,string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream,dCSP.CreateEncryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
cStream.Write(inputByteArray,0,inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
} /// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DecryptDES(string decryptString,string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream,DCSP.CreateDecryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
cStream.Write(inputByteArray,0,inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
jiezhi()楼主可以结贴了,jiezhi()的可行
* Copyright(C) 2004-2005 ******** All Rights Reserved.
************************************************************************/using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Windows.Forms;
/// <summary>
/// Triple Data Encryption Standard algorithms implementations
/// </summary>
/// <Author>Yao</Author>
/// <Date>2005/4/20</Date>public class CryptionData
{
// The length of Encryptionstring should be 8 byte and not be a weak key
private string EncryptionString; // The length of initialization vector should be 8 byte
private static Byte[] EncryptionIV = Encoding.Default.GetBytes("abcdefgh"); /// <summary>
/// Constructor
/// </summary>
public CryptionData()
{
} /// <summary>
/// Constructor
/// </summary>
/// <param name="EncryptionString">SecureKey</param>
public CryptionData(string EncryptionString)
{
this.EncryptionString = EncryptionString;
} /// <summary>
/// Encryption method for byte array
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>byte array</returns>
public byte[] EncryptionByteData(byte[] SourceData)
{
byte[] returnData = null;
try
{
// Create DESCryptoServiceProvider object
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); // Set SecureKey and IV of desProvider
byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
desProvider.Key = byteKey;
desProvider.IV = EncryptionIV;
// A MemoryStream object
MemoryStream ms = new MemoryStream();
// Create Encryptor
ICryptoTransform encrypto = desProvider.CreateEncryptor();
// Create CryptoStream object
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
// Encrypt SourceData
cs.Write(SourceData,0,SourceData.Length);
cs.FlushFinalBlock();
// Get Encryption result
returnData = ms.ToArray();
}
catch(Exception ex)
{
throw ex;
}
return returnData; } /// <summary>
/// Decryption method for byte array
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>byte array</returns>
public byte[] DecryptionByteData(byte[] SourceData)
{
byte[] returnData = null;
try
{
// Create DESCryptoServiceProvider object
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); // Set SecureKey and IV of desProvider
byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
desProvider.Key = byteKey;
desProvider.IV = EncryptionIV;
// A MemoryStream object
MemoryStream ms = new MemoryStream();
// Create Decryptor
ICryptoTransform encrypto = desProvider.CreateDecryptor();
// Create CryptoStream object
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); // Decrypt SourceData
cs.Write(SourceData, 0, SourceData.Length);
cs.FlushFinalBlock();
// Get Decryption result
returnData = ms.ToArray();
}
catch(Exception ex)
{
throw ex;
}
return returnData;
} /// <summary>
/// Encryption method for string
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>string</returns>
public string EncryptionStringData(string SourceData)
{
try
{
// Convert source data from string to byte array
byte[] SourData = Encoding.Default.GetBytes(SourceData); // Encrypt byte array
byte[] retData = EncryptionByteData(SourData); // Convert encryption result from byte array to Base64String
return Convert.ToBase64String(retData, 0, retData.Length);
}
catch(Exception ex)
{
throw ex;
}
} /// <summary>
/// Decryption method for string
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>string</returns>
public string DecryptionStringdata(string SourceData)
{
try
{
// Convert source data from Base64String to byte array
byte[] SourData = Convert.FromBase64String(SourceData); // Decrypt byte array
byte[] retData = DecryptionByteData(SourData); // Convert Decryption result from byte array to string
return Encoding.Default.GetString(retData, 0, retData.Length);
}
catch(Exception ex)
{
throw ex;
}
}
}
这是最初开发的一个类,加密字节数组这个方法是用来加密小的图像文件的。把图像读到字节数组中,就可以解密解密了。后来改造过一次,加密字符串用的是TripleDES,加密图像,因为速度的关系采用DES算法的。