VB.NET中的高级加密标准

时间:2015-12-17 04:42:38

标签: vb.net encryption aes

我正在上大学的最后一个项目。它是关于多媒体文件(文本和图像)的高级加密标准(AES),但我需要一些我不理解的代码的解释。

 Imports System
 Imports System.Collections.Generic
 Imports System.Text

 Imports System.IO
 Imports System.Security
 Imports System.Security.Cryptography
 Imports System.Diagnostics
 Imports System.Runtime.CompilerServices

 Imports System.Drawing.Imaging

模块CryptoStuff

Public key_size_bits_ku As Integer = 128
Public iv_ku() As Byte
Private Sub MakeKeyAndIV(ByVal password As String, ByVal salt() As Byte, ByVal key_size_bits As Integer,
                         ByVal block_size_bits As Integer, ByRef key() As Byte, ByRef iv() As Byte)
    Dim derive_bytes As New Rfc2898DeriveBytes(password, salt, 1000)
    key = Encoding.ASCII.GetBytes(password)
    iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
End Sub

地区“Enkripsi文件dan stream”

Public Function imgToByteArray(ByVal img As Image) As Byte()
    Using mStream As New MemoryStream()
        img.Save(mStream, img.RawFormat)
        Return mStream.ToArray()
    End Using
End Function

Public Sub EncryptFile(ByVal password As String, ByVal in_file As String,
                       ByVal out_file As String)
    CryptFile(password, in_file, out_file, True)
End Sub
Public Sub DecryptFile(ByVal password As String, ByVal in_file As String,
                       ByVal out_file As String)
    CryptFile(password, in_file, out_file, False)
End Sub
Public Sub CryptFile(ByVal password As String, ByVal in_file As String,
                     ByVal out_file As String, ByVal encrypt As Boolean)
        Using in_stream As New FileStream(in_file, FileMode.Open, FileAccess.Read)
        Using out_stream As New FileStream(out_file, FileMode.Create, FileAccess.Write)
             CryptStream(password, in_stream, out_stream, encrypt)
        End Using
    End Using
End Sub

Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
    Using mStream As New MemoryStream(byteArrayIn)
        Return Image.FromStream(mStream)
    End Using
End Function

Public Sub CryptStream(ByVal password As String, ByVal in_stream As Stream,
                       ByVal out_stream As Stream, ByVal encrypt As Boolean)        
    Dim aes_provider As New AesCryptoServiceProvider()

    Dim key_size_bits As Integer = 0
    For i As Integer = 1024 To 1 Step -1
        If (aes_provider.ValidKeySize(i)) Then
            key_size_bits = i
            Exit For
        End If
    Next i
    Debug.Assert(key_size_bits > 0)
    Console.WriteLine("Key size: " & key_size_bits)

    Dim block_size_bits As Integer = aes_provider.BlockSize

    Dim key() As Byte = Nothing
    Dim iv() As Byte = Nothing
    'Dim salt() As Byte = {&H0, &H0, &H1, &H2, &H3, &H4, &H5, &H6, &HF1, &HF0, &HEE, &H21, &H22, &H45}
    'MakeKeyAndIV(password, salt, key_size_bits_ku, block_size_bits, key, iv)
    key = Encoding.ASCII.GetBytes(password)
    iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

     Dim crypto_transform As ICryptoTransform
    If (encrypt) Then
        crypto_transform = aes_provider.CreateEncryptor(key, iv)
    Else
        crypto_transform = aes_provider.CreateDecryptor(key, iv)
    End If


    Try
        Using crypto_stream As New CryptoStream(out_stream, crypto_transform, CryptoStreamMode.Write)

            Const block_size As Integer = 1024
            Dim buffer(block_size) As Byte
            Dim bytes_read As Integer
            Do

                bytes_read = in_stream.Read(buffer, 0, block_size)
                If (bytes_read = 0) Then Exit Do


                crypto_stream.Write(buffer, 0, bytes_read)
            Loop
        End Using
    Catch
    End Try

    crypto_transform.Dispose()
End Sub

结束区域'加密文件和流“

结束模块

我不明白的代码是:

     Dim key_size_bits As Integer = 0
     For i As Integer = 1024 To 1 Step -1
        If (aes_provider.ValidKeySize(i)) Then
            key_size_bits = i
            Exit For
        End If
     Next i
     Debug.Assert(key_size_bits > 0)
     Console.WriteLine("Key size: " & key_size_bits)

     Dim block_size_bits As Integer = aes_provider.BlockSize

你能解释这些代码的使用吗? 先谢谢

0 个答案:

没有答案