Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

256 Bit Encryption in .NET Core 5

I have been using AES for 256 Bit encryption in the .NET framework for quite a few years. I recently started a new project in .NET Core 5 and keep getting and error stating that only 128 bit keys are supported in this installation. After a bit of research it seems that .NET Core only supports 128 bit keys now.

My problem is that I already have mountains of data encrypted with AES using 256 bits. Is there an alternative in .NET Core that I can use? A different library? A different package so that I don't have to decrypt and re-encrypt all of my existing data?

//--------------------------------------------------------------------------
//Perform the encryption
//--------------------------------------------------------------------------
try
{
    //Initialize the RijndaelManaged object
    rmAES = new RijndaelManaged { BlockSize = 256, Key = key, IV = iv, Padding = PaddingMode.PKCS7 };

    //Encrypt the data to the memory stream using the CryptoStream snd StreamWriter
    ICryptoTransform rmEncryptor = rmAES.CreateEncryptor(key, iv);
    rmMS = new MemoryStream();

    using CryptoStream rmCStream = new CryptoStream(rmMS, rmEncryptor, CryptoStreamMode.Write);
    using StreamWriter rmSWriter = new StreamWriter(rmCStream);
    rmSWriter.Write(sourceString);
    rmSWriter.Close();
}
catch (Exception ex)
{
    throw new Exception(ex);
}
question from:https://stackoverflow.com/questions/65851466/256-bit-encryption-in-net-core-5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your problem isn't key length, it is block size. The two things are different. .NET 5 should support AES-256 with the "correct" block size of 128 bits. Rijnadael main difference is in its support of different block sizes (128, 192 and 256 bits). From the wiki about AES: AES is a subset of the Rijndael block cipher... (omissis) ...Rijndael is a family of ciphers with different key and block sizes. For AES, NIST selected three members of the Rijndael family, each with a block size of 128 bits, but three different key lengths: 128, 192 and 256 bits.

About your problem, you could rip the RijndaelManaged class from the reference source's github... Rijndael and RijndaelManaged and RijndaelManagedTransform should be thee three main classes you'll need. Probably you'll have to "borrow" some other utility methods. They are all under the MIT license, so no problems here.

At least, that is what I would do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...