bc-csharp icon indicating copy to clipboard operation
bc-csharp copied to clipboard

Session key decryption failed at client side.

Open AkhileshKumar1991 opened this issue 4 years ago • 0 comments

I am trying to send a pgp encrypted mail to FlowCrypt client. For this, I am using the recipient public key for session key encryption. At client side decryption of session key is getting failed. Below is code snippet for encryption. Please drive me from here. I am also attaching OpenPGP algorithm used by FlowCrypt. Please let me know if any further information required.

    public static string  EncryptPgpstring(string inputFile, string outputFile, string publicKeyFile, bool armor, bool withIntegrityCheck)
            {
                if (!File.Exists(inputFile))
                {
                    NWLogger.SDK.LogWriter.Write(pi_NWLog, LogWriter.EntryStatus.Failed, "Input File not found.");
                }
    
                try
                {
                    using (Stream publicKeyStream = File.OpenRead(publicKeyFile))
                    {
                        PgpPublicKey pubKey = ReadPublicKey(publicKeyStream);
    
                        using (MemoryStream outputBytes = new MemoryStream())
                        {
                            PgpCompressedDataGenerator dataCompressor = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip);
                            PgpUtilities.WriteFileToLiteralData(dataCompressor.Open(outputBytes), PgpLiteralData.Binary, new FileInfo(inputFile));
    
                            dataCompressor.Close();
                            PgpEncryptedDataGenerator dataGenerator = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Aes256, withIntegrityCheck, new SecureRandom());
    
                            dataGenerator.AddMethod(pubKey);
                            byte[] dataBytes = outputBytes.ToArray();
                            //string message = Encoding.UTF8.GetString(dataBytes);
                            string message1 = Convert.ToBase64String(dataBytes);
    
    
                            using (Stream outputStream = File.Create(outputFile))
                            {
                                if (armor)
                                {
                                    using (ArmoredOutputStream armoredStream = new ArmoredOutputStream(outputStream))
                                    {
                                        IoHelper.WriteStream(dataGenerator.Open(armoredStream, dataBytes.Length), ref dataBytes);
                                    }
                                }
                                else
                                {
                                    IoHelper.WriteStream(dataGenerator.Open(outputStream, dataBytes.Length), ref dataBytes);
                                }
                            }
    
                            string message = File.ReadAllText(IoHelper.BasePath + @"\pgp-encrypted.asc");
                            return message;
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    LogWriter.Write();
                    throw;
                }
                
            }

OpenPGP Algo

AkhileshKumar1991 avatar Oct 12 '21 13:10 AkhileshKumar1991