helllllppp with encrypt/decrypt DES plis

charro98120165

Member
Licensed User
Longtime User
hi I ned encrypt/decrypt a string. example

string = alfonso pascual herrejon

wen i push a button encrypt the string and wen push otter button decrypt de same string this is my code

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim txtescritura As EditText
   Dim txtdecodifica As EditText
   Dim Bconv As ByteConverter
   Dim temp(0) As Byte
   
   Dim key(0) As Byte
   Dim data(0) As Byte
   Dim iv(0) As Byte
   Dim B64 As Base64
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("encdec")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btncodifica_Click

   Dim kg As KeyGenerator
    Dim c As Cipher
    Dim B64 As Base64
    Dim Bconv As ByteConverter

    Dim data(0) As Byte
    Dim iv(0) As Byte
   iv = "(=Lo78&@".GetBytes("ASCII")
        
    c.Initialize("DES/CBC/PKCS5Padding")     
    c.InitialisationVector = iv
    kg.Initialize("DES")  
   kg.GenerateKey
   key = kg.KeyToBytes
   Msgbox(Bconv.HexFromBytes(key), "Key " & key.Length & " bytes")
   
   clear = "alfonso pascual herrejon"
   data = Bconv.StringToBytes(clear, "UTF8")
   data = c.Encrypt(data, kg.key, True)
   temp = data
   txtescritura.Text = Bconv.HexFromBytes(data) 
   'Msgbox(Bconv.HexFromBytes(data), "Encrypted is " & data.Length & " bytes")              
   
End Sub


Sub btndecodifica_Click 
   Dim kg As KeyGenerator
    Dim c As Cipher
    Dim B64 As Base64
    Dim Bconv As ByteConverter

    Dim data(0) As Byte
    Dim iv(0) As Byte
    iv = "(=Lo78&@".GetBytes("ASCII")
        
    c.Initialize("DES/CBC/PKCS5Padding")     
    c.InitialisationVector = iv
    kg.Initialize("DES")  
   kg.GenerateKey
   key = kg.KeyToBytes
   data = temp 
   data = c.Decrypt(data, kg.key, True)
   Msgbox(Bconv.StringFromBytes(data, "UTF8"), "Decrypted")
   
End Sub

but wen push decrypted button the program stop in line

B4X:
data = c.Decrypt(data, kg.key, True)

helpme plis, sorry my bad english :) and tanks
 

charro98120165

Member
Licensed User
Longtime User
i have this code to encrypt and decrypt in vb .net wath is the way for traslate to B4A or wath metod?

Public Function Encrypt(ByVal plainText As String) As Byte()
Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider
des.Mode = CipherMode.ECB
'esta es la llave de myairtimer
des.Key = Encoding.ASCII.GetBytes("(=Lo78&@")
des.IV = des.Key
Dim encryptor As ICryptoTransform = des.CreateEncryptor
Dim x() As Byte = UTF8Encoding.UTF8.GetBytes(plainText)
Dim enc() As Byte = encryptor.TransformFinalBlock(x, 0, x.Length)
Return enc
End Function


Public Function DeCrypt(ByVal encrytedBytes As Byte()) As Byte()
Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider
des.Mode = CipherMode.ECB
'esta es la llave de myairtimer
des.Key = Encoding.ASCII.GetBytes("(=Lo78&@")
des.IV = des.Key
Dim decryptor As ICryptoTransform = des.CreateDecryptor
Dim dec() As Byte = decryptor.TransformFinalBlock(encrytedBytes, 0, encrytedBytes.Length)
Return dec
End Function

in the first post wrote the posible traslate to B4A its the same or wath is rong?
tanks
 
Upvote 0

charro98120165

Member
Licensed User
Longtime User
resolv the problem :) now ned send

my string encrypt with asincstreams who is this? I need send the BYTES. tanks

Sub btncodifica_Click


' use DES for variable length data using padding

c.Initialize("DES/CBC/PKCS5Padding") ' replace "DES/" with "AES/" for Rijndael or "DESEDE/" for triple DES

' CBC needs an initialisation vector
c.InitialisationVector = iv
kg.Initialize("DES") ' replace "DES" with "AES" for Rijndael or "DESEDE" for triple DES
'kg.GenerateKey
kg.KeyFromBytes(Bconv.StringToBytes("(=Lo78&@","ASCII"))
key = kg.KeyToBytes
Msgbox(Bconv.HexFromBytes(key), "Key " & key.Length & " bytes")

clear = "1:11;2:SUC_0001;3:MAQ_0001;4:00-0A-EB-54-92-57;5:RECING;6:VF;13:EQUIPO1;14:0100"
data = Bconv.StringToBytes(clear, "UTF8")
data = c.Encrypt(data, kg.key, True)
Msgbox(Bconv.HexFromBytes(data), "Encrypted is " & data.Length & " bytes")

End Sub
 
Upvote 0
Top