0

其他线程已经涵盖了 W7HE 不支持 vb 上的 file.encrypt 。但是我找不到一个简单的解决方法,我知道的唯一其他加密功能是使用:

Imports system.security.Cryptography

然后按照

Dim rsa As New RSACryptoServiceProvider
Dim encoder As New UTF8Encoding

等等等等

但是,这只适用于字符串,我需要将文件作为一个整体进行加密。任何想法将不胜感激

4

2 回答 2

0

file.encrypt 使用 EFS,实际上是透明的加密; 用户帐户存储主证书,因此使用该帐户登录的任何人都可以访问任何应用程序中的文件内容。根据您的需要,这可能根本不是您想要的。如果您想单独为您的应用程序复制 EFS 行为,只需将主密码存储在用户的私有文件中的某个位置,将您的数据文件拆分为“密钥”和“数据”部分,然后使用主密码加密密钥。(Real EFS 有一些微妙之处:它可以为许多不同的用户分别加密密码,因此它允许不同的用户访问每个文件,在主用户密码丢失的情况下提供恢复选项。它还使用用户的密码来加密主密码,因此如果有人更改密码,他们将无法打开文件。)

基本方法只是提示用户输入密码并使用它一次加密或解密整个文件,而不是一次一个字符串。序列化你的文件,但是你会不加密,然后将整个字节数组传递给类似于EncryptStringToBytes_Aes的东西。如果你写的是字节而不是字符串,唯一的区别是改变:

using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
    {
        swEncrypt.Write(plainText);
    }

csEncrypt.Write(plainText);

无论哪种方式,对于您将使用的实际加密AesCryptoServiceProvider,而不是RSACryptoServiceProvider用于传递给 AES 和类似算法的密钥/密码的顶级身份验证和加密。它很少用于加密整个文件,而且当你这样做时速度非常慢。

于 2012-09-09T04:14:31.500 回答
0

我附上了我写的一个程序来帮助你。它非常有效,可以加密任何你想要的东西。我在里面留下了笔记来回答你所有的问题。在底部,您将看到一个添加的类以减少程序的内存使用量。它工作得相当好,(8%-10%)享受。所需物品:*3 个按钮(浏览)(加密)(解密)*1 个文本框项目视频见我的链接:

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

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

'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://docs.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
于 2022-02-27T00:06:37.917 回答