Android Question Rijndael encryption with custom PassPhrase

Stephen A Wolfe

Member
Licensed User
I prefer to use Rijndael encryption, but my VB code for my projects does not work in B4A, although I have seen people use similar examples in threads here. For security reasons, I need to allow each user to set their own PassPhrase in order to prevent wide-scale attacks or reading encrypted data using the same app on a different user's device.

I'm completely new to B4A, but if I can implement this with two subs (Encrypt & Decrypt), that would be great.

Thanks in advance.
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Stephen A Wolfe

Member
Licensed User
https://www.b4x.com/android/forum/threads/base64-and-encryption-library.6839/#content


So i guess it should work using the Encryption and ByteConverter libraries.

Like I said, I'm new to B4A, so don't really know why I'm having issues.

I put this code in my project and almost everything is red underlined:
B4X:
Sub EncryptText(text As String, password As String) As Byte()
    Dim c As B4XCipher
    Return c.Encrypt(text.GetBytes("utf8"), password)
End Sub

Sub DecryptText(EncryptedData() As Byte, password As String) As String
    Dim c As B4XCipher
    Dim b() As Byte = c.Decrypt(EncryptedData, password)
    Return BytesToString(b, 0, b.Length, "utf8")
End Sub

Dim EncryptedData() As Byte = EncryptText("confidential", "123456")
Log(DecryptText(EncryptedData, "123456"))

Then I have this:
B4X:
ReadString = EncryptText(ReadString)

And that's an error also.
 
Upvote 0

Stephen A Wolfe

Member
Licensed User
B4XEncryption expects the data to be formatted in a very specific way. If you need to decrypt data coming from other non-B4X platforms then you should use the Encryption library instead.

All I'm trying to do is securely encrypt a string using my example. The unencrypted string is in ReadString.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Stephen A Wolfe

Member
Licensed User

I'm not converting bytes to a string. I need to encrypt a string. For example, ReadString = "Test" After I encrypt it, ReadString should be something like "2lrmu7JPVaQ2DC/jjo9C8A=="
 
Upvote 0

Stephen A Wolfe

Member
Licensed User

So, this is secure?
B4X:
Dim su As StringUtils
Dim encoded As String
encoded = su.EncodeBase64(data) 'data is a bytes array

I put this in my project and get an error for su. How do I add the library reference?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
variable 'Data' is used before it was assigned any value.
"Data" is the name of the String you want to be Base64 encoded. So in your case the string you wan't to be encrypted, I guess. Just replace the name "Data" with the name of your own variable
 
Upvote 0

Stephen A Wolfe

Member
Licensed User
"Data" is the name of the String you want to be Base64 encoded. So in your case the string you wan't to be encrypted, I guess. Just replace the name "Data" with the name of your own variable

I tried that using ReadString = su.EncodeBase64(ReadString)

But it gives an error. Also, this method isn't even remotely secure. I do know that much. I'm not planning to write banking software, but the encryption does need to be a lot more secure than Base64 encoding.
 
Upvote 0

Stephen A Wolfe

Member
Licensed User
"Data" is the name of the String you want to be Base64 encoded. So in your case the string you wan't to be encrypted, I guess. Just replace the name "Data" with the name of your own variable

Here's what I've come up with:
B4X:
Sub EncryptText(text As String, password As String) As Byte()
    Dim c As B4XCipher
    Return c.Encrypt(text.GetBytes("utf8"), password)
End Sub

And:
B4X:
    ReadString = "Testing"
    EncryptText(ReadString, "MyPassword")
    File.WriteString(File.DirRootExternal, "MyProduct/Settings.txt", ReadString)

But ReadString in this code does not get encrypted. What am I missing here? I tried ReadString = EncryptText(ReadString, "MyPassword") but that doesn't work.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim EncryptedData As String = EncryptText("confidential", "123456")
   Log("Encrypted: " & EncryptedData)
   Dim Decrypted As String = DecryptText(EncryptedData, "123456")
   Log("Decrypted: " & Decrypted)
End Sub

Sub EncryptText(text As String, password As String) As String
   Dim c As B4XCipher
   Dim su As StringUtils
   Return su.EncodeBase64(c.Encrypt(text.GetBytes("utf8"), password))
End Sub

Sub DecryptText(EncryptedData As String, password As String) As String
   Dim c As B4XCipher
   Dim su As StringUtils
   Dim b() As Byte = c.Decrypt(su.DecodeBase64(EncryptedData), password)
   Return BytesToString(b, 0, b.Length, "utf8")
End Sub
 
Upvote 0

Stephen A Wolfe

Member
Licensed User
EncryptText is a method which return a string- But you are NOT using the result....

it should work. Do you get any error?

I'm indeed getting an error. ReadString = DecryptText(ReadString, "MyPassword")
is underlined red and says this super vague message "Type=String, Rank=0 --> Type=Byte, Rank1

I have been able to get this working mostly. I'm able to read in the encrypted string into an Object variable, but I can't seem to figure out how to read in the string lines into a string array.

Here's what I've come up with so far. I only get an error when I run it though. I have tried so many things but need help to figure out what I'm doing wrong. The encryption part is working great and I can decrypt the encrypted string, but reading the encrypting string and trying to put the string lines into a string array is giving me issues. Here is my code:
B4X:
       Dim ReadString As String
    Dim EncryptedString As Object
    Dim MyPhone As PhoneId

B4X:
    EncryptedString = File.ReadString(File.DirRootExternal, "MyFolder/MyFile.txt")
    ReadString = DecryptText(EncryptedString, MyPhone.GetDeviceId)
    FileLoc = Regex.Split(CRLF, ReadString)
    Msgbox(FileLoc(1), "The line of decoded array I need")

I'm getting an error "String cannot be cast to Byte". It seems like this should work. The error occurs for the decrypt line. I am saving the file using the reverse method (
File.WriteString(File.DirRootExternal, "MyFolder/MyFile.txt", EncryptedString)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…