關於SHA
代碼清單
代碼清單
class Program
{
static SHA
static void Main(string[] args)
{
string testString =
byte[] hashY = GetHashData(testString
string testString
byte[] hashY
string ChangedString =
byte[] hashChange = GetHashData(ChangedString
Console
}
private static byte[] GetHashData(string s
{
Console
byte[] buffer = Encoding
byte[]hashBytes= sha
OutHash(hashBytes);
return hashBytes;
}
private static void OutHash(byte[] hashBytes)
{
foreach (byte b in hashBytes)
{
Console
}
Console
Console
}
}
現在我們簡單分析代碼清單
byte[]hashBytes= sha
sha
OutHash方法用來輸出散列值
Main方法中我定義了三個字符串
圖
從圖
在實際應用中
代碼清單
class Program
{
public static void Encript(byte[] key
{
HMACSHA
FileStream inStream = new FileStream(sourceFile
FileStream outStream = new FileStream(destFile
byte[] hashValue = myhmacsha
inStream
outStream
int bytesRead;
byte[] buffer = new byte[
do
{
bytesRead = inStream
outStream
} while (bytesRead >
myhmacsha
inStream
outStream
return;
}
public static bool Decript(byte[] key
{
HMACSHA
byte[] storedHash = new byte[hmacsha
FileStream inStream = new FileStream(sourceFile
inStream
byte[] computedHash = hmacsha
for (int i =
{
if (computedHash[i] != storedHash[i])
{
Console
return false;
}
}
Console
return true;
}
public static void Main(string[] Fileargs)
{
string file
string file
try
{
byte[] secretkey = new Byte[
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng
Encript(secretkey
Decript(secretkey
}
catch (IOException e)
{
Console
}
Console
}
}
}
看代碼清單
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng
RNGCryptoServiceProvider類提供方法用來生成高強度隨機數
Encript方法用來使用HMACSHA
Decript方法用來驗證文件完整性
執行加密Hash的過程後
圖
從圖中
驗證結果如圖
圖
From:http://tw.wingwit.com/Article/program/ASP/201311/21811.html