Android Question VB.NET and B4A Encryption & Decryption

Alex_197

Well-Known Member
Licensed User
Longtime User
For B4A

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private strPWD As String="YourPasswordHere"


End Sub





Sub Encrypt(dataToEncrypt As String ) As String

    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 = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88) ' 16 bytes for AES
      
    c.Initialize("DESEDE/CBC/PKCS5Padding")
    c.InitialisationVector = iv
    kg.Initialize("DESEDE")
  
    
    kg.KeyFromBytes(bconv.StringToBytes(strPWD,"ASCII"))
    
    data = bconv.StringToBytes(dataToEncrypt, "ASCII")
    
    data = c.Encrypt(data, kg.Key, True)

    Return B64.EncodeBtoS(data, 0, data.Length)
  
End Sub

Sub Decrypt(encryptedData As String ) As String

    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 = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88) ' 16 bytes for AES
      
    c.Initialize("DESEDE/CBC/PKCS5Padding")
    c.InitialisationVector = iv
    kg.Initialize("DESEDE")
    
    
    kg.KeyFromBytes(bconv.StringToBytes(strPWD,"ASCII"))
    
  
  
    data = B64.DecodeStoB(encryptedData)
    data = c.Decrypt(data, kg.Key, True)

    Return bconv.StringFromBytes(data, "ASCII")

End Sub

For VB.NET


B4X:
 Public Function Encrypt(dataToEncrypt As String) As String

        Try

            If dataToEncrypt = "" Then
                Return "-1"
            End If

            Dim buffer As Byte() = Encoding.ASCII.GetBytes(dataToEncrypt)
            Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
            des.IV = New Byte() {211, 5, 233, 24, 55, 166, 7, 88}

            des.Key = ASCIIEncoding.UTF8.GetBytes(YourPasswordHere)


            Dim result = System.Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length))
            des.Clear()
            Return result

        Catch ex As Exception
          
            Return "-1"
        End Try

    End Function

    Public Function Decrypt(encryptedData As String) As String

        Try

            Dim buffer As Byte() = Convert.FromBase64String(encryptedData)
            Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
            des.IV = New Byte() {211, 5, 233, 24, 55, 166, 7, 88}

            des.Key = ASCIIEncoding.UTF8.GetBytes(YourPasswordHere)

            Dim result As String = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length))
            des.Clear()
            Return result

        Catch ex As Exception
            
            Return "-1"
        End Try

    End Function
 
Upvote 0
Top