简体   繁体   English

使用 C# .NET 框架的 AES-gcm 128 加密和解密问题

[英]Problem with encryption and decryption with AES-gcm 128 using C# .NET framework

I am trying to work with Encryption and Decryption using AES-gcm 128 bits using C# .NET Framework.我正在尝试使用 C# .NET 框架使用 AES-gcm 128 位进行加密和解密。 But I could not find any good solutions with this problem.但是我找不到这个问题的任何好的解决方案。 I have just found on this website, someone recommends to use Bouncy castle library.我刚刚在这个网站上发现,有人推荐使用 Bouncy castle 库。
I have no ideas, if Bouncy castle is supported AES-gcm 128. And I sill don't know how to input this library in .NET framework.我不知道,如果 Bouncy castle 支持 AES-gcm 128。而且我不知道如何在 .NET 框架中输入这个库。
Could anyone resolve this problem for me?谁能帮我解决这个问题? I am a newbie so I really need your helps.我是新手,所以我真的需要你的帮助。
Thank you so much!!!太感谢了!!!

AES GCM 256 Runs in framework 5 and above... So, first write the program with framework 5 and above I have written a sample class to use it and the library using System.Security.Cryptography; AES GCM 256 在框架 5 及更高版本中运行...因此,首先使用框架 5 及更高版本编写程序I have added to this class我已添加到此 class

 internal class class_Encrypt_Decrypt_AES_GCM

{ {

public byte[] Encrypt(String key, string payload, byte[] header_aad, byte[] tag)
{
    byte[] cipherText = new byte[payload.Length];
    try
    {
        //string key = "a05e19dc2682a72a8a9fe3d70707393e";Example
        // string payload = "Hello";Example
        //  byte[] tag = new byte[16];Example
        // byte[] header_aad = { 0x5a, 0xa5, 0x0, 0x6, 0x1a };Example
        int payload_len = payload.Length;

        byte[] bytes_key = Encoding.ASCII.GetBytes(key);

        byte[] bytes_IV = new byte[16];
        byte[] send_IV = new byte[12];
        bytes_IV = IV_generator();
        byte[] bytes_payload = new byte[payload.Length];
        byte[] bytes_payload1 = Encoding.ASCII.GetBytes(payload);
        Array.Copy(bytes_payload1, bytes_payload, bytes_payload1.Length);
        Array.Copy(bytes_IV, send_IV, bytes_IV.Length - 4);

       
        int num = payload.Length;
        header_aad[3] = (byte)(num & 0xff);
        //----------hi byte
        header_aad[2] = (byte)(num >> 8 & 0xff);
        //==========================================
        AesGcm aesGcm = new AesGcm(bytes_key);
        aesGcm.Encrypt(send_IV, bytes_payload, cipherText, tag, header_aad);

        // String IV_STR = Convert.ToString(send_IV);
        // String key_STR = Convert.ToString(bytes_key);
        // String RES = Convert.ToString(cipherText);
        //example for Decoder
        //  aesGcm.Decrypt(send_IV, cipherText, tag, bytes_payload, header_aad);
        //  string result = ASCIIEncoding.UTF8.GetString(bytes_payload);


    }
    catch (Exception e2)
    {

        int b = 0;
    }
    return cipherText;


}

private byte[] IV_generator()
{
    byte[] byte_IV = new byte[16];
    Random rnt = new Random(DateTime.Now.Day);
    byte[] byte_time_rnd = new byte[16];
    rnt.NextBytes(byte_time_rnd);

    byte[] byet_sha384_date;
    SHA384 shaM = new SHA384Managed();
    byet_sha384_date = shaM.ComputeHash(Encoding.ASCII.GetBytes(DateTime.Now.Date.ToString()));


    for (int i = 0; i < byte_time_rnd.Length; i++)
    {
        byte_IV[i] = (byte)(byet_sha384_date[i] ^ byte_time_rnd[i]);
    }
    return byte_IV;

}

In this class, there is also a function for making IV, in which I have used time parameters在这个class中,还有一个function是用来做IV的,其中我用了时间参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM