Hello,
I would like to encrypt and decrypt text file.
I use this code :
But I have a error when I decrypt : BadPaddingException: pad block corrupted.
I don't see the reason.
Thank for your help.
I would like to encrypt and decrypt text file.
I use this code :
B4X:
Sub btnMd5_Click
'Code module
Dim mode As Int=0
Dim text As String="test de cryptage"
Get(text , mode )
Dim code As String=Get(text , mode )
mode=1
Get(code , mode )
Dim code1 As String=Get(text , mode )
End Sub
Sub Get(Text As String, Mode As Int) As String 'mode= 0/1 = encode/decode
If Text = Null OR Text = "" Then Return ""
Dim key(0) As Byte
Dim data(0) As Byte
Dim bytes(0) As Byte
Dim iv(0) As Byte
Dim sSecretKey As String = "ysatisfh"
iv = Bconv.StringToBytes(sSecretKey,"ISO-8859-1")
key = Bconv.StringToBytes(sSecretKey,"ISO-8859-1")
Dim Kg As KeyGenerator
Dim c As Cipher
c.InitialisationVector = iv
c.Initialize("DES/CBC/PKCS5Padding") ' just "DES" actually performs "DES/ECB/PKCS5Padding".
Kg.Initialize("DES")
Kg.KeyFromBytes(key)
If Mode = 0 Then
Text = padString(Text)
data = Bconv.StringToBytes(Text, "UTF8")
data = c.Encrypt(data, Kg.key, True)
Return Bconv.HexFromBytes(data)
Else If Mode = 1 Then
data = Bconv.HexToBytes(Text)
bytes = c.Decrypt(data, Kg.key, True)
Return Bconv.StringFromBytes(bytes,"UTF8").Trim
End If
End Sub
Sub padString(source As String) As String
Dim paddingChar As String, size, x, padLength As Int
paddingChar = " "
size = 16
x = source.Length Mod size
padLength = size - x
For i = 0 To padLength - 1
source = source & paddingChar
Next
Return source
End
But I have a error when I decrypt : BadPaddingException: pad block corrupted.
I don't see the reason.
Thank for your help.