У нас есть компонент, который шифрует строки, и Node API, который расшифровывает их для других целей. Я не могу заставить их расшифровывать до правильного значения. Как я могу повторить это в Node?
Я включил код компонента ниже, просто нужно знать, какие опции передать в функции crypto.
'publics
' Use these members to save min and max salt lengths.
Public minSaltLen As Integer = -1
Public maxSaltLen As Integer = -1
Private encString As String
Private decString As String
Private EncryptionKey As String 'EncryptionKey
Private MasterEncryptionKey As String ' MasterEncryptionKey
Private DecryptUserName As String
Private DecryptPassword As String
Private saltValue As String
Private passwordIterations As Integer = 2
Private keySize As Integer = 256
Private hashAlgorithm As String
Private Shared DEFAULT_HASH_ALGORITHM As String = "SHA-1"
Private Shared DEFAULT_KEY_SIZE As Integer = 256
Private Shared MAX_ALLOWED_SALT_LEN As Integer = 255
Private Shared MIN_ALLOWED_SALT_LEN As Integer = 4
Private Shared DEFAULT_MIN_SALT_LEN As Integer = MIN_ALLOWED_SALT_LEN
Private Shared DEFAULT_MAX_SALT_LEN As Integer = 8
Private encryptor As ICryptoTransform = Nothing
Private decryptor As ICryptoTransform = Nothing
Private dc As Boolean
Public Sub New()
End Sub
Public Sub setStringToEncrypt(ByVal var As String)
encString = var
End Sub
Public Sub setEncryptionKey(ByVal var As String)
EncryptionKey = var
End Sub
Public Function EncryptData()
EncryptData = Encrypt(encString)
End Function
Public Sub setDecryptUserName(ByVal var As String)
DecryptUserName = var
End Sub
Public Sub setDecryptPassword(ByVal var As String)
DecryptPassword = var
End Sub
Public Sub setStringToDecrypt(ByVal var As String)
decString = var
End Sub
Public Function DecryptData()
If validateDecryptUser() Then
DecryptData = Decrypt(decString)
Else
DecryptData = ""
Err.Raise(1000, "Encryption.AES.DecryptData()", "Decryption Username and/or Password incorrect - decryption failed")
Exit Function
End If
End Function
Private Sub initFunction()
MasterEncryptionKey = "MASTERKEY" ' must be 16 chars
If EncryptionKey.ToString() = "" Then
Err.Raise(1002, "Encryption.AES.initFunction()", "Encryption key incorrect or empty")
Exit Sub
End If
If (minSaltLen < MIN_ALLOWED_SALT_LEN) Then
Me.minSaltLen = DEFAULT_MIN_SALT_LEN
Else
Me.minSaltLen = minSaltLen
End If
If (maxSaltLen < 0 Or maxSaltLen > MAX_ALLOWED_SALT_LEN) Then
Me.maxSaltLen = DEFAULT_MAX_SALT_LEN
Else
Me.maxSaltLen = maxSaltLen
End If
' Set the size of cryptographic key.
If (keySize <= 0) Then
keySize = DEFAULT_KEY_SIZE
End If
If (hashAlgorithm Is Nothing) Then
hashAlgorithm = DEFAULT_HASH_ALGORITHM
Else
hashAlgorithm = hashAlgorithm.ToUpper().Replace("-", "")
End If
Dim initVectorBytes() As Byte = Nothing
Dim saltValueBytes() As Byte = Nothing
' Get bytes of initialization vector.
If (MasterEncryptionKey Is Nothing) Then
initVectorBytes = New Byte() {}
Else
initVectorBytes = Encoding.ASCII.GetBytes(MasterEncryptionKey)
End If
' Get bytes of salt (used in hashing).
If (saltValue Is Nothing) Then
saltValueBytes = New Byte() {}
Else
saltValueBytes = Encoding.ASCII.GetBytes(saltValue)
End If
' Generate password, which will be used to derive the key.
Dim password As PasswordDeriveBytes = New PasswordDeriveBytes( _
EncryptionKey, _
saltValueBytes, _
hashAlgorithm, _
passwordIterations)
' Convert key to a byte array adjusting the size from bits to bytes.
On Error Resume Next
Dim keyBytes() As Byte = password.GetBytes(keySize / 8)
' Initialize Rijndael key object.
Dim symmetricKey As RijndaelManaged = New RijndaelManaged()
If (initVectorBytes.Length = 0) Then
symmetricKey.Mode = CipherMode.ECB
Else
symmetricKey.Mode = CipherMode.CBC
End If
encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
End Sub
Public Function Encrypt(ByVal plainText As String) As String
dc = False
initFunction()
Encrypt = Encrypt(Encoding.UTF8.GetBytes(plainText))
End Function
Public Function Encrypt(ByVal plainTextBytes As Byte()) As String
dc = False
initFunction()
Encrypt = Convert.ToBase64String(EncryptToBytes(plainTextBytes))
End Function
Public Function EncryptToBytes(ByVal plainText As String) As Byte()
EncryptToBytes = EncryptToBytes(Encoding.UTF8.GetBytes(plainText))
End Function
Public Function EncryptToBytes(ByVal plainTextBytes As Byte()) As Byte()
Dim plainTextBytesWithSalt() As Byte = AddSalt(plainTextBytes)
Dim memoryStream As MemoryStream = New MemoryStream()
Dim cryptoStream As CryptoStream = Nothing
' Let's make cryptographic operations thread-safe.
SyncLock Me
' To perform encryption, we must use the Write mode.
cryptoStream = New CryptoStream(memoryStream, _
encryptor, _
CryptoStreamMode.Write)
' Start encrypting data.
cryptoStream.Write(plainTextBytesWithSalt, _
0, _
plainTextBytesWithSalt.Length)
' Finish the encryption operation.
cryptoStream.FlushFinalBlock()
' Move encrypted data from memory into a byte array.
Dim cipherTextBytes() As Byte = memoryStream.ToArray()
' Close memory streams.
memoryStream.Close()
cryptoStream.Close()
' Return encrypted data.
EncryptToBytes = cipherTextBytes
End SyncLock
End Function
Public Function Decrypt(ByVal cipherText As String) As String
dc = True
initFunction()
Decrypt = Decrypt(Convert.FromBase64String(cipherText))
End Function
Public Function Decrypt(ByVal cipherTextBytes As Byte()) As String
dc = True
initFunction()
Decrypt = Encoding.UTF8.GetString(DecryptToBytes(cipherTextBytes))
End Function
Public Function DecryptToBytes(ByVal cipherText As String) As Byte()
DecryptToBytes = DecryptToBytes(Convert.FromBase64String(cipherText))
End Function
Public Function DecryptToBytes(ByVal cipherTextBytes As Byte()) As Byte()
Dim decryptedBytes() As Byte = Nothing
Dim plainTextBytes() As Byte = Nothing
Dim decryptedByteCount As Integer = 0
Dim saltLen As Integer = 0
Dim memoryStream As MemoryStream = New MemoryStream(cipherTextBytes)
decryptedBytes = New Byte(cipherTextBytes.Length - 1) {}
' Let's make cryptographic operations thread-safe.
SyncLock Me
' To perform decryption, we must use the Read mode.
Dim cryptoStream As CryptoStream = New CryptoStream( _
memoryStream, _
decryptor, _
CryptoStreamMode.Read)
' Decrypting data and get the count of plain text bytes.
decryptedByteCount = cryptoStream.Read(decryptedBytes, _
0, _
decryptedBytes.Length)
' Release memory.
memoryStream.Close()
cryptoStream.Close()
End SyncLock
' If we are using salt, get its length from the first 4 bytes of plain
' text data.
If (maxSaltLen > 0 And maxSaltLen >= minSaltLen) Then
saltLen = (decryptedBytes(0) And &H3) Or _
(decryptedBytes(1) And &HC) Or _
(decryptedBytes(2) And &H30) Or _
(decryptedBytes(3) And &HC0)
End If
' Allocate the byte array to hold the original plain text
' (without salt).
plainTextBytes = New Byte(decryptedByteCount - saltLen - 1) {}
' Copy original plain text discarding the salt value if needed.
Array.Copy(decryptedBytes, saltLen, plainTextBytes, _
0, decryptedByteCount - saltLen)
' Return original plain text value.
DecryptToBytes = plainTextBytes
End Function
Private Function AddSalt(ByVal plainTextBytes As Byte()) As Byte()
If (maxSaltLen = 0 Or maxSaltLen < minSaltLen) Then
AddSalt = plainTextBytes
Exit Function
End If
' Generate the salt.
Dim saltBytes() As Byte = GenerateSalt()
' Allocate array which will hold salt and plain text bytes.
Dim plainTextBytesWithSalt() As Byte = New Byte( _
plainTextBytes.Length + _
saltBytes.Length - 1) {}
' First, copy salt bytes.
Array.Copy(saltBytes, plainTextBytesWithSalt, saltBytes.Length)
' Append plain text bytes to the salt value.
Array.Copy(plainTextBytes, 0, _
plainTextBytesWithSalt, saltBytes.Length, _
plainTextBytes.Length)
AddSalt = plainTextBytesWithSalt
End Function
Конечный класс