B4J Question Error when encrypting a json string

IslandMedic

Member
Licensed User
Here is the json string I am attempting to encrypt.

{
"root": [
{
"user_name": 0,
"id": 1607406053,
"status": "online"
},
{
"user_name": 0,
"id": 2112661677,
"status": "offline"
}
]
}

Here is the line i use to do it:
Sub encrypt(data As String,user_id As String) As String
'call a sub that looks up the password
Dim Password As String = reterive_password
'do the encryption using the data and the password
Return su.EncodeUrl(su.encodeBase64(Cipher.Encrypt(data.GetBytes("UTF8"), Password)), "UTF8")
End Sub

The error I get is:
Error occurred on line: 17 (Security)
java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
 

IslandMedic

Member
Licensed User
I took the encrypt/decrypt example and made it a stand alone application. I fed it the same json string with the exception I escaped the " with "" and it seem to work just fine. Then I take the same code and put it into my application and it fails with the same error even after escaping the ". The only difference I can think of is some special characters in the json output that is created from the code, as apposed to me copying the data via the clipboard and just pasting it in the stand alone example.
 

IslandMedic

Member
Licensed User
Solved:

For a beginner, I wouldn't put an expression on the line with a return. For example,

Return su.EncodeUrl(su.encodeBase64(Cipher.Encrypt(data.GetBytes("UTF8"), Password)), "UTF8")

The first line throws an error at this line #, but the error is in the receiving of the return. So when you code the it the following way, you quickly see that it has nothing to do with the encryption but the "return result" line. This then led to me the receiving routine where I found the error.

Dim result As String = su.EncodeUrl(su.encodeBase64(Cipher.Encrypt(data.GetBytes("UTF8"), Password)), "UTF8")
Return result

No wonder I am losing my hair!

Brad
 
Top