首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VB.NET中的高级加密标准

VB.NET中的高级加密标准
EN

Stack Overflow用户
提问于 2015-12-17 04:42:37
回答 1查看 141关注 0票数 1

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

代码语言:javascript
运行
复制
 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

代码语言:javascript
运行
复制
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文件丹流“

代码语言:javascript
运行
复制
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

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

终端模块

我不明白的密码是:

代码语言:javascript
运行
复制
     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

你能解释一下这些密码的用法吗?谢谢你之前

EN

回答 1

Stack Overflow用户

发布于 2022-02-27 00:10:38

我编写的这个应用程序非常有效,可以对您想要的任何内容进行加密。我在里面留了笔记回答你所有的问题。在底部,您将看到一个额外的类,以减少程序的内存使用。它运行得相当好,( 8% -10%)享受。所需项目:*3个按钮(浏览)(加密)(解密) *1 TextBox项目视频,请参阅我的链接:

第一部分:https://www.youtube.com/watch?v=sVaA2q8ttzQ

第2部分:https://www.youtube.com/watch?v=TyafeBJ53YU

代码语言:javascript
运行
复制
'Created by: Rythorian77 | github.com/rythorian77
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Public Class Form1


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ramClass As New AESRam()
    End Sub

    Private Sub Browse_Click(sender As Object, e As EventArgs) Handles Browse.Click

        'Allows you to access files from folders
        Dim fetchCryptDialog As New OpenFileDialog With {
            .CheckFileExists = True,
            .InitialDirectory = "C:\",
        .Multiselect = False
        }

        If fetchCryptDialog.ShowDialog = DialogResult.OK Then
            TextBox1.Text = fetchCryptDialog.FileName
        End If

    End Sub

    Private Sub Encrypt_Click(sender As Object, e As EventArgs) Handles Encrypt.Click

        'File path goes to textbox
        Dim Rythorian77 As String = TextBox1.Text

        'This password can be whater you want.
        Dim password As String = "123456789ABCDEFG!@#$%^&*()_+"

        'A data type is the characteristic of a variable that determines what kind of data it can hold.
        'Data types include those in the following table as well as user-defined types and specific types of objects.
        Dim key As Byte() = New Byte(31) {}

        'When overridden in a derived class, encodes a set of characters into a sequence of bytes.
        Encoding.Default.GetBytes(password).CopyTo(key, 0)

        'RijndaelManaged still works but is considered obsolete in todays world so we use AES
        'Represents the abstract base class from which all implementations of the Advanced Encryption Standard (AES) must inherit.
        'https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=net-6.0
        Dim aes As New RijndaelManaged() With
            {
                .Mode = CipherMode.CBC,
                .KeySize = 256,
                .BlockSize = 256,
                .Padding = PaddingMode.Zeros
            }

        'Reads a sequence of bytes from the current memory stream and advances the position within the memory stream by the number of bytes read. 
        Using mnemonicData As New MemoryStream

            'Defines a stream that links data streams to cryptographic transformations.
            Using cStream As New CryptoStream(mnemonicData, aes.CreateEncryptor(key, key), CryptoStreamMode.Write)
                Dim buffer As Byte() = File.ReadAllBytes(Rythorian77)
                cStream.Write(buffer, 0, buffer.Length)
                Dim appendBuffer As Byte() = mnemonicData.ToArray()
                Dim finalBuffer As Byte() = New Byte(appendBuffer.Length - 1) {}
                appendBuffer.CopyTo(finalBuffer, 0)
                File.WriteAllBytes(Rythorian77, finalBuffer)

            End Using
        End Using

    End Sub
 'The above code notes compliment the same
    Private Sub Decrypt_Click(sender As Object, e As EventArgs) Handles Decrypt.Click
        Dim Rythorian77 As String = TextBox1.Text

        Dim password As String = "123456789ABCDEFG!@#$%^&*()_+"

        Dim key As Byte() = New Byte(31) {}

        Encoding.Default.GetBytes(password).CopyTo(key, 0)

        Dim aes As New RijndaelManaged() With
            {
                .Mode = CipherMode.CBC,
                .KeySize = 256,
                .BlockSize = 256,
                .Padding = PaddingMode.Zeros
            }

        Using mnemonicData As New MemoryStream
            '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>    < aes.CreateDecryptor > is the only change from above aes.CreateEncryptor
            Using cStream As New CryptoStream(mnemonicData, aes.CreateDecryptor(key, key), CryptoStreamMode.Write)
                Dim buffer As Byte() = File.ReadAllBytes(Rythorian77)
                cStream.Write(buffer, 0, buffer.Length)
                Dim appendBuffer As Byte() = mnemonicData.ToArray()
                Dim finalBuffer As Byte() = New Byte(appendBuffer.Length - 1) {}
                appendBuffer.CopyTo(finalBuffer, 0)
                File.WriteAllBytes(Rythorian77, finalBuffer)

            End Using
        End Using

    End Sub

End Class



**Add this separate Class:**

Imports System.Runtime.InteropServices

Public Class AESRam

    'This controls the amount of RAM that your process uses, it doesn't otherwise have any affect on the virtual memory size of your process. 
    'Sets the minimum and maximum working set sizes for the specified process.
    'This will cut memory usage in half.
    <DllImport("KERNEL32.DLL", EntryPoint:="SetProcessWorkingSetSize", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)>
    Friend Shared Function SetProcessWorkingSetSize(pProcess As IntPtr, dwMinimumWorkingSetSize As Integer, dwMaximumWorkingSetSize As Integer) As Boolean
    End Function

    'Retrieves a pseudo handle for the current process.
    <DllImport("KERNEL32.DLL", EntryPoint:="GetCurrentProcess", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)>
    Friend Shared Function GetCurrentProcess() As IntPtr
    End Function

    'See Above
    Public Sub New()
        Dim pHandle As IntPtr = GetCurrentProcess()
        SetProcessWorkingSetSize(pHandle, -1, -1)
    End Sub

End Class
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34326755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档