簡體   English   中英

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

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

我正在嘗試使用 C# .NET 框架使用 AES-gcm 128 位進行加密和解密。 但是我找不到這個問題的任何好的解決方案。 我剛剛在這個網站上發現,有人推薦使用 Bouncy castle 庫。
我不知道,如果 Bouncy castle 支持 AES-gcm 128。而且我不知道如何在 .NET 框架中輸入這個庫。
誰能幫我解決這個問題? 我是新手,所以我真的需要你的幫助。
太感謝了!!!

AES GCM 256 在框架 5 及更高版本中運行...因此,首先使用框架 5 及更高版本編寫程序我已添加到此 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;

}

在這個class中,還有一個function是用來做IV的,其中我用了時間參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM