{"id":116968,"date":"2024-06-06T07:18:52","date_gmt":"2024-06-06T05:18:52","guid":{"rendered":"https:\/\/code-maze.com\/?p=116968"},"modified":"2024-06-06T07:18:52","modified_gmt":"2024-06-06T05:18:52","slug":"csharp-bouncy-castle-cryptography","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/","title":{"rendered":"Bouncy Castle Cryptography Library for .NET"},"content":{"rendered":"<p>Cryptography remains an ever-important topic in building modern security-minded applications. Bouncy Castle is a cryptography library for .NET that allows users to build robust cryptography features. In this article, we will cover just a few important capabilities Bouncy Castle offers us and how to implement them.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/authorization-dotnet\/BouncyCastleCryptography\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s get started!<\/p>\n<h2>What is Bouncy Castle?<\/h2>\n<p><strong>Bouncy Castle is a free open-source .NET library offering a wide cryptography feature set for building security into applications<\/strong>. Bouncy Castle is developed by the Legion of the Bouncy Castle, an Australian Charity. This library offers a wide array of basic and advanced cryptographic algorithms. Additionally, it offers capabilities to generate and verify cryptographic certificates.<\/p>\n<p>To learn more about Bouncy Castle and the Legion of the Bouncy Castle visit their website <a href=\"https:\/\/www.bouncycastle.org\/\" target=\"_blank\" rel=\"nofollow noopener\">here<\/a>. For a refresher on cryptography refer to\u00a0<a href=\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/\" target=\"_blank\" rel=\"noopener\">this article<\/a> on CodeMaze.<\/p>\n<h2>Installing Bouncy Castle<\/h2>\n<p>We can install Bouncy Castle via the .NET command line:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">dotnet add package BouncyCastle.Cryptography<\/code><\/p>\n<h2>Hashing With Bouncy Castle<\/h2>\n<p><strong>Hashing functions are deterministic cryptographic algorithms that can map a data set down to a fixed-size byte array<\/strong>. This process is irreversible which lends itself well to validating data has not been changed without the risk of divulging the data itself. We can use Bouncy Castle to hash data using various algorithms such as MD5 and SHA.<\/p>\n<p><strong>One real-world use of hashing is a publisher providing the hashed value of the binary of an application.<\/strong> The user downloading these binaries can then verify the file(s) have not been altered in any way by verifying the hash provided by the publisher. Because hashing is deterministic we expect that performing the same hashing algorithm on these files will produce a matching hash to the one the publisher has provided.<\/p>\n<h3>Md5 Hashing<\/h3>\n<p>Let&#8217;s begin with performing an MD5 hash with Bouncy Castle:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var secretValue = \"This is my pasword! Dont read me!\";\r\n\r\nvar hash = Md5Hasher.Md5Hash(secretValue);\r\n\r\npublic static byte[] Md5Hash(string secret)\r\n{\r\n    var hash = new MD5Digest();\r\n\r\n    var result = new byte[hash.GetDigestSize()];\r\n\r\n    var data = Encoding.UTF8.GetBytes(secret);\r\n\r\n    hash.BlockUpdate(data, 0, data.Length);\r\n\r\n    hash.DoFinal(result,\u00a00);\r\n\r\n    return result;\r\n}<\/pre>\n<p>To perform an MD5 hash, first, we create an <code>MD5Digest<\/code> object that we will use to create the hash. Next, we create a byte array, <code>result<\/code>, the size of the digest. We then convert the secret value to UTF-8 encoded byte array. Next, we update the hash digest object with a byte array. We do this by calling <code>BlockUpdate()<\/code> with blocks of bytes the size of our byte array. Finally, we call <code>DoFinal()<\/code> on the digest object to produce the final hash.<\/p>\n<p>This will produce a hash of our secret:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">U0e8NZgYzVdAFWH0+\/Wwvw==<\/code><\/p>\n<h3>SHA Hashing<\/h3>\n<p>Let&#8217;s move on to SHA hashing.\u00a0<\/p>\n<p>We can use Bouncy Castle to perform a SHA256 hash:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var secretValue = \"This is my pasword! Dont read me!\";\r\n\r\nvar hash = ShaHasher.ShaHash(secretValue);\r\n\r\npublic static byte[] ShaHash(string secret)\r\n{\r\n    var hash = new Sha256Digest();\r\n\r\n    var result = new byte[hash.GetDigestSize()];\r\n\r\n    var data = Encoding.UTF8.GetBytes(secret);\r\n\r\n    hash.BlockUpdate(data, 0, data.Length);\r\n\r\n    hash.DoFinal(result, 0);\r\n\r\n    return result;\r\n}<\/pre>\n<p>As we are using Bouncy Castle the code is very similar to creating an MD5 hash although they are different algorithms altogether. In this case, we create a <code>Sha256Digest<\/code> object to perform the hashing. Similarly, we convert the secret into a byte array. We then load the byte array in the hash block. Lastly, like before, we call <code>DoFinal()<\/code> on the digest object to produce our final hash.<\/p>\n<p>This is an example of what a SHA256 hashed value would look like:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">n0xnEbCTlQ66Un8K2G8XxvZPlWo0GizTISE\/BTYkV3Q=<\/code><\/p>\n<h2>Symmetric Encryption with Bouncy Castle<\/h2>\n<p>In the following sections we will discuss how to perform specific symmetric encryption algorithms, but let&#8217;s discuss what options Bouncy Castle offers. Symmetric encryption always begins by creating a cipher object. We use the <code>CipherUtilities.GetCipher(\"AlgorithmName\/OperationMode\/PaddingStyle\")<\/code> method to make this cipher object. This method accepts a string with three sections that are delimited by a forward slash. The first section describes the algorithm the cipher should use. The second section describes the operation mode for the cipher. Last, the final section describes which padding style the cipher should use.<\/p>\n<p>Let&#8217;s take a look at the options we can choose from for each of these sections:<\/p>\n<ol>\n<li>\n<p><strong>Algorithm<\/strong>:<\/p>\n<ul style=\"list-style-type: disc;\">\n<li>AES: <code>\"AES\"<\/code><\/li>\n<li>DES: <code>\"DES\"<\/code><\/li>\n<li>Triple DES (DESede): <code>\"DESede\"<\/code>, <code>\"3DES\"<\/code><\/li>\n<li>Blowfish: <code>\"Blowfish\"<\/code><\/li>\n<li>CAST-5: <code>\"CAST5\"<\/code><\/li>\n<li>IDEA: <code>\"IDEA\"<\/code><\/li>\n<li>Camellia: <code>\"Camellia\"<\/code><\/li>\n<li>SEED: <code>\"SEED\"<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Mode of Operation<\/strong>:<\/p>\n<ul style=\"list-style-type: disc;\">\n<li>Electronic Codebook (ECB): <code>\"ECB\"<\/code><\/li>\n<li>Cipher Block Chaining (CBC): <code>\"CBC\"<\/code><\/li>\n<li>Cipher Feedback (CFB): <code>\"CFB\"<\/code><\/li>\n<li>Output Feedback (OFB): <code>\"OFB\"<\/code><\/li>\n<li>Counter (CTR): <code>\"CTR\"<\/code><\/li>\n<li>Galois\/Counter Mode (GCM): <code>\"GCM\"<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Padding Style<\/strong>:<\/p>\n<ul style=\"list-style-type: disc;\">\n<li>NoPadding: <code>\"NoPadding\"<\/code><\/li>\n<li>PKCS5Padding: <code>\"PKCS5Padding\"<\/code><\/li>\n<li>PKCS7Padding: <code>\"PKCS7Padding\"<\/code><\/li>\n<li>ISO10126Padding: <code>\"ISO10126Padding\"<\/code><\/li>\n<li>ISO7816d4Padding: <code>\"ISO7816-4Padding\"<\/code><\/li>\n<li>TBCPadding: <code>\"TBCPadding\"<\/code><\/li>\n<li>X923Padding: <code>\"X9.23Padding\"<\/code><\/li>\n<li>ZeroBytePadding: <code>\"ZeroBytePadding\"<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>Symmetric encryption, also known as private key encryption<\/strong>, is a class of encryption algorithms <strong>that use a single private key to perform encryption and decryption<\/strong> <strong>of a data set<\/strong>. This means the party that wants to decrypt data can only do so if they have obtained this key from whoever encrypted the data. Bouncy Castle allows us to easily use various symmetric encryption algorithms to protect data. In this section, we will discuss two well-known symmetric encryption algorithms: AES, and Blowfish.<\/p>\n<h3>AES Encryption<\/h3>\n<p>First, let&#8217;s take a look at a code example of how to perform AES encryption:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">string input = \"Hello, Bouncy Castle!\";\r\n\r\nvar encryptedData = AesEncryptor.AesEncrypt(input, out byte[] iv, out byte[] key);\r\n\r\npublic static byte[] AesEncrypt(string input, out byte[] iv, out byte[] key)\r\n{\r\n    var inputBytes = Encoding.UTF8.GetBytes(input);\r\n\r\n    \/\/ Generate a random 128-bit key\r\n    key = new byte[16];\r\n    var random = new SecureRandom();\r\n    random.NextBytes(key);\r\n\r\n    \/\/ Generate a random 128-bit initialization vector (IV)\r\n    iv = new byte[16];\r\n    random.NextBytes(iv);\r\n\r\n    \/\/ Create AES cipher with CBC mode\r\n    var cipher = CipherUtilities.GetCipher(\"AES\/CBC\/PKCS7Padding\");\r\n\r\n    cipher.Init(true, new ParametersWithIV(new KeyParameter(key), iv));\r\n\r\n    return cipher.DoFinal(inputBytes);\r\n}\r\n\r\n<\/pre>\n<p>We begin by transforming our input data set into a byte array using <code>UTF8.GetBytes()<\/code>. Next, we create two <strong>randomly generated<\/strong> byte arrays for the key and initialization vector (IV). Now that we have a key and IV, we generate a cipher object and initialize it with our key and IV. The first parameter of <code>Init()<\/code> is a boolean that indicates if we are encrypting. Finally, we call the <code>DoFinal()<\/code> method on the cipher object to produce the encrypted data.<\/p>\n<p>Now that we have encrypted data, let&#8217;s take a look at how we can perform decryption using Bouncy Castle to recover the original data:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static string AesDecrypt(byte[] key, byte[] iv, byte[] encryptedBytes)\r\n{\r\n    var cipher = CipherUtilities.GetCipher(\"AES\/CBC\/PKCS7Padding\");\r\n    cipher.Init(false, new ParametersWithIV(new KeyParameter(key), iv));\r\n\r\n    \/\/ Decrypt the encrypted data\r\n    var decryptedBytes = cipher.DoFinal(encryptedBytes);\r\n\r\n    return Encoding.UTF8.GetString(decryptedBytes);\r\n}<\/pre>\n<p>We start by creating a cipher object with the <strong>same algorithm and mode<\/strong> used to encrypt the data. This is very important because if the cipher is not the same the decryption would not work. Next, we initialize the cipher with the <strong>same IV and key as the encryption<\/strong>. Additionally, we provide a <code>false<\/code> value in the <code>Init()<\/code> method to indicate we will be decrypting.<\/p>\n<h3>Blowfish Encryption<\/h3>\n<p>Moving on, let&#8217;s take a look at how we can encrypt data using Blowfish:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var input = \"Hello, Bouncy Castle!\";\r\nvar password = \"mysecretpassword\";\r\n\r\nvar encryptedBytes = BlowfishEncryptor.BlowfishEncrypt(input, password, out byte[] iv);\r\n\r\npublic static byte[] BlowfishEncrypt(string input, string password, out byte[] iv)\r\n{\r\n    var inputBytes = Encoding.UTF8.GetBytes(input);\r\n\r\n    var random = new SecureRandom();\r\n    iv = new byte[8]; \/\/ Blowfish uses an 8-byte (64-bit) IV\r\n    random.NextBytes(iv);\r\n\r\n    var cipher = CipherUtilities.GetCipher(\"Blowfish\/CBC\/PKCS7Padding\");\r\n\r\n    var keyBytes = Encoding.UTF8.GetBytes(password);\r\n    var keyParam = new KeyParameter(keyBytes);\r\n\r\n    cipher.Init(true, new ParametersWithIV(keyParam, iv));\r\n\r\n    return cipher.DoFinal(inputBytes);\r\n}<\/pre>\n<p>Blowfish uses <strong>password-based encryption<\/strong> which means we use a password to generate a key. In the example, we generate a random IV and a key using the password as an input to the <code>KeyParameter()<\/code> constructor. Next, we create a cipher object using <code>GetCipher()<\/code> and we specify we want to use Blowfish with CBC mode and PKCS7 padding. Again, we initialize the cipher set to encryption. Finally, we produce the final encryption by calling <code>DoFinal()<\/code>.<\/p>\n<p>Lastly, let&#8217;s take a look at how we can decrypt a data set we encrypted using Blowfish:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static string BlowfishDecrypt(byte[] encryptedBytes, string password, byte[] iv)\r\n{\r\n    var cipher = CipherUtilities.GetCipher(\"Blowfish\/CBC\/PKCS7Padding\");\r\n\r\n    var keyBytes = Encoding.UTF8.GetBytes(password);\r\n    var keyParam = new KeyParameter(keyBytes);\r\n\r\n    cipher.Init(false, new ParametersWithIV(keyParam, iv));\r\n\r\n    var inputBytes = cipher.DoFinal(encryptedBytes);\r\n\r\n    return Encoding.UTF8.GetString(inputBytes);\r\n}<\/pre>\n<p>To perform the decryption, we initialize a new cipher with the same settings as we did when it was encrypted and set it to decryption mode. Next, we generate another <code>KeyParameter<\/code> object using the password byte array. Finally, we call <code>DoFinal()<\/code> to receive our original data back.<\/p>\n<h2>Asymmetric Encryption With Bouncy Castle<\/h2>\n<p><strong>Asymmetric encryption is also known as public key encryption<\/strong>. They are a class of encryption algorithms <strong>that use a public key to encrypt data<\/strong> and a <strong>private key to perform decryption<\/strong> of a data set. The public and private keys are mathematically related. Only a private key related to the public key can decrypt the data set. Bouncy Castle enables us to perform asymmetric encryptions. In the following section, we will cover two asymmetric encryption algorithms: RSA, and DSA. It should be noted that DSA is typically used to digitally sign files as a security measure to prove files have not been tempered while exchanging between two parties.<\/p>\n<h3>RSA Encryption<\/h3>\n<p>Here is how we can perform RSA encryption using Bouncy Castle:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">string input = \"Hello, Bouncy Castle!\";\r\n\r\n\/\/ Generate RSA key pair\r\nvar keyPair = RsaEncryptor.GenerateRsaKeyPair();\r\n\r\nvar encryptedBytes = RsaEncryptor.RsaEncrypt(input, keyPair.Public);\r\n\r\npublic static AsymmetricCipherKeyPair GenerateRsaKeyPair()\r\n{\r\n    var rsaKeyPairGen = new RsaKeyPairGenerator();\r\n    rsaKeyPairGen.Init(new KeyGenerationParameters(new SecureRandom(), 2048));\r\n    return rsaKeyPairGen.GenerateKeyPair();\r\n}\r\n\r\npublic static byte[] RsaEncrypt(string input, AsymmetricKeyParameter publicKey)\r\n{\r\n    var inputBytes = Encoding.UTF8.GetBytes(input);\r\n\r\n    var cipher = new Pkcs1Encoding(new RsaEngine());\r\n\r\n    cipher.Init(true, publicKey);\r\n\r\n    return cipher.ProcessBlock(inputBytes, 0, inputBytes.Length);\r\n}<\/pre>\n<p>Here we see the pattern for asymmetric encryption is different from symmetric encryption. First, we generate a key pair by creating a <code>RsaKeyPairGenerator<\/code> object. We initialize it with a random number generator of type <code>SecureRandom<\/code> and an <code>int<\/code> indicating the strength of the encryption using a bit length number. Lastly, we call <code>GenerateKeyPair()<\/code> to produce the private and public keys.<\/p>\n<p>Now that we have a key pair, we create a cipher object by providing an <code>RsaEngine<\/code> object to a <code>PKCS1Encoding<\/code> object. Next, we initialize the cipher with the encryption mode as <code>true<\/code> and the public key we generated. Finally, we call ProcessBlock() with our input byte array to encrypt, an offset value for where to start reading our input, and the length of the input. This will produce an encrypted byte array representation of our input data.<\/p>\n<p>Now, let&#8217;s discuss how we can recover our data through decryption:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">string decryptedString = RsaEncryptor.RsaDecrypt(encryptedBytes, keyPair.Private);\r\n\r\npublic static string RsaDecrypt(byte[] encryptedBytes, AsymmetricKeyParameter privateKey)\r\n{\r\n    var cipher = new Pkcs1Encoding(new RsaEngine());\r\n\r\n    \/\/ Initialize the cipher for decryption with the private key\r\n    cipher.Init(false, privateKey);\r\n\r\n    \/\/ Decrypt the encrypted data\r\n    var decryptedBytes = cipher.ProcessBlock(encryptedBytes, 0, encryptedBytes.Length);\r\n\r\n    return Encoding.UTF8.GetString(decryptedBytes);\r\n}<\/pre>\n<p>Again, we create a cipher with the <strong>same algorithm engine object and padding style<\/strong> as used in encryption. Next, we initialize the cipher in decryption mode and <strong>pass in the private key<\/strong> this time. Now, we call the <code>ProcessBlock()<\/code> method again to produce the original byte array data.<\/p>\n<h3>Creating Digital Signatures<\/h3>\n<p><strong>Another application of asymmetric encryption is digital signatures used for secure file transfers.<\/strong> In this section, we will use the DSA algorithm to achieve this goal.<\/p>\n<p>Let&#8217;s start by looking at an example of how we can digitally sign a byte array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">string input = \"Hello, Bouncy Castle!\";\r\n\r\nvar keyPair = DsaEncryptor.GenerateDsaKeyPair();\r\n\r\nvar signature = DsaEncryptor.DsaSign(input, keyPair.Private);\r\n\r\npublic static AsymmetricCipherKeyPair GenerateDsaKeyPair()\r\n{\r\n    \/\/ Create DSA parameters\r\n    var dsaParamsGenerator = new DsaParametersGenerator();\r\n    var random = new SecureRandom();\r\n    dsaParamsGenerator.Init(1024, 80, random); \/\/ key size: 1024 bits, certainty: 80\r\n\r\n    \/\/ Generate DSA key pair\r\n    var dsaParams = dsaParamsGenerator.GenerateParameters();\r\n    var dsaKeyParams = new DsaKeyGenerationParameters(random, dsaParams);\r\n    var dsaKeyPairGen = new DsaKeyPairGenerator();\r\n    dsaKeyPairGen.Init(dsaKeyParams);\r\n\r\n    return dsaKeyPairGen.GenerateKeyPair();\r\n}\r\n\r\npublic static byte[] DsaSign(string message, AsymmetricKeyParameter privateKey)\r\n{\r\n    var messageBytes = Encoding.UTF8.GetBytes(message);\r\n\r\n    var signer = SignerUtilities.GetSigner(\"SHA256withDSA\");\r\n\r\n    signer.Init(true, privateKey);\r\n\r\n    signer.BlockUpdate(messageBytes, 0, messageBytes.Length);\r\n\r\n    return signer.GenerateSignature();\r\n}<\/pre>\n<p>In this example, we are signing a byte array produced from a string, but in a practical example, we would read in a file as a byte array and perform all the same operations on it.<\/p>\n<p>Firstly, we start by creating a key pair we can use for signing. Create a <code>DsaParametersGenerator<\/code> object and initialize it with a secure random number generator, key size, and a certainty value. Next, we generate the parameters by calling <code>GenerateParameters()<\/code> and pass them into the constructor of <code>DsaKeyGenerationParameters<\/code>. We follow this up by creating a <code>DsaKeyPairGenerator<\/code> object which we initialize with the previously created <code>DsaKeyGenerationParameters<\/code>. Lastly, we generate the keys by calling <code>GenerateKeyPair()<\/code>.<\/p>\n<p>Now that we have the DSA keys we can begin the signing process. Starting off, we create an <code>ISigner<\/code> implementation by calling <code>SignerUtilities.GetSigner(\"SHA256withDSA\")<\/code>. We initialize the signer by calling <code>Init()<\/code> and indicating we are signing with the <code>true<\/code> value and our private key. Once we have initialized the signer, we call <code>BlockUpdate()<\/code> to pass in our data, an offset value, and the data array length. We produce the signature by calling <code>GenerateSignature()<\/code>.\u00a0 It is important to note that the original data has not been obscured.<\/p>\n<h3>Validating Digital Signatures<\/h3>\n<p>We can also verify the signature on the data using Bouncy Castle:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var isSignatureValid = DsaEncryptor.DsaVerify(input, signature, keyPair.Public);\r\n\r\npublic static bool DsaVerify(string message, byte[] signature, AsymmetricKeyParameter publicKey)\r\n{\r\n    var messageBytes = Encoding.UTF8.GetBytes(message);\r\n\r\n    var signer = SignerUtilities.GetSigner(\"SHA256withDSA\");\r\n\r\n    signer.Init(false, publicKey);\r\n\r\n    signer.BlockUpdate(messageBytes, 0, messageBytes.Length);\r\n\r\n    return signer.VerifySignature(signature);\r\n}<\/pre>\n<p>Begin by creating another <code>ISigner<\/code> implementation with the same parameters as the one used to produce the signature. We initialize it with a <code>false<\/code> value and pass in the public key we generated before. We call <code>BlockUpdate()<\/code> on the signed passing in the input data, an offset, and the data array length. Lastly, we verify the signature by calling <code>VerifySignature()<\/code> and passing in the signature we produced.<\/p>\n<h2>Conclusion<\/h2>\n<p>Bouncy Castle allows us to perform a variety of cryptographic operations with an easy-to-use and consistent syntax. Data security remains an ever-important aspect of development. This is a great library to enable data security in applications!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cryptography remains an ever-important topic in building modern security-minded applications. Bouncy Castle is a cryptography library for .NET that allows users to build robust cryptography features. In this article, we will cover just a few important capabilities Bouncy Castle offers us and how to implement them. Let&#8217;s get started! What is Bouncy Castle? Bouncy Castle [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[1582,2206,1811,1577,613,1580],"class_list":["post-116968","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-asymmetric-encryption-in-cryptography","tag-bouncy-castle-cryptography","tag-c","tag-cryptography","tag-encryption","tag-symmetric-encryption-in-cryptography","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Bouncy Castle Cryptography Library for .NET - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we will learn Bouncy Castle Cryptography library in .NET with some examples of using it for Encryption, Decryption, Hashing.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bouncy Castle Cryptography Library for .NET - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we will learn Bouncy Castle Cryptography library in .NET with some examples of using it for Encryption, Decryption, Hashing.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-06T05:18:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Alvaro Montoya\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alvaro Montoya\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/\"},\"author\":{\"name\":\"Alvaro Montoya\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/78b0d58c590fbaaac88b126b3c588f4e\"},\"headline\":\"Bouncy Castle Cryptography Library for .NET\",\"datePublished\":\"2024-06-06T05:18:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/\"},\"wordCount\":1730,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"Asymmetric Encryption in Cryptography\",\"Bouncy Castle Cryptography\",\"C#\",\"Cryptography\",\"encryption\",\"Symmetric Encryption in Cryptography\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/\",\"url\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/\",\"name\":\"Bouncy Castle Cryptography Library for .NET - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2024-06-06T05:18:52+00:00\",\"description\":\"In this article, we will learn Bouncy Castle Cryptography library in .NET with some examples of using it for Encryption, Decryption, Hashing.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bouncy Castle Cryptography Library for .NET\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/78b0d58c590fbaaac88b126b3c588f4e\",\"name\":\"Alvaro Montoya\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Alvaro-Montoya-400.jpg-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Alvaro-Montoya-400.jpg-150x150.png\",\"caption\":\"Alvaro Montoya\"},\"description\":\"Alvaro has been professionally developing solutions in .NET for over eight years. He has worked in simulations, flight planning, and most recently a cloud-based solution for healthcare prior authorization. Over the years, He has worked on developing desktop applications, web applications, .NET APIs, and cloud-based distributed solutions in Azure and AWS. Outside of work, Alvaro enjoys playing video games and playing basketball.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/alvaro-montoya-096589108\/\"],\"url\":\"https:\/\/code-maze.com\/author\/alvaromontoya\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bouncy Castle Cryptography Library for .NET - Code Maze","description":"In this article, we will learn Bouncy Castle Cryptography library in .NET with some examples of using it for Encryption, Decryption, Hashing.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/","og_locale":"en_US","og_type":"article","og_title":"Bouncy Castle Cryptography Library for .NET - Code Maze","og_description":"In this article, we will learn Bouncy Castle Cryptography library in .NET with some examples of using it for Encryption, Decryption, Hashing.","og_url":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/","og_site_name":"Code Maze","article_published_time":"2024-06-06T05:18:52+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Alvaro Montoya","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Alvaro Montoya","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/"},"author":{"name":"Alvaro Montoya","@id":"https:\/\/code-maze.com\/#\/schema\/person\/78b0d58c590fbaaac88b126b3c588f4e"},"headline":"Bouncy Castle Cryptography Library for .NET","datePublished":"2024-06-06T05:18:52+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/"},"wordCount":1730,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["Asymmetric Encryption in Cryptography","Bouncy Castle Cryptography","C#","Cryptography","encryption","Symmetric Encryption in Cryptography"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/","url":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/","name":"Bouncy Castle Cryptography Library for .NET - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2024-06-06T05:18:52+00:00","description":"In this article, we will learn Bouncy Castle Cryptography library in .NET with some examples of using it for Encryption, Decryption, Hashing.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-bouncy-castle-cryptography\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Bouncy Castle Cryptography Library for .NET"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/78b0d58c590fbaaac88b126b3c588f4e","name":"Alvaro Montoya","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Alvaro-Montoya-400.jpg-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Alvaro-Montoya-400.jpg-150x150.png","caption":"Alvaro Montoya"},"description":"Alvaro has been professionally developing solutions in .NET for over eight years. He has worked in simulations, flight planning, and most recently a cloud-based solution for healthcare prior authorization. Over the years, He has worked on developing desktop applications, web applications, .NET APIs, and cloud-based distributed solutions in Azure and AWS. Outside of work, Alvaro enjoys playing video games and playing basketball.","sameAs":["https:\/\/www.linkedin.com\/in\/alvaro-montoya-096589108\/"],"url":"https:\/\/code-maze.com\/author\/alvaromontoya\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/116968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=116968"}],"version-history":[{"count":1,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/116968\/revisions"}],"predecessor-version":[{"id":116969,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/116968\/revisions\/116969"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=116968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=116968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=116968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}