1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question Simple PGP

Discussion in 'Android Questions' started by GERSON PINTO, Jun 17, 2019.

  1. GERSON PINTO

    GERSON PINTO Member Licensed User

    Hello!
    I am trying to make an application that does encryption of a text. I'm basing on this DonManfred post:

    https://www.b4x.com/android/forum/threads/simplepgp-encrypt-decrypt-sign-verify.89521/#post-668497

    My code:

    Code:
    #AdditionalJar: bcpkix-jdk15on-1.58.0.0
    #AdditionalJar: bcprov-jdk15on-1.59
    #AdditionalJar: bctls-jdk15on-1.58.0.0
    #AdditionalJar: pg-1.54.0.0
    #AdditionalJar: sc-core-1.58.0.0
    #AdditionalJar: sc-prov-1.58.0.0
    #AdditionalJar: slf4j-api-1.7.25


    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.

    End Sub

    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
        Dim pgpkeys As PGPKeyPairGenerator
        
    Dim signer As PGPMessageSigner
        
    Dim crypt As PGPMessageEncryptor
        
    Private EditPass As EditText
        
    Private txt1 As EditText
        
    Private txt2 As EditText
        
    Private btnCrypt As Button
        
    Private btnDecrypt As Button
        
    Private btnCls As Button
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        Activity.LoadLayout("principal")

        
    Dim publicKey As OutputStream
        publicKey = 
    File.OpenOutput(File.DirRootExternal,"PublicKey.txt",False)
        
        
    Dim secretKey As OutputStream
        secretKey = 
    File.OpenOutput(File.DirRootExternal,"SecretKey.txt",False)

        pgpkeys.Initialize(
    "")
        
    Log(pgpkeys.generateKeyPair("gersonpinto","pass123",publicKey, secretKey))
        
        
    'signer.
        'crypt.encrypt(publickeyofreceipient, "Manfred",plaininputdata, destfile)
        
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub


    Sub btnCrypt_Click
        
        
    File.WriteString(File.DirRootExternal, "clear.txt", txt1.Text)
        
    File.WriteString(File.DirRootExternal, "crip.txt""criptografado")
        
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "crip.txt"False)
        
    Dim pk As InputStream = File.OpenInput(File.DirRootExternal,"PublicKey.txt")
        
    Dim plaindata As InputStream =File.OpenInput(File.DirRootExternal,"clear.txt")
    '       
        crypt.encrypt(pk,"name",plaindata,out)
    '   
        txt2.Text = File.ReadString(File.DirRootExternal,"criptografado.txt")
        out.Close
        pk.Close
        plaindata.Close

        
        
    End Sub
    Explanation:

    Public and private key generation is ok

    clear.txt is the file with the text to be encrypted

    crip.txt is the file with encrypted text

    I don´t understand what is inputDataName parameter in crypt.encrypt (second parameter) (is a string)

    The problem is crypt.encrypt method (not working for me)
    Any help?
     
  2. GERSON PINTO

    GERSON PINTO Member Licensed User

    The error:
    upload_2019-6-17_14-52-28.png
     
  3. DonManfred

    DonManfred Expert Licensed User

    Why you are not initializing the library?
    Initialize crypt in activity create before you use any method of it.
     
    Last edited: Jun 18, 2019
    GERSON PINTO 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