2

加密字符串的方法大约有 100,000 种。使用 AES、CBC 和 PKCS7 等标准使事情变得更容易——但 IV 仍然存在问题。盐、编码等(如www.Crypto.Stackexchange.com上所述)

对于我的 iOS 项目 (Obj-C),我正在使用FBEncryptor 项目,这是一种简单且有据可查的加密字符串的方法。但是,我需要能够解密由 Windows 平台上的 FBEncryptor 生成的 iOS AES 字符串的代码 - 最好是 VB.NET。

我还没有找到另一个与 FBEncryptor 兼容的 VB.NET 项目。

有谁知道使用 FBEncryptor 的任何 VB.NET 加密项目?如果没有,那么我需要做什么才能使 Rijndael 托管库(由 Microsoft 提供)与 FBEncryptor 一起使用?

我使用Rijndael Managed的​​ VB.NET 代码:

Imports System.Security
Imports System.Security.Cryptography
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions
Imports System.Text


Module Encrypt2
Public saltBytes As Byte()
Dim hashAlgorithm As String = "SHA1"

Public Sub GenerateSalt()
    ' Define min and max salt sizes.
    Dim minSaltSize As Integer
    Dim maxSaltSize As Integer

    minSaltSize = 8
    maxSaltSize = 8

    ' Generate a random number for the size of the salt.
    Dim random As Random
    random = New Random()

    Dim saltSize As Integer
    saltSize = random.Next(minSaltSize, maxSaltSize)

    ' Allocate a byte array, which will hold the salt.
    saltBytes = New Byte(saltSize - 1) {}

    ' Initialize a random number generator.
    Dim rng As RNGCryptoServiceProvider
    rng = New RNGCryptoServiceProvider()

    ' Fill the salt with cryptographically strong byte values.
    rng.GetNonZeroBytes(saltBytes)
End Sub

Public Function Encrypt(ByVal plainText As String, ByVal keyword As String) As String
    Dim hashAlgorithm As String = "SHA1"
    Dim passwordIterations As Integer = 128
    Dim initVector As String = "@7781157e2629b09"
    Dim keySize As Integer = 256

    Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)

    Dim plainTextBytes As Byte() = Encoding.ASCII.GetBytes(plainText)

    GenerateSalt()

    Dim password As New Rfc2898DeriveBytes(keyword, saltBytes, passwordIterations)

    Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)
    Dim symmetricKey As New RijndaelManaged()

    symmetricKey.Mode = CipherMode.CBC

    Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)

    Dim memoryStream As New MemoryStream()
    Dim cryptoStream As New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)

    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
    cryptoStream.FlushFinalBlock()
    Dim cipherTextBytes As Byte() = memoryStream.ToArray()
    memoryStream.Close()
    cryptoStream.Clear()

    Dim cipherText As String = Convert.ToBase64String(cipherTextBytes)

    Return (cipherText)

End Function

Public Function Decrypt(ByVal cipherText As String, ByVal keyword As String) As String

    Dim hashAlgorithm As String = "SHA1"

    Dim passwordIterations As Integer = 128
    Dim initVector As String = "@7781157e2629b09"
    Dim keySize As Integer = 256

    ' Convert strings defining encryption key characteristics into Byte
    ' arrays. Let us assume that strings only contain ASCII codes.
    ' If strings include Unicode characters, use Unicode, UTF7, or UTF8
    ' encoding

    Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector)

    ' Convert the Ciphertext into a Byte array
    Dim cipherTextBytes As Byte() = Encoding.ASCII.GetBytes(cipherText)

    ' First we must create a password, from which the key will be 
    ' derived. This password will be generated from the specified
    ' passphrase and salt value. The password will be created using 
    ' the specified hash algorithm. Password creation can be done in 
    ' several iterations
    GenerateSalt()
    Dim password As New Rfc2898DeriveBytes(keyword, saltBytes, passwordIterations)

    ' Use the password to generate pseudo-random bytes for the encryption
    ' key. Specify the size of the key in bytes (instead of bits)

    Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)

    ' Create uninitialized Rijndael encryption object

    Dim symmetricKey As New RijndaelManaged()

    ' It is reasonable to set encryption mode to Cipher Block Chaining
    ' (CBC). Use default options for other symmetric key parameters.
    symmetricKey.Mode = CipherMode.CBC

    ' Generate decryptor from the existing key bytes and initialization
    ' vector. Key size will be defined based on the number of the key
    'bytes

    Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)

    ' Define memory stream which will be used to hold encrypted data.

    Dim memoryStream As New MemoryStream(cipherTextBytes)

    ' Define cryptographic stream (always user Read mode for encryption).
    Dim cryptoStream As New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)


    ' Since at this point we don't know what the size of decrypted data
    ' will be, allocate the buffer long enough to hold ciphertext;
    ' plaintext is never longer than ciphertext
    Dim plainTextBytes As Byte() = New Byte(cipherTextBytes.Length - 1) {}

    ' Start decrypting.
    Dim decryptedByteCount As Integer = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)

    ' Close both streams
    memoryStream.Close()
    cryptoStream.Close()

    ' Convert decrypted data into a string
    ' Let us assume that the original plaintext string was UTF8-Encoded.
    Dim plainText As String = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)

    ' Return the decrypted string
    Return (plainText)


End Function

End Module

您会注意到 IV 是静态的,并且密码迭代次数很少。这是因为我不确定如何以与 FBEncryptor 相同的方法生成 IV。您可能还会注意到,在尝试解密时,字节长度存在问题。

任何帮助将不胜感激!谢谢!

4

1 回答 1

2

一注。我查看了 FBEncryptor。它使用了一些不好的做法。因此,如果安全性对您很重要,我建议您多阅读一些内容,然后选择另一个进行加密的代码(或自己编写)。

FBEncryptor 进行以下加密 a) 将字符串转换为字节数组(基于 UTF8 编码) b) 将密钥转换为字节数组(基于 UTF 编码)。c) 只复制密钥的前 16 个字节 d) 创建 16 个 0x00 字节的 IV e) 使用 AES/CBC/PKCS7Padding 加密项目 a) 中的字节数组,并在 d) 中创建 IV f) 生成的加密数据转换为 Base64

并且它执行以下解密 a) 将加密字符串从 Base64 转换为字节数组 b) 将密钥转换为字节数组(基于 UTF 编码)。c) 只复制密钥的前 16 个字节 d) 创建 IV,它是 0x00 的 16 个字节 e) 使用 AES/CBC/PKCS7Padding 从项 a) 解密字节数组,IV 在 d) 中创建 f) 将加密数据从 e) 转换为使用 UTF8 编码的字符串

你应该在 VB.NET 中做同样的事情。我认为您几乎拥有所有部分-您将需要删除所有与 SHA1 和盐相关的东西,并使用相当于步骤 b) 的内容。你需要研究 PKCS7Padding for VB.NET

于 2012-08-23T22:00:53.470 回答