Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java VC+中的AES/GCM加密+;_Java_C#_Visual C++_Openssl_Aes Gcm - Fatal编程技术网

Java VC+中的AES/GCM加密+;

Java VC+中的AES/GCM加密+;,java,c#,visual-c++,openssl,aes-gcm,Java,C#,Visual C++,Openssl,Aes Gcm,我在VC++中使用OpenSSL库尝试了GCM加密。我正在获取加密数据,但数据与JAVA和C#中的输出数据不匹配。我使用了相同的参数凭据 以下是我正在使用的代码: int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *aad,int aad_len, unsigned char *key, unsigned char *iv,unsigned char *ciphertext, unsigned char

我在VC++中使用OpenSSL库尝试了GCM加密。我正在获取加密数据,但数据与JAVA和C#中的输出数据不匹配。我使用了相同的参数凭据

以下是我正在使用的代码:

int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *aad,int aad_len, unsigned char *key, unsigned char *iv,unsigned char *ciphertext, unsigned char *tag)
{
    EVP_CIPHER_CTX *ctx;

    int len;

    int ciphertext_len;


    /* Create and initialise the context */
    if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors();

    /* Initialise the encryption operation. */
    if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
        handleErrors();

    /* Set IV length if default 12 bytes (96 bits) is not appropriate */
    if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL))
        handleErrors();

    /* Initialise key and IV */
    if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors();

    /* Provide any AAD data. This can be called zero or more times as
    * required
    */
    if (1 != EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len))
        handleErrors();

    /* Provide the message to be encrypted, and obtain the encrypted output.
    * EVP_EncryptUpdate can be called multiple times if necessary
    */
    if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
        handleErrors();
    ciphertext_len = len;

    /* Finalise the encryption. Normally ciphertext bytes may be written at
    * this stage, but this does not occur in GCM mode
    */
    if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
    ciphertext_len += len;

    /* Get the tag */
    if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
        handleErrors();

    /* Clean up */
    EVP_CIPHER_CTX_free(ctx);

    return ciphertext_len;
}
我们正在使用的C代码

public byte[] GCM_Encryption(byte[] plainText)
        {
            byte[] authenticationTag;
            byte[] Nonce;
            byte[] AAD;
            byte[] ciphertext;

            string _nonce = Cur_DnT.Substring(Cur_DnT.Length - 12);
            string _AAD = Cur_DnT.Substring(Cur_DnT.Length - 16);

            Nonce = new byte[] { 0x2d, 0x32, 0x35, 0x54, 0x31, 0x35, 0x3a, 0x31, 0x35, 0x3a, 0x32, 0x39 };//Encoding.UTF8.GetBytes(_nonce);
            AAD = new byte[] { 0x37, 0x2d, 0x30, 0x35, 0x2d, 0x32, 0x35, 0x54, 0x31, 0x35, 0x3a, 0x31, 0x35, 0x3a, 0x32, 0x39 };//Encoding.UTF8.GetBytes(_AAD);

            using (AuthenticatedAesCng aes = new AuthenticatedAesCng())
            {
                aes.Key = new byte[] { 0xF9, 0x28, 0x01, 0x98, 0xBB, 0xC9, 0x43, 0xF8, 0x85, 0x6D, 0xA8, 0x36, 0x0F, 0x07, 0x37, 0x92, 0x7C, 0x49, 0x45, 0x29, 0x6C, 0xAF, 0xD5, 0x30, 0x1B, 0x92, 0x0E, 0x32, 0xAA, 0x20, 0xA2, 0x33 };//SKey;
                aes.IV = Nonce;
                aes.CngMode = CngChainingMode.Gcm;

                // This data is required to verify the authentication tag, but will not go into the
                // ciphertext
                aes.AuthenticatedData = AAD;

                // Do the encryption
                using (MemoryStream ms = new MemoryStream())
                using (IAuthenticatedCryptoTransform encryptor = aes.CreateAuthenticatedEncryptor())
                using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                {
                    // Encrypt the plaintext
                    byte[] plaintext = plainText;
                    cs.Write(plaintext, 0, plaintext.Length);

                    // Complete the encryption operation, and generate the authentication tag
                    cs.FlushFinalBlock();

                    // Get the generated ciphertext and authentication tag
                    ciphertext = ms.ToArray();
                    authenticationTag = encryptor.GetTag();
                }
            }
            string ddf = BitConverter.ToString(ciphertext);
            return ciphertext.Concat(authenticationTag).ToArray();
        }

我的需求是C++中的AES GCM,而不是其他模式之一。你有很多例子可供选择。您还应该展示一些Java或C代码,这样人们就可以看到您是如何获得AES密钥的。看起来您正在使用的示例是,这是一个很好的起点。使用测试向量查看哪些实现产生正确的结果。NIST提供了一些at。您使用的C代码的IV(Nonce)长度为12字节。在C代码中,您显式地将其设置为16字节长,因此这肯定会导致差异。您也不会在C代码中显示key/IV/AAD。如果你确认它们是相同的,那么,这将是有益的。我的要求是C++中的AES GCM,而不是其他模式之一。你有很多例子可供选择。您还应该展示一些Java或C代码,这样人们就可以看到您是如何获得AES密钥的。看起来您正在使用的示例是,这是一个很好的起点。使用测试向量查看哪些实现产生正确的结果。NIST提供了一些at。您使用的C代码的IV(Nonce)长度为12字节。在C代码中,您显式地将其设置为16字节长,因此这肯定会导致差异。您也不会在C代码中显示key/IV/AAD。如果您能确认它们是相同的,那将很有帮助。