hello,
i try to Encrypt a string into a map Password key.
i will use WriteMap for saving
then i will use ReadMap and Decrypt the key Password back into original
but decrypt did not work and run into error^^
code modul start with Test
i try to Encrypt a string into a map Password key.
i will use WriteMap for saving
then i will use ReadMap and Decrypt the key Password back into original
but decrypt did not work and run into error^^
code modul start with Test
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.
Dim SettingsMap As Map
Dim FileName As String
Dim Password As String
End Sub
Sub Test
Log("Test")
SettingsInit
SettingsLoad
Log( SettingsMap.Get("User"))
Log( DecryptText(SettingsMap.Get("Password")))
SettingsMap.Put("User","Markus")
SettingsMap.Put("Password",EncryptText("MeinPassword"))
SettingsSave
SettingsLoad
Log( SettingsMap.Get("User"))
Log( DecryptText(SettingsMap.Get("Password")))
End Sub
Sub SettingsInit
Log("SettingsInit")
FileName = "BinPackingSettings.dat"
Password = "123"
SettingsMap.Initialize
SettingsMap.Clear
SettingsMap.Put("User","")
SettingsMap.Put("Password","")
End Sub
Sub SettingsSave() As Boolean
Log("SettingsSave")
File.WriteMap(File.DirInternal,FileName,SettingsMap)
Log("Saved")
Return True
End Sub
Sub SettingsLoad()
Log("SettingsLoad")
If File.Exists(File.DirInternal,FileName)=True Then
SettingsMap = File.ReadMap(File.DirInternal,FileName)
Log("Loaded")
Else
ToastMessageShow("new settings",False)
End If
End Sub
'verschlüsseln
Sub EncryptText(text As String) As String
Log("EncryptText " & text)
Dim c As B4XCipher
Dim e() As Byte
e = c.Encrypt(text.GetBytes("utf8"), Password)
Return BytesToString(e,0,e.Length,"UTF-8")
End Sub
'entschlüsseln
Sub DecryptText(text As String) As String
Log("DecryptText " & text)
'Try
Dim c As B4XCipher
Dim a() As Byte = text.GetBytes("utf8")
If a.Length=0 Then Return ""
Dim b() As Byte = c.Decrypt( a, Password)
Return BytesToString(b, 0, b.Length, "UTF-8")
'Catch
' Log(LastException)
' Return ""
' End Try
End Sub
Last edited: