使用AES 256对消息进行加密,然后使用SHA-384哈希算法对包括密钥在内的消息进行哈希,最后使用C#中的ECDSA

本文关键字:哈希 消息 ECDSA 中的 密钥 最后 算法 然后 加密 使用 AES | 更新日期: 2023-09-27 18:26:43

我对密码学很陌生。我被要求完成一项任务,我需要用AES加密(使用Key和IV=0)对文件进行加密,一旦我能够加密文件,我就需要在文件头中包含私钥,并在完成后使用SHA 384对文件进行哈希,最后我需要用ECDSA-P-384签名来发送消息。现在,我可以使用AES 256算法加密文件,这是我的代码。

主要方法-

            public void EnStart()
            {
            try
            {
                using (Aes myAes = Aes.Create())
                    {
                    myAes.BlockSize = 128;  // define BlockSize
                    myAes.KeySize = 256;    // define KeySize
                    myAes.IV = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //MIDS will use an empty IV
                    myAes.Padding = PaddingMode.None;
                    // Encrypt the string to an array of bytes. + Write myAes.Key in separate file
                    byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
                    string strEncryptedMsg = System.Text.Encoding.UTF8.GetString(encrypted); // get UTF8 encoded string.
                    System.IO.File.WriteAllText(@"D:'DEV'OUT'EncryptedText.txt", strEncryptedMsg);
                    string strEncryptedFile = string.Empty;
                    byte[] fileBytes = File.ReadAllBytes(@"D:'Amit'DEV'OUT'EncryptedText.txt");
                    strEncryptedFile = System.Text.Encoding.UTF8.GetString(fileBytes);
                    // Decrypt the bytes to a string. 
                    myAes.Key = ReadKeyFile(strKeyPath);
                    string strDecryptedMsg = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
                    System.IO.File.WriteAllText(@"D:'Amit'DEV'OUT'DecryptedText.txt", strDecryptedMsg);
                    //Display the original data and the decrypted data.
                    //Console.WriteLine("Key:   {0}", strKey);
                    }           
                }
            catch (Exception e)
                {
                Console.WriteLine("Error: {0}", e.Message);
                }
            }

//加密方法

            static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
            {
                // Check arguments.
                if (plainText == null || plainText.Length <= 0)
                    throw new ArgumentNullException("plainText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("IV-Key");
                byte[] encrypted;
                // Create an Aes object
                // with the specified key and IV.
                using (Aes aesAlg = Aes.Create())
                {
                    aesAlg.Key = Key;
                    aesAlg.IV = IV;
                    //create key file 
                    CreateKeyFile(Key, strKeyPath);
                    // Create a encryptor to perform the stream transform.
                    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                    // Create the streams used for encryption.
                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        //msEncrypt.Write(Key, 0, 32);
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                //Write all data to the stream.
                                swEncrypt.Write(plainText);
                            }
                            encrypted = msEncrypt.ToArray();
                        }
                    }
                }
                // Return the encrypted bytes from the memory stream.
                return encrypted;
            }

//解密方法

            static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
            {
                // Check arguments.
                if (cipherText == null || cipherText.Length <= 0)
                    throw new ArgumentNullException("cipherText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("IV-Key");
                // Declare the string used to hold
                // the decrypted text.
                string plaintext1 = null;
                // Create an Aes object
                // with the specified key and IV.
                using (Aes aesAlg = Aes.Create())
                {
                    aesAlg.Key = Key;
                    aesAlg.IV = IV;
                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                    // Create the streams used for decryption.
                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                plaintext1 = srDecrypt.ReadToEnd();
                            }
                        }
                    }
                }
                return plaintext1;
            }

//创建密钥文件方法和获取字节方法

            static byte[] GetBytes(string str)
            {
                byte[] bytes = new byte[str.Length * sizeof(char)];
                System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
                return bytes;
            }
            static void CreateKeyFile(byte[] bytes, string strKeyPath)
            {
                if (File.Exists(strKeyPath))
                {
                    File.Delete(strKeyPath);
                }
                    //File.WriteAllBytes(string path, byte[] bytes)
                    File.WriteAllBytes(strKeyPath,bytes);
            }

现在我的查询是

1) 我可以单独加密文件和编写密钥(只是为了测试功能)。但当我试图解密加密文件时,它会抛出一个错误。填充是不对的。请告知,我的代码出了什么问题

2) 一旦我完成了加密,我就不知道用密钥对文件进行哈希,请建议我在这里有用的代码片段。

3) 哈希之后,我需要用ECDSA p-384数字签名算法对此进行签名。请告知。

使用AES 256对消息进行加密,然后使用SHA-384哈希算法对包括密钥在内的消息进行哈希,最后使用C#中的ECDSA

  1. 您没有指定填充,如果数据长度是块大小的倍数,并且加密和解密方法都事先知道,则这种情况适用。通常使用PKCS#7(PKCS#5基本相同)填充。

  2. 加密模式应为CBC。

  3. 加密密钥应该是所需的确切大小,不要依赖于默认填充。

  4. iv应该是一个随机字节字符串,并预挂在加密数据上。

  5. 数据流中应该有一个版本指示符,以便将来可以进行更改。

注意:AES,即对称密钥加密,没有公共和私有密钥,这些术语通常保留用于RSA等非对称加密。