How to use pgpainless-core

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5168

    #1

    How to use pgpainless-core

    Document




    Setup





    // If you use Gradle
    ...
    dependencies {
    ...
    implementation "org.pgpainlessgpainless-core:1.7.2"
    ...
    }

    // If you use Maven
    ...

    ...

    org.pgpainless
    pgpainless-core
    1.7.2

    ...








    What's ASCII Armor

    ASCII armor is a layer of radix64 encoding that can be used to wrap binary OpenPGP data in order to make it save to transport via text-based channels (e.g. email bodies).


    The way in which ASCII armor can be applied depends on the type of data that you want to protect. The easies way to ASCII armor an OpenPGP key or certificate is by using PGPainless’ asciiArmor() method:






    PGPPublicKey certificate = ...;
    String asciiArmored = PGPainless.asciiArmor(certificate);







    How to Encrypt message or file

    1. Suppose you have a FileInputStream or ByteArrayInputStream. Then
      also you need to have a OutputStream, like




    OutputStream outputStream =
    Files.newOutputStream(outputFileLocation, StandardOpenOption.CREATE_NEW);






    1. Before you call the Painless.encryptAndOrSign(), you need to build up the ProducerOptions. It will be like




    /** The secret key ring used for decrypting and signing. */
    private final PGPSecretKeyRing secretKeyRing = ...;

    /** The public key ring used for encrypting. */
    private final PGPPublicKeyRing publicKeyRing = ...;

    /** Stores the password securely, required to access the secret key. */
    private final SecretKeyRingProtector secretKeyRingProtector = ...;

    SigningOptions signOptions = SigningOptions.get().addInlineSignature(secretKeyR ingProtector, secretKeyRing);







    when you try to build your secretKeyRing and secretKeyRingProtector, you probably will get it from your properties like






    this.secretKeyRing = armorStringKeyringLoader.load(properties.getSecret Key(), PGPSecretKeyRing.class)











    this.publicKeyRing = armorStringKeyringLoader.load(properties.getPublic Key(), PGPPublicKeyRing.class)







    In order to load the key, all you need to do is to get the BufferedInputStream, then






    protected T readKeyRing(BufferedInputStream keyInputStream, Class clazz) throws IOException, KeyRingLoaderException {
    if (clazz.isAssignableFrom(PGPSecretKeyRing.class)) {
    return clazz.cast(PGPainless.readKeyRing().secretKeyRing( keyInputStream));
    } else if (clazz.isAssignableFrom(PGPPublicKeyRing.class)) {
    return clazz.cast(PGPainless.readKeyRing().publicKeyRing( keyInputStream));
    } throw new KeyRingLoaderException(
    String.format("Unsupported key ring type [class=%s]", clazz.getSimpleName()));
    }











    ProducerOptions options = ProducerOptions.signAndEncrypt(signingOptions, encryptionOptions);









    More...
Working...