AES ECB加密/解密仅解密前16个字节(AES ECB encrypt/decrypt only decrypts the first 16 bytes)

我有解码AES 256字符串的函数,但它仅返回16个字符

bool decrypt_block(unsigned char* cipherText, unsigned char* plainText, unsigned char* key)
{
    AES_KEY decKey;
    if (AES_set_decrypt_key(key, 256, &decKey) < 0)
        return false;
    AES_decrypt(cipherText, plainText, &decKey);
    return true;
}

decrypt_block( encoded, resultText, ( unsigned char *) "57f4dad48e7a4f7cd171c654226feb5a");

任何想法

解决方案

看来您在混淆密钥长度和块大小.

AES可以用于3种不同的密钥长度:128位,192位& 256位.

AES始终使用128位(16个字节)的块大小.对于长度超过16个字节的消息,您需要一次解密(或加密)16个字节,并希望每次获得16个字节的输出. (您还需要决定使用哪种模式-例如CBC,CTR,ECB等.如果您要解密其他人提供的文本,则该决定已经由您决定.如果您为自己做出决定,请承担请记住,ECB几乎永远不是正确的选择.)如果消息的长度不是16字节的倍数,则需要将其粘贴以使其正确. PKCS#7填充是最常见的.

有关更多信息,请参见有关AES的维基百科文章.

I had function that decode AES 256 string but it return only 16 char

bool decrypt_block(unsigned char* cipherText, unsigned char* plainText, unsigned char* key)
{
    AES_KEY decKey;
    if (AES_set_decrypt_key(key, 256, &decKey) < 0)
        return false;
    AES_decrypt(cipherText, plainText, &decKey);
    return true;
}

decrypt_block( encoded, resultText, ( unsigned char *) "57f4dad48e7a4f7cd171c654226feb5a");

Any idea

解决方案

It appears that you are confusing key length and block size.

AES can be used with 3 different key lengths: 128-bits, 192-bits & 256-bits.

AES always uses a block size of 128 bits (16 bytes). For messages that are more than 16 bytes long, you need to decrypt (or encrypt) 16 bytes at a time and expect to get 16 bytes of output each time. (You will also need to decide which mode to use - e.g. CBC, CTR, ECB, etc.. If you're decrypting text provided by somebody else then that decision has already been taken for you. If making the decision for yourself, bear in mind that ECB is almost never the right choice.) If the message isn't a multiple of 16 bytes long, you'll need to pad it so that it is. PKCS #7 padding is the most common.

See the Wikipedia article on AES for more information.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值