Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the OpenSSL.Net C# wrapper to encrypt a string with AES?

Tags:

c#

openssl

I am trying to send some encrypted data from my SharePoint site to my company's PeopleSoft site. The PeopleSoft folks insist that I have to use the OpenSSL library for my encryption. I have downloaded and installed the OpenSSL.Net project from SourceForge.

For my purposes, I need to simply encrypt a string with AES. I know how to do this with the System.Security.Cryptography library, but am having a very difficult time translating this to the OpenSSL.Net side. Very frustrating, since I can see everything that I think I need in Intellisense!

Does anybody have an example of performing string encryption/decryption with AES using the OpenSSL.Net wrapper?

Thanks!

-Nick

like image 593
user266382 Avatar asked Feb 04 '10 17:02

user266382


People also ask

Does .NET use OpenSSL?

NET Core for Linux, which works across multiple Linux distributions, can support both OpenSSL 1.0. x and OpenSSL 1.1. x. . NET Core 2.1 and .

What is OpenSSL in C?

OpenSSL contains an open-source implementation of the SSL and TLS protocols. The core library, written in the C programming language, implements basic cryptographic functions and provides various utility functions. Wrappers allowing the use of the OpenSSL library in a variety of computer languages are available.

Does .NET core use OpenSSL?

NET Core uses OpenSSL on macOS , a dependency that must be installed separately. OpenSSL added support for TLS 1.2 in version 1.0. 1, and added support for TLS 1.3 in version 1.1.

What is OpenSSL version?

OpenSSL Releases The format of the version provides a lot of information. The position of the numbers represent the release type: Major Releases – You can recognize a major release if one or both of the first two digits change. This type of release can break compatibility with previous versions. For example: 1.1.


1 Answers

Here is the sample which works for me. I simplified it by using copy-paste but should not matter.

I'm using text password due to compatibility with JS library but open SSL itself supports direct usage of byte[] Key and IV so it's up to you what to use.

In order to switch binary data into the string just use

Encoding.UTF8.GetBytes() and Encoding.UTF8.GetString()

to convert back and forth.

    public Byte[] Encrypt(Byte[] data, String password)
    {
        //Just random 8 bytes for salt
        var salt = new Byte[] {1, 2, 3, 4, 5, 6, 7, 8};

        using (var cc = new CipherContext(Cipher.AES_256_CBC))
        {
            //Constructing key and init vector from string password
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            byte[] iv;
            byte[] key = cc.BytesToKey(MessageDigest.MD5, salt, passwordBytes, 1, out iv);

            var memoryStream = new MemoryStream();

            //Performing encryption thru unmanaged wrapper
            var aesData = cc.Crypt(data, key, iv, true);

            //Append salt so final data will look Salted___SALT|RESTOFTHEDATA
            memoryStream.Write(Encoding.UTF8.GetBytes("Salted__"), 0, 8);
            memoryStream.Write(salt, 0, 8);
            memoryStream.Write(aesData, 0, aesData.Length);

            return memoryStream.ToArray();
        }
    }

    public Byte[] Decrypt(String password, Byte[] encryptedData)
    {
        byte[] salt = null;
        //extracting salt if presented
        if (encryptedData.Length > 16)
        {
            if (Encoding.UTF8.GetString(encryptedData).StartsWith("Salted__"))
            {
                salt = new Byte[8];
                Buffer.BlockCopy(encryptedData, 8, salt, 0, 8);
            }
        }

        //Removing salt from the original array
        int aesDataLength = encryptedData.Length - 16;
        byte[] aesData = new byte[aesDataLength];
        Buffer.BlockCopy(encryptedData, 16, aesData, 0, aesDataLength);


        using (var cc = new CipherContext(Cipher.AES_256_CBC))
        {
            //Constructing key and init vector from string password and salt
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            byte[] iv;
            byte[] key = cc.BytesToKey(MessageDigest.MD5, salt, passwordBytes, 1, out iv);

            //Decrypting
            return cc.Decrypt(aesData, key, iv, 0);

        }


    }
like image 119
Alex Krupnov Avatar answered Oct 17 '22 22:10

Alex Krupnov