Cómo descifrar Rijndael en ruby (codificado en VB.net)
Estoy usando Rinjael para codificar en VB.NET y necesito decodificar en Ruby. Mi clase de encriptación VB.NET se ve así:
Private Class Encryptor
Private symmetricKey As System.Security.Cryptography.RijndaelManaged
Private iVector As Byte()
Private Key As Byte()
Public Function encrypt(ByVal data As String) As String
Try
Dim plainTextBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
Dim encryptor As System.Security.Cryptography.ICryptoTransform = symmetricKey.CreateEncryptor(Key, iVector)
Dim memoryStream As New System.IO.MemoryStream
Dim cryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(memoryStream, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
cryptoStream.FlushFinalBlock()
Dim cipherTextBytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
cryptoStream.Close()
Return Convert.ToBase64String(cipherTextBytes)
Catch
Return ""
End Try
End Function
Public Function decrypt(ByVal data As String) As String
Try
Dim crypted As Byte() = Convert.FromBase64String(data)
Dim decryptor As System.Security.Cryptography.ICryptoTransform = symmetricKey.CreateDecryptor(Key, iVector)
Dim memoryStream As New System.IO.MemoryStream(crypted)
Dim cryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(memoryStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read)
Dim plain(crypted.Length) As Byte
Dim count As Integer = cryptoStream.Read(plain, 0, plain.Length)
memoryStream.Close()
cryptoStream.Close()
Return System.Text.Encoding.UTF8.GetString(plain, 0, count)
Catch
Return ""
End Try
End Function
Public Sub New(ByVal clientkey As String)
iVector = System.Text.Encoding.ASCII.GetBytes("1234567890123456")
Key = System.Text.Encoding.ASCII.GetBytes(clientkey)
symmetricKey = New System.Security.Cryptography.RijndaelManaged
symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC
End Sub
End Class
esto funciona bien y puedo descifrar en java usando AAD / CBC / PKCS5Padding. Ahora, mi contraseña y mi iv son de 16 caracteres (16 * 16bit = 256). Cuando trato de descifrar en Ruby, se queja de que mi contraseña es demasiado corta ... Supongo que está usando caracteres de 8 bits. Yo uso esta clase para descifrar en ruby:
require 'openssl'
module Crypt
# Decrypts a block of data (encrypted_data) given an encryption key
# and an initialization vector (iv). Keys, iv's, and the data
# returned are all binary strings. Cipher_type should be
# "AES-256-CBC", "AES-256-ECB", or any of the cipher types
# supported by OpenSSL. Pass nil for the iv if the encryption type
# doesn't use iv's (like ECB).
#:return: => String
#:arg: encrypted_data => String
#:arg: key => String
#:arg: iv => String
#:arg: cipher_type => String
def Crypt.decrypt(encrypted_data, key, iv, cipher_type)
aes = OpenSSL::Cipher::Cipher.new(cipher_type)
aes.decrypt
aes.key = key
aes.iv = iv if iv != nil
aes.update(encrypted_data) + aes.final
end
# Encrypts a block of data given an encryption key and an
# initialization vector (iv). Keys, iv's, and the data returned
# are all binary strings. Cipher_type should be "AES-256-CBC",
# "AES-256-ECB", or any of the cipher types supported by OpenSSL.
# Pass nil for the iv if the encryption type doesn't use iv's (like
# ECB).
#:return: => String
#:arg: data => String
#:arg: key => String
#:arg: iv => String
#:arg: cipher_type => String
def Crypt.encrypt(data, key, iv, cipher_type)
aes = OpenSSL::Cipher::Cipher.new(cipher_type)
aes.encrypt
aes.key = key
aes.iv = iv if iv != nil
aes.update(data) + aes.final
end
end
Ahora. Al tratar de descifrar usando Crypt.decrypt (datos, clave, iv, "AES-CBC-256") estoy seguro de que se deben realizar transformaciones de cadena / byte preliminares en mis datos, clave, iv para que funcione correctamente.
¿Cómo debo llamar a Crypt.decrypt usando key = "passwordpassword" y iv = "1234567890123456"? ¿Necesito base64 decodificar mis datos?
Aquí está mi llamada de descifrado, que no parece funcionar (tratando de rellenar con ceros):
text = Base64.decode64(text)
pass = Digest::SHA1.hexdigest("#{@pass}0000000000000000").unpack('a2'*32).map{|x| x.hex}.pack('c'*32)
iv = Digest::SHA1.hexdigest("12345678901234560000000000000000").unpack('a2'*32).map{|x| x.hex}.pack('c'*32)
return Crypt.decrypt(text,pass,iv,"AES-256-CBC")