Android Question How can I add a line of the file to a string

Alisson

Active Member
Licensed User
Hi!
How can I add a line of the file to a string.
The string makes an HTTP request.
This is my code:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("noticias")
    conexao.Initialize(0, "")
    conexao.Listen
   
       
    Dim pubkey(0) As Byte
    Dim prvkey(0) As Byte   
    Dim data(0) As Byte
    Dim formats(0) As String
    Dim Reader As TextReader
   
    Dim kpg As KeyPairGenerator
    Dim c As Cipher
    c.Initialize("RSA")
    kpg.Initialize("RSA", 512) ' the maximum number of bytes that can be encrypted increase with key size
    kpg.GenerateKey
    formats = kpg.Formats
   
    data = kpg.PublicKeyToBytes ' check we can round trip the public key as X509
    Msgbox(formats(0) & CRLF & Bconv.HexFromBytes(data), "Public key " & data.Length & " bytes")
    kpg.PublicKeyFromBytes(data)
   
    data = kpg.PrivateKeyToBytes  ' check we can round trip the public key as PKCS8
    Msgbox(formats(1) & CRLF & Bconv.HexFromBytes(data), "Private key " & data.Length & " bytes")
    kpg.PrivateKeyFromBytes(data)
   
    Reader.Initialize(File.OpenInput(File.DirRootExternal, "crypto.mdl")) 'Open file with url encrypted
        clear = Reader.ReadLine                        ' read the line
        Reader.Close                                         'close file   
       
       
   
    data = Bconv.StringToBytes(clear, "UTF8")                                     
    data = c.Encrypt(data, kpg.PublicKey, False)   
   
    data = c.Decrypt(data, kpg.PrivateKey, False)             ' decrypted in the theory
    link1 = Bconv.StringFromBytes(data, "UTF8")               ' variable to with url decrypted
   
        Dim job3 As HttpJob
        job3.Initialize("Job3", Me)

   job3.Download(link1)
  
   
End Sub

If use one string, exemple:
url = "http://www.site.com"
Its OK!

But my exemple not work!

Thanks! :)
 

DonManfred

Expert
Licensed User
Longtime User
But my exemple not work!
I dont see a JobDone sub!?

Please post more complete code

Additional a snippet from the File Methods tutorial

B4X:
Sub ReadListExample
    Dim List1 As List
    'We are not initializing the list because it just holds the list that returns from File.ReadList
    List1 = File.ReadList(File.DirRootExternal, "List.txt")
    Msgbox("List1.Size = " & List1.Size & CRLF & "The third item is: " & List1.Get(2), "")
End Sub
 
Upvote 0

Alisson

Active Member
Licensed User
I switched RSA by base64.
My app now use base64 encrypt.

Encrypt:

B4X:
Reader.Initialize(File.OpenInput(File.DirRootExternal, "1.txt"))
          Dim line As String
        line = Reader.ReadLine
        Reader.Close
          b64str = B64.EncodeStoS(line, "UTF8")
        Writer.Initialize(File.OpenOutput(File.DirRootExternal, "crypto.mdl", False))
        Writer.WriteLine(b64str)
        Writer.Close

Decrypt:

B4X:
Dim B64 As Base64
Reader.Initialize(File.OpenInput(File.DirRootExternal, "crypto.mdl"))
        link1 = Reader.ReadLine
        Reader.Close
    b64str = B64.DecodeStoS(link1,"UTF8")

DonManfred thanks very much.
 
Upvote 0
Top