本程序的主要功能是实现对指定文件的对称加密,可选算法包括DES,Triple DES,Rijndael。具体加密过程使用.NET自己定义的加密与解密方法。在执行加密过程中可以自由选择填充方式。为了使密钥足够安全所以程序采用自动生成密钥与初始向量的办法。对密钥的管理采用二进制格式加密后存储,用户只能导入和导出密钥。
* 实现对数据的DES加密
*
* 调试:VS.NET 2005 beta2.0
*
****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Security;
using System.IO;
//***************************************************************************/
namespace SJKlaus.Security
{
public class SJKlaus_DES
{
public static byte[] SJK_DES(byte[] Key, byte[] IV, CipherMode Mode, PaddingMode Padding, byte[] plainbytes)
{
try
{
byte[] cipherStr;
SymmetricAlgorithm sa = DES.Create();
//初始化加密参数
sa.Key = Key;
sa.IV = IV;
sa.Mode = Mode;
sa.Padding = Padding;
/*****************************************/
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
//byte[] plainbytes = Encoding.UTF8.GetBytes(str);
cs.Write(plainbytes, 0, plainbytes.Length);
cs.Close();
cipherStr = ms.ToArray();
ms.Close();
return cipherStr;
}
catch(Exception ex)
{
byte[] err=Encoding.UTF8.GetBytes(ex.Message);
return err;
}
}
//*******************************************************************************************
public static byte[] SJK_DeDES(byte[] Key,byte[] IV,CipherMode Mode,PaddingMode Padding,byte[] cipherbytes)
{
try
{
SymmetricAlgorithm sa = DES.Create();
sa.Key = Key;
sa.IV = IV;
sa.Mode = Mode;
sa.Padding = Padding;
//**********************************
MemoryStream ms = new MemoryStream(cipherbytes);
CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(), CryptoStreamMode.Read);
byte[] plainbytes=new byte[cipherbytes.Length];
cs.Read(plainbytes, 0, cipherbytes.Length);
cs.Close();
ms.Close();
return plainbytes;
}
catch
{
byte[] err = Encoding.UTF8.GetBytes("There is an error");
return err;
}
}
}
}
* 实现对数据的TripleDes加密
*
* 调试:VS.NET 2005 beta2.0
*
****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Security;
using System.IO;
//***************************************************************************/
namespace SJKlaus.Security
{
public class SJKlaus_TripleDES
{
public static byte[] SJK_TripleDES(byte[] Key, byte[] IV, CipherMode Mode, PaddingMode Padding, byte[] plainbytes)
{
try
{
byte[] cipherStr;
SymmetricAlgorithm sa = TripleDES.Create();
//初始化加密参数
sa.Key = Key;
sa.IV = IV;
sa.Mode = Mode;
sa.Padding = Padding;
/*****************************************/
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
//byte[] plainbytes = Encoding.UTF8.GetBytes(str);
cs.Write(plainbytes, 0, plainbytes.Length);
cs.Close();
cipherStr = ms.ToArray();
ms.Close();
return cipherStr;
}
catch (Exception ex)
{
byte[] err = Encoding.UTF8.GetBytes(ex.Message);
return err;
}
}
//*******************************************************************************************
public static byte[] SJK_DeTripleDES(byte[] Key, byte[] IV, CipherMode Mode, PaddingMode Padding, byte[] cipherbytes)
{
try
{
SymmetricAlgorithm sa = TripleDES.Create();
sa.Key = Key;
sa.IV = IV;
sa.Mode = Mode;
sa.Padding = Padding;
//**********************************
MemoryStream ms = new MemoryStream(cipherbytes);
CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(), CryptoStreamMode.Read);
byte[] plainbytes = new byte[cipherbytes.Length];
cs.Read(plainbytes, 0, cipherbytes.Length);
cs.Close();
ms.Close();
return plainbytes;
}
catch (Exception ex)
{
byte[] err = Encoding.UTF8.GetBytes(ex.Message);
return err;
}
}
}
}
*
* 实现对数据的Rijndael加密
*
* 调试:VS.NET 2005 beta2.0
*
****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Security;
using System.IO;
//***************************************************************************/
namespace SJKlaus.Security
{
public class SJKlaus_Rijndael
{
public static byte[] SJK_Rijndael(byte[] Key, byte[] IV, CipherMode Mode, PaddingMode Padding, byte[] plainbytes)
{
try
{
byte[] cipherStr;
SymmetricAlgorithm sa =Rijndael.Create();
//初始化加密参数
sa.Key = Key;
sa.IV = IV;
sa.Mode = Mode;
sa.Padding = Padding;
/*****************************************/
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
//byte[] plainbytes = Encoding.UTF8.GetBytes(str);
cs.Write(plainbytes, 0, plainbytes.Length);
cs.Close();
cipherStr = ms.ToArray();
ms.Close();
return cipherStr;
}
catch (Exception ex)
{
byte[] err = Encoding.UTF8.GetBytes(ex.Message);
return err;
}
}
//*******************************************************************************************
public static byte[] SJK_DeRijndael(byte[] Key, byte[] IV, CipherMode Mode, PaddingMode Padding, byte[] cipherbytes)
{
try
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = Key;
sa.IV = IV;
sa.Mode = Mode;
sa.Padding = Padding;
//**********************************
MemoryStream ms = new MemoryStream(cipherbytes);
CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(), CryptoStreamMode.Read);
byte[] plainbytes = new byte[cipherbytes.Length];
cs.Read(plainbytes, 0, cipherbytes.Length);
cs.Close();
ms.Close();
return plainbytes;
}
catch (Exception ex)
{
byte[] err = Encoding.UTF8.GetBytes(ex.Message);
return err;
}
}
}
}
*
* 实现对数据的RC2加密
*
* 调试:VS.NET 2005 beta2.0
*
****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Security;
using System.IO;
//***************************************************************************/
namespace SJKlaus.Security
{
public class SJKlaus_RC2
{
public static byte[] SJK_RC2(byte[] Key, byte[] IV, CipherMode Mode, PaddingMode Padding, byte[] plainbytes)
{
try
{
byte[] cipherStr;
SymmetricAlgorithm sa =RC2.Create();
//初始化加密参数
sa.Key = Key;
sa.IV = IV;
sa.Mode = Mode;
sa.Padding = Padding;
/*****************************************/
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
//byte[] plainbytes = Encoding.UTF8.GetBytes(str);
cs.Write(plainbytes, 0, plainbytes.Length);
cs.Close();
cipherStr = ms.ToArray();
ms.Close();
return cipherStr;
}
catch (Exception ex)
{
byte[] err = Encoding.UTF8.GetBytes(ex.Message);
return err;
}
}
//*******************************************************************************************
public static byte[] SJK_DeRC2(byte[] Key, byte[] IV, CipherMode Mode, PaddingMode Padding, byte[] cipherbytes)
{
try
{
SymmetricAlgorithm sa = RC2.Create();
sa.Key = Key;
sa.IV = IV;
sa.Mode = Mode;
sa.Padding = Padding;
//**********************************
MemoryStream ms = new MemoryStream(cipherbytes);
CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(), CryptoStreamMode.Read);
byte[] plainbytes = new byte[cipherbytes.Length];
cs.Read(plainbytes, 0, cipherbytes.Length);
cs.Close();
ms.Close();
return plainbytes;
}
catch (Exception ex)
{
byte[] err = Encoding.UTF8.GetBytes(ex.Message);
return err;
}
}
}
}
注意:本文为作者(sinory)原创,转载请注明。