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.
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"))
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.
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.
I recommend you to start with the video tutorials: https://www.b4x.com/etp.html
This specific issue is discussed in one of the videos (probably about strings though I'm not sure).
I recommend you to start with the video tutorials: https://www.b4x.com/etp.html
This specific issue is discussed in one of the videos (probably about strings though I'm not sure).
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=="
I recommend you to start with the video tutorials: https://www.b4x.com/etp.html
This specific issue is discussed in one of the videos (probably about strings though I'm not sure).
"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
"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.
"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
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.
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
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.
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
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)