简体   繁体   English

当使用256位密钥调用aes128密码时,libgcrypt做什么不同?

[英]What is libgcrypt doing different when calling the aes128 cipher with a 256bit key?

This is a continuation of my previous question ( RSA Decryption) . 这是我之前的问题( RSA Decryption)的延续。

Edit: The answer below was clear and concise. 编辑:下面的答案清晰简洁。 I've added some sample code below which has helped me verify what I think is happening. 我在下面添加了一些示例代码,帮助我验证了我的想法。 I will test today and post back. 我今天将测试并发回。 The client application is creating a 256bit key but when it calls gcry_cipher_setkey it uses the keylength returned from the algorithm. 客户端应用程序正在创建256位密钥,但是当它调用gcry_cipher_setkey时,它使用从算法返回的密钥长度。 So I'm guessing that the setkey is truncating the 256bit key to 128bit. 所以我猜测setkey是将256位密钥截断为128位。 Easy enough to test out. 很容易测试出来。

I am attempting to decrypt a file that was encrypted using libgcrypt with aes128 / cbc / no padding. 我试图解密使用libgcrypt使用aes128 / cbc / no padding加密的文件。 There are two different applications that are doing the encryption both of which I have no control over. 有两个不同的应用程序正在进行加密,我无法控制这些应用程序。 One is using a 128bit AES key while the other is using 256bit key. 一种是使用128位AES密钥,而另一种是使用256位密钥。 All the internal calls are the same otherwise. 否则所有内部调用都是相同的。

Edit : Here is pseudo encrypting code: Edit 2: Fixed order & Added comments for future users: 编辑 :这是伪加密代码: 编辑2:固定订单和为未来用户添加的评论:

#define AES_KEY_SIZE 32
char AESKey[AES_KEY_SIZE+1];
GenerateAESKey(AESKey);

Error = gcry_cipher_open(AesHandle, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
// this is always 128bits (16 bytes)
BlockLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128);
// this should return 128bits (16 bytes)
KeyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES128);
Error = gcry_cipher_setiv(AesHandle, NULL, 0);
// AESKey is a 32byte (256bit) char array & KeyLength is 128bits (16bytes)
// so gcry_cipher_setkey only uses the first 'KeLength' bytes of 'AESKey'
// which in this case is the first 16 bytes of 'AESKey'
Error = gcry_cipher_setkey(*AesHandle, AESKey, KeyLength);
Error = gcry_cipher_encrypt(AesHandle, Encrypted, BlockLength, ToEncrypt, BlockLenth);


void GenerateAESKey( char * AESKey ) {
    int i;

    srand(time(NULL));
    for ( i = 0; i < AES_KEY_SIZE; i++ ) {
        AESKey[i] = (rand() % 93)+33;
    }
    AESKey[AES_KEY_SIZE] = '\0';
}

So in my C# code I do this before before I start decrypting: 所以在我的C#代码中,我在开始解密之前就这样做了:

var aesKey = DecryptAesKey(s);
if (aesKey.Length == 32)
{
   var tempKey = new byte[16];
   Buffer.BlockCopy(aesKey,0,tempKey,0,16);
   aesKey = tempKey;
}

I am using C# & bouncycastle to decrypt the files. 我正在使用C#和bouncycastle来解密文件。 I successfully decrypt the files that were encrypted w/128bit key. 我成功解密了使用128位密钥加密的文件。 But fail when the key is 256bit. 但是当密钥是256位时失败。 Fail means that the output is garbled. 失败意味着输出是乱码。

I have verified that the AES Key is getting decrypted correctly for both sources. 我已经验证AES密钥正在为两个源正确解密。

My question is what is Libgrypt doing different when the key is 256bits? 我的问题是当关键是256位时,Libgrypt的做法有何不同? Or is this even the right path to find out why my decryption is failing? 或者这是找到解密失败原因的正确途径? Thank you for any info or direction you can point me in. 感谢您指点我的任何信息或方向。

Brian 布赖恩

I assume you have 我猜你有

  1. 2 different keys (128 and 256 bit) 2个不同的键(128和256位)
  2. 2 different sources of ciphertext (1: AES128 2: AES256 Both: CBC/No Padding ) 2种不同的密文源(1: AES128 2: AES256两者: CBC/No Padding
  3. Can decrypt 128 ciphertext 可以解密128个密文
  4. Fail with 256 (everything is garbled and nothing from plaintext is decrypted). 失败256(一切都是乱码,没有明文解密)。

The main difference is the key length of 128 or 256 bit . 主要区别是密钥长度128256位 It specifies the number of repetitions of transformation rounds that convert the input. 它指定转换输入的转换轮次的重复次数。 You probably will never need internal implementation details. 您可能永远不需要内部实现细节。 AES uses more rounds with larger keys. AES使用更多的密钥和更大的密钥。 10 for 128-bit keys, 14 for 256-bit keys. 10位用于128位密钥,14位用于256位密钥。

Important part is that Block size is always 128 bit for 128 and 256 CBC AES. 重要的是,128和256 CBC AES的块大小始终为128位

Both AES-128-CBC and AES-256-CBC use 128 bit IV . AES-128-CBCAES-256-CBC都使用128位IV

So my wild guess (without seeing your AES256 code) is that there can be bug somewhere in Block or IV size in your AES256 code. 因此,我的猜测(没有看到您的AES256代码)是您的AES256代码中的块或IV大小的某处可能存在错误。

If you just set key for 128AES with this function, check this docs 如果您只是使用此功能设置128AES的密钥,请查看此文档

 gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t h, const void *k, size_t l)

The length l (in bytes) of the key k must match the required length of the algorithm set for this context or be in the allowed range for algorithms with variable key size. 密钥k的长度l(以字节为单位)必须与该上下文的算法集的所需长度匹配,或者在具有可变密钥大小的算法的允许范围内。 The function checks this and returns an error if there is a problem. 该函数检查此项并在出现问题时返回错误。 A caller should always check for an error. 呼叫者应始终检查错误。

And you normally don't want to use CBC with NoPadding (unless your data size is always a multiple of 16 bytes) but padding problems garble only the last 16-byte block of plaintext. 而且您通常不希望将CBC与NoPadding一起使用(除非您的数据大小始终是16字节的倍数),但填充问题只会导致最后16字节的明文块。

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

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