iOS Question B4A encryption and B4I Decryption

Rafael Perez Jurado

Member
Licensed User
Longtime User
When I encrypt a text in B4A and I try decrypt with B4I the result is blank....
 

Attachments

  • androidencrypt.zip
    7.4 KB · Views: 394
  • IosEncript.zip
    2.8 KB · Views: 398

Erel

B4X founder
Staff member
Licensed User
Longtime User
I changed your B4A code to log the output and run it.

The result was:
A6z/x16jbBntUNIzjvKefEPbvDPb04FmIeebPrquterSONxejoJEAQ==

I took this string to the B4i project and it successfully decrypted it and returned hola.
B4X:
Dim s As String = Decrypt("A6z/x16jbBntUNIzjvKefEPbvDPb04FmIeebPrquterSONxejoJEAQ==", "M0rtadel0")
The string returns was padded with zero bytes.

You can remove it with this line:
B4X:
s = s.Replace(Chr(0), "")
 
Upvote 0

Rafael Perez Jurado

Member
Licensed User
Longtime User
Ok, It's work, the last question is:

I need to decrypt too with VB .NET, if this AES Sha-1, but i need the IV for the function of decrypt.

B4X:
Public Function Descifrar_AES() As String
Dim contrasena As String = "loco"
Dim cad As String = "l0c0"
Dim algoritmo As String = "sha1"
Dim iteraciones As Integer = 1000
Dim vectorInicial As String = "1234567896541235"
Dim tamanollave As Integer = 256

Try
Dim vectorInicialB As Byte()
vectorInicialB = Encoding.ASCII.GetBytes(vectorInicial)
Dim cadenaB As Byte()
cadenaB = Encoding.ASCII.GetBytes(cad)
Dim textoACifrarB As Byte()
textoACifrarB = Convert.FromBase64String(cadenaCif)
Dim contra As Rfc2898DeriveBytes
contra = New Rfc2898DeriveBytes(contrasena, cadenaB, iteraciones)
Dim llaveB As Byte()
llaveB = contra.GetBytes(tamanollave / 8)
Dim claveSimetrica As RijndaelManaged
claveSimetrica = New RijndaelManaged()
claveSimetrica.Mode = CipherMode.CBC
Dim descifrador As ICryptoTransform
descifrador = claveSimetrica.CreateDecryptor(llaveB, vectorInicialB)
Dim lector As MemoryStream
lector = New MemoryStream(textoACifrarB)
Dim crypto As CryptoStream
crypto = New CryptoStream(lector, descifrador, CryptoStreamMode.Read)
Dim textoPlanoB As Byte()
ReDim textoPlanoB(textoACifrarB.Length)
Dim bytesDesencriptados As Integer
bytesDesencriptados = crypto.Read(textoPlanoB, 0, textoPlanoB.Length)
lector.Close()
crypto.Close()
Dim textoPlano As String
textoPlano = Encoding.UTF8.GetString(textoPlanoB, 0, bytesDesencriptados)
cadena = textoPlano
bError = False
Return cadena
sMsg = "Descifrado Exitoso."
Catch ex As Exception
bError = True
sMsg = "Descifrado fallo: " & ex.Message
Return sMsg
End Try
End Function
 
Upvote 0
Top