Android Question [solved] EncryptText String in Map

Discussion in 'Android Questions' started by MarkusR, May 22, 2018.

Tags:
  1. MarkusR

    MarkusR Well-Known Member Licensed User

    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
    Code:
    '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 StringAs 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 StringAs 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
    Snap_2018.05.22_20h42m20s_001_.png
     
    Last edited: May 22, 2018
  2. DonManfred

    DonManfred Expert Licensed User

    I do not see any Error posted
     
    MarkusR likes this.
  3. MarkusR

    MarkusR Well-Known Member Licensed User

    ok, i will send tomorrow.
     
  4. MarkusR

    MarkusR Well-Known Member Licensed User

    i updated first message :)
     
  5. DonManfred

    DonManfred Expert Licensed User

    You should post errors as TEXT.
     
    MarkusR likes this.
  6. MarkusR

    MarkusR Well-Known Member Licensed User

    no problem yet
     
  7. KZero

    KZero Active Member Licensed User

    try to save the encrypted bytes base64 String instead of UTF8 string
     
    MarkusR likes this.
  8. KZero

    KZero Active Member Licensed User

    Use Encryption Library
    Code:
    Sub EncryptText(text As StringAs String
        
    Log("EncryptText " & text)
        
    Dim c As B4XCipher
        
    Dim e() As Byte
        e = c.Encrypt(text.GetBytes(
    "utf8"), Password)
        
    Dim B64 As Base64
        
    Return B64.EncodeBtoS(e,0,e.Length)
    End Sub

    Sub DecryptText(text As StringAs String
        
    Log("DecryptText " & text)
        
    If text.Length=0 Then Return ""
        
    Dim B64 As Base64
        
    Dim c As B4XCipher
        
    Dim a() As Byte = B64.DecodeStoB(text)
        
    Dim b() As Byte = c.Decrypt( a, Password)
        
    Return BytesToString(b, 0, b.Length, "UTF-8")
    End Sub
     
    MarkusR likes this.
  9. MarkusR

    MarkusR Well-Known Member Licensed User

    @KZero
    thank you, in the same time i got this :)
    the idea with base 64 works

    Code:
    'verschlüsseln
    Sub EncryptText(text As StringAs String

        
    Log("EncryptText " & text)

        
    Dim c As B4XCipher

        
    Dim e() As Byte
        e = c.Encrypt(text.GetBytes(
    "utf8"), Password)
        
        
    Dim su As StringUtils
        
        
    Return su.EncodeBase64(e)

    End Sub

    'entschlüsseln
    Sub DecryptText(textb64 As StringAs String

        
    Log("DecryptText " & textb64)

        
    'Try
            Dim c As B4XCipher
            
    Dim su As StringUtils
        
    Dim a() As Byte = su.DecodeBase64(textb64)
            
    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
     
    KZero likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice