Cryptography / decryption with private key and public key using C #

1 minute read

As the title suggests, implementation of encryption / decryption using C #.

I wanted to use the encryption and decryption functions as easily as possible, so I implemented it.

I referred to the following MS sites.

The environment is VS2019 and uses .NET Core 3.1.

As an image, coding assuming the following exchanges.

(1) The server issues a private key and a public key.
(2) Pass the public key information as a character string to the client.
(3) The client encrypts the data using the public key information and sends it to the server.
(4) The server decrypts the data using the private key information.

It may be good to use a certificate, but I don’t have much knowledge about it, so I wanted to complete everything with just the code …

Source is below. (Click to see the source) </summary> <div>

namespace RsaConsole
    class Program
        static void Main(string[] args)
            //Convert the string you want to encrypt to a byte array
            string encryptWord = "Hiyashi chuka";
            var encByte = Encoding.Unicode.GetBytes(encryptWord);

            //Generate private and public keys
            RSA rsa = RSA.Create();

            //Save key information
            var publicKey = rsa.ToXmlString(false);
            var privateKey = rsa.ToXmlString(true);

            //Restore object from public key information string
            byte[] encryptedByte;
            using (RSA encRsa = RSA.Create())
                encryptedByte = encRsa.Encrypt(encByte, RSAEncryptionPadding.Pkcs1);

            //Convert encrypted byte data to a hexadecimal string
            var encByteToString = BitConverter.ToString(encryptedByte);
            Console.WriteLine($"Encrypt: {encByteToString}");

            //Assuming that the hexadecimal string generated by the above method was received from the outside,
            //Restore using private key

            //Convert the sent character string to a byte array
            var encStrToBytes = encByteToString.Split('-').Select(r => Convert.ToByte(r, 16)).ToArray();
            byte[] decryptedByte;

            //Restores an object from a private key information string and decrypts it
            using (RSA decRsa = RSA.Create())
                decryptedByte = decRsa.Decrypt(encStrToBytes, RSAEncryptionPadding.Pkcs1);

            var decryptedString = Encoding.Unicode.GetString(decryptedByte);

            Console.WriteLine($"Decrypt: {decryptedString}");

            //Try decrypting with a textual key
            var invalidDec = string.Empty;
                using (RSA invalidRsa = RSA.Create())
                    decryptedByte = invalidRsa.Decrypt(encStrToBytes, RSAEncryptionPadding.Pkcs1);
                Console.WriteLine("Failed to decrypt.");



The output looks like this.

Encrypt: 6F-5B-B7-F8-3D-C9-00-23-BE-84-29-82-56-7E-BE-7B-79-E8-BB-5F-76-2B-03-A8-49-1C-76-FB-49-56-04-1E-8A-CA-AD-5C-57-F7-21-CC-17-FE-04-C8-FD-F9-D5-0D-7B-2B-2B-3A-34-A0-AE-5D-82-D3-1E-78-55-22-06-DB-C2-AA-99-A8-D1-52-6A-27-0F-A6-86-D9-CA-DA-78-6F-00-D2-6C-1C-51-F2-88-FA-CC-F8-CD-B0-0D-8E-D1-55-89-53-43-4A-92-25-DD-13-55-8E-F4-95-5D-6E-B3-95-89-3A-DE-6A-A1-A5-89-A9-1F-3F-30-6A-04-D0-B0-4E-CF-CA-6E-03-D9-D7-30-2C-48-31-8A-34-4F-0D-5E-1A-19-CF-61-52-A0-AB-20-C0-69-B6-DE-CE-C7-6C-59-C4-C0-64-2A-05-7B-07-96-A0-77-DD-F3-5E-D0-1C-E8-76-3D-A7-6B-51-8C-BC-50-A9-9D-03-31-1C-36-97-DB-F8-63-55-4F-2C-85-FA-C0-03-C6-C8-BE-93-11-DA-67-CD-55-3E-E0-D3-BC-13-FF-26-FD-A6-F2-87-5E-31-01-C0-4C-9D-6F-6A-45-8F-34-A5-07-6B-C3-A7-19-48-77-89-4E-9F-A5-9C-F2-61-83-CE-3E-66-59-47-56-10-F3-03
Decrypt:Hiyashi chuka
Failed to decrypt.

It can be decrypted properly, and it can not be decrypted with a textual key (naturally)

After that, if you also send a hash of the message, you can also verify the communication partner at once. It’s safe to do that.

that’s all!