一年前為了練習密碼學時寫的。
開發環境:Window XP
開發工具:Visual Stutio 2005
開發語言:VB.NET
開發時間:95年8月
源始檔下載
執行畫面
程式說明
主要是使用密碼編譯成Base64字串,及將Base64字串解碼成明文
.NET的密碼類的類別都在System.Security.Cryptography命名空間中
Rijndael 為對稱加解密演算法
MD5,SHA1 為雜湊演算法
Class Rijndael金鑰組
Protected Friend 金鑰 As Byte()
Protected Friend 向量 As Byte()
End Class
Class Rijndael
Shared Function 建立金鑰組() As Rijndael金鑰組
Dim rij As New RijndaelManaged
Dim 金鑰組 As New Rijndael金鑰組
rij.GenerateKey()
rij.GenerateIV()
金鑰組.金鑰 = rij.Key
金鑰組.向量 = rij.IV
Return 金鑰組
End Function
Shared Function 加密(ByRef 原文 As String, ByRef 金鑰組 As Rijndael金鑰組) As String
Dim buffer() As Byte = Unicode.GetBytes(原文)
Dim rij As New RijndaelManaged
rij.Key = 金鑰組.金鑰
rij.IV = 金鑰組.向量
Return System.Convert.ToBase64String(rij.CreateEncryptor.TransformFinalBlock(buffer, 0, buffer.Length))
End Function
Shared Function 解密(ByRef 原文 As String, ByRef 金鑰組 As Rijndael金鑰組) As String
Dim buffer() As Byte = System.Convert.FromBase64String(原文)
Dim rij As New RijndaelManaged
rij.Key = 金鑰組.金鑰
rij.IV = 金鑰組.向量
Return Unicode.GetString(rij.CreateDecryptor.TransformFinalBlock(buffer, 0, buffer.Length))
End Function
End Class
Class SHA1
Shared Function 雜湊(ByRef 雜湊碼 As String, ByRef 原文 As String) As String
Dim buffer() As Byte = Unicode.GetBytes(原文)
If String.IsNullOrEmpty(雜湊碼) Then
Return System.Convert.ToBase64String(New SHA1CryptoServiceProvider().ComputeHash(buffer))
Else
Dim key() As Byte = Unicode.GetBytes(雜湊碼)
Return System.Convert.ToBase64String(New HMACSHA1(key).ComputeHash(buffer))
End If
End Function
End Class
Class MD5
Shared Function 雜湊(ByRef 雜湊碼 As String, ByRef 原文 As String) As String
Dim buffer() As Byte = Unicode.GetBytes(原文)
If String.IsNullOrEmpty(雜湊碼) Then
Return System.Convert.ToBase64String(New MD5CryptoServiceProvider().ComputeHash(buffer))
Else
Dim key() As Byte = Unicode.GetBytes(雜湊碼)
Return System.Convert.ToBase64String(New HMACMD5(key).ComputeHash(buffer))
End If
End Function
End Class
Protected Friend 金鑰 As Byte()
Protected Friend 向量 As Byte()
End Class
Class Rijndael
Shared Function 建立金鑰組() As Rijndael金鑰組
Dim rij As New RijndaelManaged
Dim 金鑰組 As New Rijndael金鑰組
rij.GenerateKey()
rij.GenerateIV()
金鑰組.金鑰 = rij.Key
金鑰組.向量 = rij.IV
Return 金鑰組
End Function
Shared Function 加密(ByRef 原文 As String, ByRef 金鑰組 As Rijndael金鑰組) As String
Dim buffer() As Byte = Unicode.GetBytes(原文)
Dim rij As New RijndaelManaged
rij.Key = 金鑰組.金鑰
rij.IV = 金鑰組.向量
Return System.Convert.ToBase64String(rij.CreateEncryptor.TransformFinalBlock(buffer, 0, buffer.Length))
End Function
Shared Function 解密(ByRef 原文 As String, ByRef 金鑰組 As Rijndael金鑰組) As String
Dim buffer() As Byte = System.Convert.FromBase64String(原文)
Dim rij As New RijndaelManaged
rij.Key = 金鑰組.金鑰
rij.IV = 金鑰組.向量
Return Unicode.GetString(rij.CreateDecryptor.TransformFinalBlock(buffer, 0, buffer.Length))
End Function
End Class
Class SHA1
Shared Function 雜湊(ByRef 雜湊碼 As String, ByRef 原文 As String) As String
Dim buffer() As Byte = Unicode.GetBytes(原文)
If String.IsNullOrEmpty(雜湊碼) Then
Return System.Convert.ToBase64String(New SHA1CryptoServiceProvider().ComputeHash(buffer))
Else
Dim key() As Byte = Unicode.GetBytes(雜湊碼)
Return System.Convert.ToBase64String(New HMACSHA1(key).ComputeHash(buffer))
End If
End Function
End Class
Class MD5
Shared Function 雜湊(ByRef 雜湊碼 As String, ByRef 原文 As String) As String
Dim buffer() As Byte = Unicode.GetBytes(原文)
If String.IsNullOrEmpty(雜湊碼) Then
Return System.Convert.ToBase64String(New MD5CryptoServiceProvider().ComputeHash(buffer))
Else
Dim key() As Byte = Unicode.GetBytes(雜湊碼)
Return System.Convert.ToBase64String(New HMACMD5(key).ComputeHash(buffer))
End If
End Function
End Class