如何使用Rijndael加密与.Net Core类库? (不是.Net框架) [英] How to use Rijndael encryption with a .Net Core class library? (Not .Net Framework)

查看:1865
本文介绍了如何使用Rijndael加密与.Net Core类库? (不是.Net框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在.Net Core类库中使用Rijndael加密? (不是.Net框架类库)我们需要创建一个共享的.Net Core库用于多个项目,并需要实现在项目中使用相同Rijndael加密的Encrypt和Decrypt方法。

How do we use Rijndael encryption in a .Net Core class library? (Not a .Net Framework Class Library) We need to create a shared .Net Core library for use in multiple projects and need to implement Encrypt and Decrypt methods that use the same Rijndael encryption across the projects.

我们目前正在使用:


  • VS Enterprise 2015

  • c# / li>
  • .Net核心类库

  • .NETStandard,版本= v1.6参考

看起来Rijndael和AES的实现在.Net Core 1.0版本中是缺失的......它似乎只包括基类。我们如何获得Rijndael或AES加密的.Net Core实现作为对.Net Core类库项目的引用?

It appears that the implementation of Rijndael and AES is missing from the .Net Core 1.0 release...it seems to only include the base classes. How do we get a .Net Core implementation of Rijndael or AES encryption added as a reference to a new .Net Core Class Library project?

这里是Encrypt方法in .Net Framework 4.5.2:

Here is the Encrypt method that works in .Net Framework 4.5.2:

    public static string Encrypt(string valueToEncrypt, string symmetricKey, string initializationVector)
    {
        string returnValue = valueToEncrypt;

        var aes = new System.Security.Cryptography.RijndaelManaged();
        try
        {
            aes.Key = ASCIIEncoding.ASCII.GetBytes(symmetricKey);
            aes.IV = ASCIIEncoding.ASCII.GetBytes(initializationVector);
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.ISO10126;

            var desEncrypter = aes.CreateEncryptor();
            var buffer = ASCIIEncoding.ASCII.GetBytes(valueToEncrypt);

            returnValue = Convert.ToBase64String(desEncrypter.TransformFinalBlock(buffer, 0, buffer.Length));
        }
        catch (Exception)
        {
            returnValue = string.Empty;
        }

        return returnValue;
    }


推荐答案

)之间Rijndael和AES是Rijndael允许块大小改变,但AES不。由于RijndaelManaged的默认块大小与AES块大小(128位/ 16字节)相同,因此实际上使用AES。

The difference (in .NET) between Rijndael and AES is that Rijndael allows the block size to change, but AES does not. Since RijndaelManaged's default block size is the same as the AES block size (128 bit / 16 byte) you are, in fact, using AES.

而不是实例化实现类型按名称,只需使用工厂( Aes.Create())。在.NET Core和.NET Framework中都有效。

Instead of instantiating the implementation type by name, just use the factory (Aes.Create()). That works in both .NET Core and .NET Framework.

其他值得一提的方法:


  • 所有SymmetricAlgorithm实例都是IDisposable,您应该使用使用语句。

  • 所有ICryptoTransform实例未正确命名为 desEncryptor )为IDisposable,则应在中使用语句。

  • ISO10126填充在.NET Core 1.0中不可用。如果你需要兼容现有的流,你可以自己应用填充和指定PaddingMode.None。否则,PKCS7更标准。

  • 您的AES密钥不是很随机,因为它来自一个ASCII字符串(许多值将无效)。


    • Base64至少具有完整的值范围

    • 通过Rfc2898DeriveBytes类,PBKDF2(基于密码的密钥导出函数2)

    • KeyAgreement通常更好,但是.NET Core 1.0中既不提供ECDH也不提供经典DH。

    • All SymmetricAlgorithm instances are IDisposable, you should use them in a using statement.
    • All ICryptoTransform instances (such as your incorrectly named desEncryptor) are IDisposable, you should use them in a using statement.
    • ISO10126 padding is not available in .NET Core 1.0. If you need to be compatible with existing streams you can apply the padding yourself and specify PaddingMode.None. Otherwise, PKCS7 is more standard.
    • Your AES key isn't very random, since it comes from an ASCII string (lots of values won't be valid).
      • Base64 at least has full value range
      • PBKDF2 (Password-Based Key Derivation Function 2) via the Rfc2898DeriveBytes class allows for shared-string-secret in, predictable noise out.
      • KeyAgreement is in general better, but neither ECDH nor classic DH are available in .NET Core 1.0.

      这篇关于如何使用Rijndael加密与.Net Core类库? (不是.Net框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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