Java Rijndael解密方法的VB.Net等价物

Java Rijndael解密方法的VB.Net等价物,java,vb.net,rijndael,Java,Vb.net,Rijndael,我有一个工作的java解密函数,如下所示。RijndaelAlgorithm的java源代码是您在互联网上看到的标准代码,例如: 我正在尝试使用VB.Net复制此函数。我知道.Net支持Rijndael,但我还没有找到一个组合可以产生与下面的java代码相同的解密密码。看起来这是一个很简单的任务,但我已经花了好几天的时间研究这个问题 有人能告诉我或指导我VB.Net中的示例代码,这些代码将生成一个与java函数等价的函数吗 以下是我的论点: t = f8d44...cf22f8a 32

我有一个工作的java解密函数,如下所示。RijndaelAlgorithm的java源代码是您在互联网上看到的标准代码,例如:

我正在尝试使用VB.Net复制此函数。我知道.Net支持Rijndael,但我还没有找到一个组合可以产生与下面的java代码相同的解密密码。看起来这是一个很简单的任务,但我已经花了好几天的时间研究这个问题

有人能告诉我或指导我VB.Net中的示例代码,这些代码将生成一个与java函数等价的函数吗

以下是我的论点:

t = f8d44...cf22f8a     32 total characters 
keyArray = [113,64,51,102,120...98,98,108,115]  32 total bytes
keySize = 32
blockSize = 16
以下是我的java函数:

public static String decrypt(String t, byte[] keyArray, int keySize, int blockSize)  
{
        StringBuffer plain = new StringBuffer(t.length());
        byte[] textBytes = RijndaelAlgorithm.getBytes(t);
        int cipherLen = textBytes.length;

        Object key = RijndaelAlgorithm.makeKey(setKeyLength(keyArray, keySize), keySize);

        for (int offset = 0; offset < cipherLen; offset += blockSize)
        {
              byte[] plainBytes = RijndaelAlgorithm.blockDecrypt(textBytes, offset, key, blockSize);

              plain.append(new String(plainBytes));
        }
        return plain.toString();
}
公共静态字符串解密(字符串t,字节[]keyArray,int keySize,int blockSize)
{
StringBuffer平原=新的StringBuffer(t.length());
byte[]textBytes=RijndaelAlgorithm.getBytes(t);
int cipherLen=textBytes.length;
objectkey=rijndaelgorithm.makeKey(setKeyLength(keyArray,keySize),keySize);
for(int offset=0;offset
您可以从这里的源代码中得到一些提示:

我在VB.NET中使用这两个函数,希望这对您有所帮助

这就是我使用这些函数的方式

_ConnectionString = RijndaelSimple.Decrypt(_ConnectionString, RijndaelPhrase, CurrentSession.ApplicationSession.Application.ApplicationGuid.ToString.ToLower, "SHA1", 2, RijndaelVector, 128)





Imports System.Text.RegularExpressions
Imports System.Data.SqlClient
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

' <summary>
' This class uses a symmetric key algorithm (Rijndael/AES) to encrypt and 
' decrypt data. As long as encryption and decryption routines use the same 
' parameters to generate the keys, the keys are guaranteed to be the same.
' The class uses static functions with duplicate code to make it easier to 
' demonstrate encryption and decryption logic. In a real-life application, 
' this may not be the most efficient way of handling encryption, so - as 
' soon as you feel comfortable with it - you may want to redesign this class.
' </summary>

Public Class RijndaelSimple

' <summary>
' Encrypts specified plaintext using Rijndael symmetric key algorithm
' and returns a base64-encoded result.
' </summary>
' <param name="plainText">
' Plaintext value to be encrypted.
' </param>
' <param name="passPhrase">
' Passphrase from which a pseudo-random password will be derived. The 
' derived password will be used to generate the encryption key. 
' Passphrase can be any string. In this example we assume that this 
' passphrase is an ASCII string.
' </param>
' <param name="saltValue">
' Salt value used along with passphrase to generate password. Salt can 
' be any string. In this example we assume that salt is an ASCII string.
' </param>
' <param name="hashAlgorithm">
' Hash algorithm used to generate password. Allowed values are: "MD5" and
' "SHA1". SHA1 hashes are a bit slower, but more secure than MD5 hashes.
' </param>
' <param name="passwordIterations">
' Number of iterations used to generate password. One or two iterations
' should be enough.
' </param>
' <param name="initVector">
' Initialization vector (or IV). This value is required to encrypt the 
' first block of plaintext data. For RijndaelManaged class IV must be 
' exactly 16 ASCII characters long.
' </param>
' <param name="keySize">
' Size of encryption key in bits. Allowed values are: 128, 192, and 256. 
' Longer keys are more secure than shorter keys.
' </param>
' <returns>
' Encrypted value formatted as a base64-encoded string.
' </returns>
Public Shared Function Encrypt(ByVal plainText As String, _
                               ByVal passPhrase As String, _
                               ByVal saltValue As String, _
                               ByVal hashAlgorithm As String, _
                               ByVal passwordIterations As Integer, _
                               ByVal initVector As String, _
                               ByVal keySize As Integer) _
                       As String

    ' Convert strings 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()
    initVectorBytes = Encoding.ASCII.GetBytes(initVector)

    Dim saltValueBytes As Byte()
    saltValueBytes = Encoding.ASCII.GetBytes(saltValue)

    ' Convert our plaintext into a byte array.
    ' Let us assume that plaintext contains UTF8-encoded characters.
    Dim plainTextBytes As Byte()
    plainTextBytes = Encoding.UTF8.GetBytes(plainText)

    ' 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.
    Dim password As PasswordDeriveBytes
    password = New PasswordDeriveBytes(passPhrase, _
                                       saltValueBytes, _
                                       hashAlgorithm, _
                                       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()
    keyBytes = password.GetBytes(keySize / 8)

    'Dim keyBytes2 As Byte()
    'keyBytes2 = password.Rfc2898DeriveBytes

    ' Create uninitialized Rijndael encryption object.
    Dim symmetricKey As RijndaelManaged
    symmetricKey = 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 encryptor from the existing key bytes and initialization 
    ' vector. Key size will be defined based on the number of the key 
    ' bytes.
    Dim encryptor As ICryptoTransform
    encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)

    ' Define memory stream which will be used to hold encrypted data.
    Dim memoryStream As MemoryStream
    memoryStream = New MemoryStream()

    ' Define cryptographic stream (always use Write mode for encryption).
    Dim cryptoStream As CryptoStream
    cryptoStream = New CryptoStream(memoryStream, _
                                    encryptor, _
                                    CryptoStreamMode.Write)
    ' Start encrypting.
    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)

    ' Finish encrypting.
    cryptoStream.FlushFinalBlock()

    ' Convert our encrypted data from a memory stream into a byte array.
    Dim cipherTextBytes As Byte()
    cipherTextBytes = memoryStream.ToArray()

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

    ' Convert encrypted data into a base64-encoded string.
    Dim cipherText As String
    cipherText = Convert.ToBase64String(cipherTextBytes)

    ' Return encrypted string.
    Encrypt = cipherText
End Function

' <summary>
' Decrypts specified ciphertext using Rijndael symmetric key algorithm.
' </summary>
' <param name="cipherText">
' Base64-formatted ciphertext value.
' </param>
' <param name="passPhrase">
' Passphrase from which a pseudo-random password will be derived. The 
' derived password will be used to generate the encryption key. 
' Passphrase can be any string. In this example we assume that this 
' passphrase is an ASCII string.
' </param>
' <param name="saltValue">
' Salt value used along with passphrase to generate password. Salt can 
' be any string. In this example we assume that salt is an ASCII string.
' </param>
' <param name="hashAlgorithm">
' Hash algorithm used to generate password. Allowed values are: "MD5" and
' "SHA1". SHA1 hashes are a bit slower, but more secure than MD5 hashes.
' </param>
' <param name="passwordIterations">
' Number of iterations used to generate password. One or two iterations
' should be enough.
' </param>
' <param name="initVector">
' Initialization vector (or IV). This value is required to encrypt the 
' first block of plaintext data. For RijndaelManaged class IV must be 
' exactly 16 ASCII characters long.
' </param>
' <param name="keySize">
' Size of encryption key in bits. Allowed values are: 128, 192, and 256. 
' Longer keys are more secure than shorter keys.
' </param>
' <returns>
' Decrypted string value.
' </returns>
' <remarks>
' Most of the logic in this function is similar to the Encrypt 
' logic. In order for decryption to work, all parameters of this function
' - except cipherText value - must match the corresponding parameters of 
' the Encrypt function which was called to generate the 
' ciphertext.
' </remarks>
Public Shared Function Decrypt(ByVal cipherText As String, _
                               ByVal passPhrase As String, _
                               ByVal saltValue As String, _
                               ByVal hashAlgorithm As String, _
                               ByVal passwordIterations As Integer, _
                               ByVal initVector As String, _
                               ByVal keySize As Integer) _
                       As String

    ' 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()
    initVectorBytes = Encoding.ASCII.GetBytes(initVector)

    Dim saltValueBytes As Byte()
    saltValueBytes = Encoding.ASCII.GetBytes(saltValue)

    ' Convert our ciphertext into a byte array.
    Dim cipherTextBytes As Byte()
    cipherTextBytes = Convert.FromBase64String(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.
    Dim password As PasswordDeriveBytes
    password = New PasswordDeriveBytes(passPhrase, _
                                       saltValueBytes, _
                                       hashAlgorithm, _
                                       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()
    keyBytes = password.GetBytes(keySize / 8)

    ' Create uninitialized Rijndael encryption object.
    Dim symmetricKey As RijndaelManaged
    symmetricKey = 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
    decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)

    ' Define memory stream which will be used to hold encrypted data.
    Dim memoryStream As MemoryStream
    memoryStream = New MemoryStream(cipherTextBytes)

    ' Define memory stream which will be used to hold encrypted data.
    Dim cryptoStream As CryptoStream
    cryptoStream = 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()
    ReDim plainTextBytes(cipherTextBytes.Length)

    ' Start decrypting.
    Dim decryptedByteCount As Integer
    decryptedByteCount = 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
    plainText = Encoding.UTF8.GetString(plainTextBytes, _
                                        0, _
                                        decryptedByteCount)

    ' Return decrypted string.
    Decrypt = plainText
End Function
\u ConnectionString=RijndaelSimple.Decrypt(\u ConnectionString,RijndaelPhrase,CurrentSession.Application.Application.ApplicationGuid.ToString.ToLower,“SHA1”,2,RijndaelVector,128)
导入System.Text.RegularExpressions
导入System.Data.SqlClient
导入System.Security.Cryptography
导入系统文本
导入System.IO
' 
'此类使用对称密钥算法(Rijndael/AES)加密和
'解密数据。只要加密和解密例程使用相同的
'参数生成密钥时,保证密钥相同。
'该类使用带有重复代码的静态函数,以便于
'演示加密和解密逻辑。在实际应用中,
这可能不是处理加密的最有效方式,因此
“一旦你对它感到满意,你可能会想重新设计这个类。
' 
公共类RijndaelSimple
' 
'使用Rijndael对称密钥算法加密指定的明文
'并返回base64编码的结果。
' 
' 
'要加密的纯文本值。
' 
' 
'将从中派生伪随机密码的密码短语。这个
'派生密码将用于生成加密密钥。
'密码短语可以是任何字符串。在本例中,我们假设
'密码短语是ASCII字符串。
' 
' 
'Salt值与密码短语一起用于生成密码。盐罐
“任何字符串都可以。在本例中,我们假设salt是一个ASCII字符串。
' 
' 
'用于生成密码的哈希算法。允许的值为:“MD5”和
"SHA1"。SHA1哈希有点慢,但比MD5哈希更安全。
' 
' 
'用于生成密码的迭代次数。一次或两次迭代
“应该够了。
' 
' 
'初始化向量(或IV)。需要此值来加密
'第一块纯文本数据。对于RijndaelManaged,四级必须
'正好16个ASCII字符长。
' 
' 
'加密密钥的大小(以位为单位)。允许的值为:128、192和256。
'长密钥比短密钥更安全。
' 
' 
'格式化为base64编码字符串的加密值。
' 
公共共享函数加密(ByVal明文作为字符串_
ByVal密码短语作为字符串_
ByVal saltValue作为字符串_
ByVal哈希算法作为字符串_
ByVal passwordIterations作为整数_
ByVal initVector作为字符串_
ByVal键设置为整数)_
作为字符串
'将字符串转换为字节数组。
'让我们假设字符串只包含ASCII码。
'如果字符串包含Unicode字符,请使用Unicode、UTF7或UTF8
'编码。
Dim initVectorBytes作为字节()
initVectorBytes=Encoding.ASCII.GetBytes(initVector)
Dim saltValueBytes作为字节()
saltValueBytes=Encoding.ASCII.GetBytes(saltValue)
'将明文转换为字节数组。
'我们假设纯文本包含UTF8编码字符。
Dim plainTextBytes作为字节()
明文字节=Encoding.UTF8.GetBytes(明文)
'首先,我们必须创建一个密码,从中派生密钥。
'此密码将根据指定的密码短语生成,并且
“盐的价值。将使用指定的哈希创建密码
'算法。密码创建可以在多次迭代中完成。
将密码设置为PasswordDeriveBytes
password=新的PasswordDeriveBytes(密码短语_
saltValueBytes_
哈希算法_
密码(迭代)
'使用密码为加密生成伪随机字节
“钥匙。以字节(而不是位)为单位指定密钥的大小。
Dim keyBytes作为字节()
keyBytes=password.GetBytes(keySize/8)
'Dim keyBytes2作为字节()
'keyBytes2=password.Rfc2898DeriveBytes
'创建未初始化的Rijndael加密对象。
RijndaelManaged的Dim symmetricKey
symmetricKey=New RijndaelManaged()
'将加密模式设置为密码块链接是合理的
"(CBC)。对其他对称密钥参数使用默认选项。
symmetricKey.Mode=CipherMode.CBC
'从现有密钥字节和初始化生成加密程序
“矢量。密钥大小将根据密钥的数量定义
'字节。
作为ICryptoTransform的Dim加密机
加密机