下面的類實現了文件的加密和解密操作,試驗了幾種文件類型均沒有問題,現在和大家共享一下。
namespace mycryptohelp
{
/// <summary>
/// 異常處理類
/// </summary>
public class cryptohelpexception : applicationexception
{
public cryptohelpexception(string msg):base(msg){}
}
/// <summary>
/// crypthelp
/// </summary>
public class cryptohelp
{
private const ulong fc_tag = 0xfc010203040506cf;
private const int buffer_size = 128*1024;
/// <summary>
/// 檢驗兩個byte數組是否相同
/// </summary>
/// <param name="b1">byte數組</param>
/// <param name="b2">byte數組</param>
/// <returns>true-相等</returns>
private static bool checkbytearrays(byte[] b1, byte[] b2)
{
if(b1.length == b2.length)
{
for(int i = 0; i < b1.length; ++i)
{
if(b1[i] != b2[i])
return false;
}
return true;
}
return false;
}
/// <summary>
/// 創建rijndael symmetricalgorithm
/// </summary>
/// <param name="password">密碼</param>
/// <param name="salt"></param>
/// <returns>加密對象</returns>
private static symmetricalgorithm createrijndael(string password, byte[] salt)
{
passwordderivebytes pdb = new passwordderivebytes(password,salt,"sha256",1000);
symmetricalgorithm sma = rijndael.create();
sma.keysize = 256;
sma.key = pdb.getbytes(32);
sma.padding = paddingmode.pkcs7;
return sma;
}
/// <summary>
/// 加密文件隨機數生成
/// </summary>
private static randomnumbergenerator rand = new rngcryptoserviceprovider();
/// <summary>
/// 生成指定長度的隨機byte數組
/// </summary>
/// <param name="count">byte數組長度</param>
/// <returns>隨機byte數組</returns>
private static byte[] generaterandombytes(int count)
{
byte[] bytes = new byte[count];
rand.getbytes(bytes);
return bytes;
}
/// <summary>
/// 加密文件
/// </summary>
/// <param name="infile">待加密文件</param>
/// <param name="outfile">加密后輸入文件</param>
/// <param name="password">加密密碼</param>
public static void encryptfile(string infile, string outfile, string password)
{
using(filestream fin = file.openread(infile),
fout = file.openwrite(outfile))
{
long lsize = fin.length; // 輸入文件長度
int size = (int)lsize;
byte[] bytes = new byte[buffer_size]; // 緩存
int read = -1; // 輸入文件讀取數量
int value = 0;
// 獲取iv和salt
byte[] iv = generaterandombytes(16);
byte[] salt = generaterandombytes(16);
// 創建加密對象
symmetricalgorithm sma = cryptohelp.createrijndael(password, salt);
sma.iv = iv;
// 在輸出文件開始部分寫入iv和salt
fout.write(iv,0,iv.length);
fout.write(salt,0,salt.length);
// 創建散列加密
hashalgorithm hasher = sha256.create();
using(cryptostream cout = new cryptostream(fout,sma.createencryptor(),cryptostreammode.write),
chash = new cryptostream(stream.null,hasher,cryptostreammode.write))
{
binarywriter bw = new binarywriter(cout);
bw.write(lsize);
bw.write(fc_tag);
// 讀寫字節塊到加密流緩沖區
while( (read = fin.read(bytes,0,bytes.length)) != 0 )
{
cout.write(bytes,0,read);
chash.write(bytes,0,read);
value += read;
}
// 關閉加密流
chash.flush();
chash.close();
// 讀取散列
byte[] hash = hasher.hash;
// 輸入文件寫入散列
cout.write(hash,0,hash.length);
// 關閉文件流
cout.flush();
cout.close();
}
}
}
新聞熱點
疑難解答
圖片精選