C# Encrypt data with BouncyCastle and upload it anonymously

ihor.eth
3 min readApr 26, 2020

“Arguing that you don’t care about the right to privacy because you have nothing to hide is no different than saying you don’t care about free speech because you have nothing to say” — E. Snowden

I’ve always thought of privacy be it online or not as of a RIGHT and not a privilege. It doesn’t matter if you have something to hide or not, it shouldn’t limit one’s ability to utilize this right. In the age when 80% of the apps on your phone harvest data on everything you look at, type or say, on every place you visit, on every purchase you make there is always a snooping eye upon us recording every action in the face of Uncle G, NSA and everyone in between. If you share this view please consider supporting Electronic Frontier Foundation.

COVID19 & Me

As one of my friends put it: “The virus couldn’t get to our lungs so it went after our jobs”. I too was eventually laid off so with all the free time I had on my hands I started going thru my personal projects backlog and one of the items was to develop a small C# program to encrypt some data and post it online anonymously.

Prerequisites

There are a lot of sources the data to encrypt can come from but for this particular example, I’ll assume we have some file (.txt or other). My workhorse runs on Windows so I use Gpg4win to generate my PGP keys. Kleopatra, a certificate manager for OpenPGP and X.509 (S/MIME), and common crypto dialogs will be installed as part of the package and that’s what you use to generate your personal OpenPGP key pair. This step is straightforward so I won’t go into detail here.

The Code

We’ll be using the BouncyCastle NuGet package to encrypt files. Here is what the helper class could look like

Encryption Helper

where input path would be a path to the file you want to encrypt and publicKeyPath is the path to the physical .asc key file you created with Kleopatra. The calling method could look like this

public byte[] EncryptData(string inputFilePath, string keyPath){MemoryStream ms = new MemoryStream();EncryptionHelper.EncryptFile(inputFilePath, ms, keyPath);byte[] result = ms.ToArray();ms.Dispose();
return result;
}

To upload the file I’ve used anonymous files API, and the code to do that could look like this

Upload function

This call will return the URL to the file you’ve just uploaded and some other information. After following the URL and downloading the file we can go back to Kleopatra, hit Decrypt/Verify, select the downloaded file, enter the password you came up with when creating a key pair, and voila: the data that looked like this

Encrypted text without armor

is back to a human-readable format.

The End

If you liked the article please don’t forget to put your hands together and if you have questions feel free to comment. Also, if you have the means please consider donating to EFF, Gpg4win, and BouncyCastle. Cheers!

--

--

ihor.eth

Full-stack web and blockchain developer passionate about technology. Top Rated Freelancer, owner @ codespaceinc.co and zerocodenft.com