C 檔案加密解密 SHA256

2021-06-10 03:05:11 字數 1747 閱讀 7662

/// /// 加密檔案隨機數生成

///

private static randomnumbergenerator rand = new rngcryptoserviceprovider();

private const int buffer_size = 128 * 1024;

private const ulong fc_tag = 0xfc010203040506cf;

/// /// 生成指定長度的隨機byte陣列

///

/// byte陣列長度

/// 隨機byte陣列

private static byte generaterandombytes(int count)

/// /// 建立rijndael symmetricalgorithm

///

/// 密碼

///

/// 加密物件

private static symmetricalgorithm createrijndael(string password, byte salt)

public static void encryptfile(string infile, string outfile, string password)

// 關閉加密流

chash.flush();

chash.close();

// 讀取雜湊

//在 chash 獲取雜湊金鑰

byte hash = hasher.hash;

// 輸入檔案寫入雜湊

cout.write(hash, 0, hash.length);

// 關閉檔案流

cout.flush();

cout.close();}}

}/// /// 解密檔案

///

/// 待解密檔案

/// 解密後輸出檔案

/// 解密密碼

public static void decryptfile(string infile, string outfile, string password)

if (slack > 0)

chash.flush();

chash.close();

fout.flush();

fout.close();

byte curhash = hasher.hash;

// 獲取比較和舊的雜湊物件

byte oldhash = new byte[hasher.hashsize / 8];

read = cin.read(oldhash, 0, oldhash.length);

if ((oldhash.length != read) || (!checkbytearrays(oldhash, curhash)))

throw new exception("檔案被破壞");

}if (outvalue != lsize)

throw new exception("檔案大小不匹配");}}

/// /// 檢驗兩個byte陣列是否相同

///

/// byte陣列

/// byte陣列

/// true-相等

private static bool checkbytearrays(byte b1, byte b2)

return true;

}return false;

}

MD5加密 SHA256加密 RSA加密

rsa加密 md5加密 1 md5加密較為可靠,需要引入commons codec 1.9.jar 2 md5加密不可逆,也就是加密後不能再根據加密的秘鑰去解密,網上md5解密實際上是暴力破解。如果需要可逆的話,請選擇des aes 異或 凱撒 rsa等。3 md5加密可以實現任何物件加密,這就決定...

SHA 256演算法流程

sha 256 輸入訊息 m 輸出訊息 256位 bit hash值 步驟 訊息填充 m的長度 mod 512 r,考慮r r為輸入訊息長度按512bit進行分組後,最後一組的長度 系統給出8個32位暫存器 a,b,c,d,e,f,g,h,其初始值分別取自8個素數 2,3,5,7,11,13,17,...

SHA 256演算法實現過程

1.定義8個32位常量 h0 0x6a09e667 h1 0xbb67ae85 h2 0x3c6ef372 h3 0xa54ff53a h4 0x510e527f h5 0x9b05688c h6 0x1f83d9ab h7 0x5be0cd192.再定義乙個k的32位整形陣列,陣列大小為64 k ...