mattosaurus / PgpCore

.NET Core class library for using PGP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error in BouncyCastle.Utilities.Strings.ToUtf8ByteArray

HerdMS opened this issue · comments

has someone have experience with this error?

"An exception was thrown by the invocation.
Exception: System.ArgumentNullException: String reference not set to an instance of a String. (Parameter 's')"

seems has to do with Org.BouncyCastle.Utilities.Strings.ToUtf8ByteArray(String s

i added the debugger error messages, i logged the variables before the encryption executes to check if the are null, but they are not null, so i dont know, why its not encrypting.
i just did it locally. when i use file encryption it works, but stream encryption somehow produces this error.

any idea would be much appreciated!

[2023-12-13T21:23:18.487Z] Public key loaded successfully. [2023-12-13T21:23:18.489Z] Verifying streams before encryption... [2023-12-13T21:23:18.511Z] EncryptionKeys are null: False [2023-12-13T21:23:18.511Z] Beginning encryption process. [2023-12-13T21:23:18.511Z] OutputFileStream is null: False [2023-12-13T21:23:18.510Z] InputFileStream is null: False [2023-12-13T21:23:18.863Z] Function 'PGPencrypt_01_Firma', Invocation id 'd4b47244-1ee6-4cd9-9fbd-f74fc1bde4fc': An exception was thrown by the invocation. [2023-12-13T21:23:18.864Z] Result: Function 'PGPencrypt_01_Firma', Invocation id 'd4b47244-1ee6-4cd9-9fbd-f74fc1bde4fc': An exception was thrown by the invocation. Exception: System.ArgumentNullException: String reference not set to an instance of a String. (Parameter 's') [2023-12-13T21:23:18.865Z] at System.Text.Encoding.GetBytes(String s) [2023-12-13T21:23:18.865Z] at System.Text.UTF8Encoding.UTF8EncodingSealed.GetBytes(String s) [2023-12-13T21:23:18.866Z] at Org.BouncyCastle.Utilities.Strings.ToUtf8ByteArray(String s) [2023-12-13T21:23:18.867Z] at Org.BouncyCastle.Bcpg.OpenPgp.PgpLiteralDataGenerator.Open(Stream outStr, Char format, String name, Int64 length, DateTime modificationTime) [2023-12-13T21:23:18.867Z] at PgpCore.Utilities.WriteStreamToLiteralDataAsync(Stream output, Char fileType, Stream input, String name, Boolean oldFormat)

Hi, did you get this error whilst using PgpCore? If so can you share your code please.

Hi @mattosaurus,

i am using PgpCore 6.2.0 with bouncyCastle.Cryptography 2.1.1

this is my code:

`
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using PgpCore;

namespace encrypt_pgp_01_Firma
{
public class Function1
{
private readonly ILogger _logger;
private readonly IConfiguration _configuration;

    public Function1(ILogger<Function1> logger, IConfiguration configuration)
    {
        _logger = logger;
        _configuration = configuration;

    }

    [Function(nameof(Function1))]
    public async Task Run([BlobTrigger("%rawcontainer%Firma_{name}.csv", Connection = "")] Stream stream, string name)
    {
            
        // Stream - Version:

        // Load keys
        EncryptionKeys encryptionKeys;
        using (Stream publicKeyStream = new FileStream(@"C:\Users\frederik.ceglarek\Desktop\ReportingBeegyPublicKey.asc", FileMode.Open))
            encryptionKeys = new EncryptionKeys(publicKeyStream);

        // Reference input/output files
        PGP pgp = new PGP(encryptionKeys);

        using (FileStream inputFileStream = new FileStream(@"C:\Users\frederik.ceglarek\Desktop\Firma_20231206.csv", FileMode.Open))
        using (Stream outputFileStream = File.Create(@"C:\Users\frederik.ceglarek\Desktop\Neuer Ordner\Firma_20231206.pgp"))

            await pgp.EncryptAsync(inputFileStream, outputFileStream);
        
        /*
        // File - Version:

        // Load keys
        FileInfo publicKey = new FileInfo(@"C:\Users\frederik.ceglarek\Desktop\ReportingBeegyPublicKey.asc");
        EncryptionKeys encryptionKeys = new EncryptionKeys(publicKey);

        // Reference input/output files
        FileInfo inputFile = new FileInfo(@"C:\Users\frederik.ceglarek\Desktop\Firma_20231206.csv");
        FileInfo encryptedFile = new FileInfo(@"C:\Users\frederik.ceglarek\Desktop\Neuer Ordner\Firma_20231206.pgp");

        // Encrypt
        PGP pgp = new PGP(encryptionKeys);
        await pgp.EncryptAsync(inputFile, encryptedFile);
        */
    }
}

}
`

Curious thing is, when i use the File encryption, it works properly, only when i try to use stream encryption, the mentioned error occurs.

Cool, I'll take a look later when I get a chance (hopefully today or tomorrow). If this is urgent I'd suggest reverting back to v5.13.1 for now.

My initial thought is that the stream gets read by something else in PgpCore first and is then at the end when it comes to be read by this process.

Hi,
i was able to solve this by going back to v5.13.1.

I think pgp.EncryptAsync is not available in this version so I used pgp.EncryptStreamAsync, which worked as aspected!

Thanks for your help!

This was an issue with not setting the filename when one was available. It should now be fixed in v6.3.