Net 6.0: "AesManaged" is outdated: 'Derived cryptographic types are obsolete. Use the Create method on the base type instead.'



  • The distinguished community!

    Help me improve the code! For the encryption of lines (to maintain confidential data in the common database), use class:

    public static class StringEncriptor
        {
            private static readonly byte[] KEY = Enumerable.Range(0, 32).Select(x => (byte)x).ToArray();
    
        public static string Encrypt (string text)
        {
            using AesManaged aes = new() { Key = KEY };
            using MemoryStream ms = new();
            ms.Write(aes.IV);
            using (CryptoStream cs = new(ms, aes.CreateEncryptor(), CryptoStreamMode.Write, true))
            {
                cs.Write(Encoding.UTF8.GetBytes(text));
            }
            return Convert.ToBase64String(ms.ToArray());
        }
    
        public static string Decrypt (string base64)
        {
            using MemoryStream ms = new(Convert.FromBase64String(base64));
            byte[] iv = new byte[16];
            ms.Read(iv);
            using AesManaged aes = new() { Key = KEY, IV = iv };
            using CryptoStream cs = new(ms, aes.CreateDecryptor(), CryptoStreamMode.Read, true);
            using MemoryStream output = new();
            cs.CopyTo(output);
            return Encoding.UTF8.GetString(output.ToArray());
        }
    }
    

    He did good work and performed his full functions at Net 5.0. With release .Net 6.0, he decided to go to it (the draft was still at an early stage, so it was real). And now he's faced with this warning:

    SYSLIB0021  "AesManaged" является устаревшим: 'Derived cryptographic types are obsolete. Use the Create method on the base type instead.'

    It's clear that Create's method should be used. Help me get this straight. ♪ ♪

    Thank you!



  • Thank you very much for the answer. He offered the right option that I share:

    public static class StringEncriptor
    {
        private static readonly byte[] KEY = Enumerable.Range(0, 32).Select(x => (byte)x).ToArray();
    
    public static string Encrypt (string text)
    {
        using Aes aes = Aes.Create("AesManaged");
        aes.Key = KEY;
        using MemoryStream ms = new();
        ms.Write(aes.IV);
        using (CryptoStream cs = new(ms, aes.CreateEncryptor(), CryptoStreamMode.Write, true))
        {
            cs.Write(Encoding.UTF8.GetBytes(text));
        }
        return Convert.ToBase64String(ms.ToArray());
    }
    
    public static string Decrypt (string base64)
    {
        using MemoryStream ms = new(Convert.FromBase64String(base64));
        byte[] iv = new byte[16];
        ms.Read(iv);
        using Aes aes = Aes.Create("AesManaged");
        aes.Key = KEY;
        aes.IV = iv;
        using CryptoStream cs = new(ms, aes.CreateDecryptor(), CryptoStreamMode.Read, true);
        using MemoryStream output = new();
        cs.CopyTo(output);
        return Encoding.UTF8.GetString(output.ToArray());
    }
    

    }


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2