文件的加密与解密算法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

using System;

using System.IO;

using System.Security.Cryptography;

using System.Text;

namespace BS.Utility

{

public class MySecurity

{

#region 构造函数

public MySecurity()

{

}

#endregion

#region ( 0 )Rijndael算法

private SymmetricAlgorithm mobjCryptoService= new RijndaelManaged(); private static string Key= "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ

$lhj!y6&(*jkP87jH7 ";

///

/// 获得密钥

///

/// 密钥

private byte[] GetLegalKey()

{

string sTemp = Key;

mobjCryptoService.GenerateKey();

byte[] bytTemp = mobjCryptoService.Key;

int KeyLength = bytTemp.Length;

if (sTemp.Length > KeyLength)

sTemp = sTemp.Substring(0, KeyLength);

else if (sTemp.Length < KeyLength)

sTemp = sTemp.PadRight(KeyLength, ' ');

return ASCIIEncoding.ASCII.GetBytes(sTemp);

}

///

/// 获得初始向量IV

///

/// 初试向量IV

{

string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg

4ui%$hjk ";

mobjCryptoService.GenerateIV();

byte[] bytTemp = mobjCryptoService.IV;

int IVLength = bytTemp.Length;

if (sTemp.Length > IVLength)

sTemp = sTemp.Substring(0, IVLength);

else if (sTemp.Length < IVLength)

sTemp = sTemp.PadRight(IVLength, ' ');

return ASCIIEncoding.ASCII.GetBytes(sTemp);

}

///

/// 加密方法

///

/// 待加密的串

/// 经过加密的串

public string RijndaelEncrypt(string Source)

{

byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);

MemoryStream ms = new MemoryStream();

mobjCryptoService.Key = GetLegalKey();

mobjCryptoService.IV = GetLegalIV();

ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

cs.Write(bytIn, 0, bytIn.Length);

cs.FlushFinalBlock();

ms.Close();

byte[] bytOut = ms.ToArray();

return Convert.ToBase64String(bytOut);

}

///

/// 解密方法

///

/// 待解密的串

/// 经过解密的串

public string RijndaelDecrypt(string Source)

{

byte[] bytIn = Convert.FromBase64String(Source);

MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);

mobjCryptoService.Key = GetLegalKey();

mobjCryptoService.IV = GetLegalIV();

ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);

StreamReader sr = new StreamReader(cs);

return sr.ReadToEnd();

}

#endregion

#region ( 1 )Base64与UTF8混用

public string BUEncrypt( string bb )

{

byte[] by=new byte[bb.Length];

by=System.Text.Encoding.UTF8.GetBytes( bb );

string r=Convert.ToBase64String( by );

return r;

}

public string BUDecrypt( string aa )

{

byte[] by=Convert.FromBase64String( aa );

string r=Encoding.UTF8.GetString( by );

return r;

}

#endregion

#region ( 2 )倒序加1与顺序减1(中文支持不好)

public string AddEncrypt( string rs ) //倒序加1加密

{

byte[] by=new byte[rs.Length];

for( int i=0;

i <=rs.Length-1;

i++ )

{

by[i]=( byte )( ( byte )rs[i]+1 );

}

rs= " ";

相关文档
最新文档