0

I am working on a project in which API request & responses are sent after PGP encryption and then Base64 encoding. Similarly, after receiving the request, I have to first decode it with base64 & then PGP decryption is performed.

Whenever I use my own function in .Net to decode the request, PGP decryption gives error of illegal characters or unknown message format. But when I decode the request from an online tool that is https://www.base64decode.org , my PGP decryptor works fine.

I am using DidiSoft.PGP library for PGP encryption/decryption. Below is the code snippet I am using for Base64 decoding:

public static string Base64Decode(string base64EncodedData)
    {
        //Base64 URL Decode
        string converted = base64EncodedData
                            .Replace('-', '+')
                            .Replace('_', '/')
                            .Replace(@"\/", "/");

        byte[] base64EncodedBytes =  Convert.FromBase64String(converted);
        return Encoding.UTF8.GetString(base64EncodedBytes);
    }

I wonder what algorithm is used in the online tool, or what I am missing to get the correct string. Any help is appreciated.

EDIT: One additional info I found out that I need to perform Base64 decoding according to RFC4648 Section 5 (RFC 4648 §5: base64url (URL- and filename-safe standard)). Can anyone pls. guide its implementation in .NetCore or is there any NugetPackage to do so

Community
  • 1
  • 1
  • You seem to assume that the problem is related to the Base64 decoding itself (which I don't want to rule out, the method converts Base64url to Base64 messages, see [here](https://en.wikipedia.org/wiki/Base64#Variants_summary_table) for the various Base64 variants). But another possibility seems more plausible to me. – Topaco Mar 12 '21 at 08:46
  • 1
    Possibly the issue is due to the UTF8 decoding. If I understand it correctly, the method is used for the Base64 decoding of _ciphertexts_. However, these are generally corrupted by a UTF8 decoding, which then possibly causes your error as a consequential error. Normally the Base64 decoded data is passed to the decryption in binary form, i.e. as `byte[]` (unless the decryption does the Base64 decoding itself). – Topaco Mar 12 '21 at 08:49
  • https://tools.ietf.org/html/rfc4648#section-5 this implementation in need in .NetCore – Shazia Tabassum Mar 12 '21 at 11:12
  • Just google C# and base64url: https://stackoverflow.com/a/26354677/9014097 – Topaco Mar 12 '21 at 11:23
  • The issue is resolved now. Basically, after decoding the string, when I convert the byte[] into string, the original characters lost their shape. I used the same method for decoding, and after getting the byte[], I wrote it directly to a file and PGP decryption method directly read it from file. It worked like a charm !! – Shazia Tabassum Mar 12 '21 at 19:23

0 Answers0