有没有办法在vb.net项目中实现blowfish crypt?我看了很多,但在System.Security.Cryptography
或微软关于此类加密的vb.net文档中找不到任何内容!请帮忙,如果它本身不支持,有人知道一个好的第三方图书馆吗?
答案 0 :(得分:0)
感谢 Bob Learned(TheLearnedOne)代表Reference
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Imports BlowFish.BlowfishNET
Public Class Form1
Private Sub ShowBlowfishAlgorithm()
Dim i As Integer
Dim buf As Byte() = New Byte(2) {}
Dim encData As Byte(), decData As Byte()
' set up the algorithm (set to false to go with AES for comparison purposes)
Dim alg As SymmetricAlgorithm = MakeAlgo(True)
' we encrypt and decrypt from and to a memory stream, so first we have to set up a
' source (by writing some bytes to it) and a target stream
Dim inStream As New MemoryStream()
For i = 0 To 10
inStream.WriteByte(CByte(i))
Next
inStream.Position = 0
' need to reset it for the following reading
Dim outStream As New MemoryStream()
' now we create a crypto stream, to show that our BlowfishAlgorithm plays together
' with a standard .NET framework security component
Dim encStream As New CryptoStream(outStream, alg.CreateEncryptor(), CryptoStreamMode.Write)
' write data from our input stream to the encrypted stream (which then will finally
' put it into our output stream) by using a small buffer (as it it is done usually)
While inStream.Position < inStream.Length
Dim read As Integer = inStream.Read(buf, 0, buf.Length)
encStream.Write(buf, 0, read)
End While
encStream.Close()
' show what we got for the encrypted data
encData = outStream.ToArray()
Console.WriteLine("plain : " + HexPrint(inStream.ToArray()))
Console.WriteLine("encrypted: " + HexPrint(encData))
' decrypt the encrypted data, with the an input stream now set up with the
' encrypted data and being passed to the decryption stream
outStream = New MemoryStream()
Dim decStream As New CryptoStream(New MemoryStream(encData), alg.CreateDecryptor(), CryptoStreamMode.Read)
While outStream.Position < encData.Length
Dim read As Integer = decStream.Read(buf, 0, buf.Length)
If 0 = read Then
Exit While
End If
outStream.Write(buf, 0, read)
End While
decStream.Close()
decData = outStream.ToArray()
Console.WriteLine("decrypted: " + HexPrint(decData))
For i = 0 To 10
' verify that we got the right data back by simulating and comparing the
' original input data
If decData(i) <> i Then
Console.WriteLine("decryption error!")
Exit For
End If
Next
End Sub
Private Function HexPrint(ByVal buf As Byte()) As String
Dim result As New StringBuilder(buf.Length * 3)
For i As Integer = 0 To buf.Length - 1
If i > 0 Then
result.Append(" "c)
End If
result.Append(buf(i).ToString("x2"))
Next i
Return result.ToString()
End Function
Private Function MakeAlgo(ByVal useBlowfish As Boolean) As SymmetricAlgorithm
Dim result As SymmetricAlgorithm = IIf((useBlowfish), New BlowfishAlgorithm(), SymmetricAlgorithm.Create())
result.Mode = CipherMode.CBC
If useBlowfish Then
result.KeySize = 40
End If
result.GenerateKey()
result.GenerateIV()
result.Padding = PaddingMode.PKCS7
Return result
End Function
End Class