如何使用OpenSSL.Net C#包装器通过AES加密字符串? [英] How do I use the OpenSSL.Net C# wrapper to encrypt a string with AES?

查看:82
本文介绍了如何使用OpenSSL.Net C#包装器通过AES加密字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些加密的数据从我的SharePoint网站发送到公司的PeopleSoft网站. PeopleSoft人士坚持认为,我必须使用OpenSSL库进行加密.我已经从SourceForge下载并安装了OpenSSL.Net项目.

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.

出于我的目的,我需要简单地使用AES加密字符串.我知道如何使用System.Security.Cryptography库执行此操作,但是将其转换到OpenSSL.Net端却遇到了非常困难的时间.非常令人沮丧,因为我可以在Intellisense中看到我认为需要的一切!

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!

有人有使用OpenSSL.Net包装器通过AES执行字符串加密/解密的示例吗?

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

谢谢!

-尼克

推荐答案

以下是对我有用的示例.我通过使用复制粘贴功能对其进行了简化,但这并不重要.

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

由于与JS库的兼容性,我使用的是文本密码,但是开放SSL本身支持直接使用byte [] Key和IV,因此由您决定使用什么.

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()

来回转换.

    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);

        }


    }

这篇关于如何使用OpenSSL.Net C#包装器通过AES加密字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆