如何使用AES(CBC)128位算法加密可变大小的数据

huangapple go评论63阅读模式
英文:

How to encrypt variable size of data by using AES (CBC) 128 Algorithm

问题

我想加密4字节的数据,但AES要求输入16字节数据并输出16字节数据,那么如何解决这个问题,如果有人有源代码,请分享。

谢谢

为了加密4字节的数据,我们需要再添加12个字节,组成一个16字节的块,这将增加通过无线传输的数据长度。那么如何发送加密数据并保持原始数据长度?

英文:

I want encrypt 4 byte of data, but AES takes 16 byte of data as input and gives 16 bytes as output,
So How to overcome this problem, If some have source code please share

Thanks

To encrypt 4 byte of data we need to add 12 more bytes and make a block of 16 bytes, its take more data length to transfer via wireless, So How to send Encrypted data with Real data length

答案1

得分: 1

在加密时,追加12个字节的随机垃圾。
在解密时,忽略最后12个字节。

如果长度不固定,那么
[1位垃圾][7位位长度][n位数据][128-8-n个垃圾位以填充到128]

英文:

On encryption, append 12 bytes of random junk.
On decryption, ignore the last 12 bytes.

If length is not fixed, then
[1-bit junk][7-bit bit-length][n-bits of data][128-8-n junk bits to fill to 128]

答案2

得分: 0

Cipher block chaining (CBC mode) requires that the plaintext be a multiple of the block size. If it is not, you'll need to pad it out to a multiple of the block size.

Cipher-feedback mode (CFB mode) exists primarily to avoid this -- as it uses the block cipher as a way of generating key bits to XOR with the plaintext to encrypt it, it can be used with any bit length, and padding is not needed. With CFB mode, however, it is critical to not reuse IVs, as that will directly leak the first block of the plaintext. In CBC mode, reusing IVs is still bad (will leak info about correlated inputs), but arguably not as bad.

英文:

Cipher block chaining (CBC mode) requires that the plaintext be a mulitple of the block size. If it is not, you'll need to pad it out to a multiple of the block size.

Cipher-feedback mode (CFB mode) exists primarily to avoid this -- as it uses the block cipher as a way of generating key bits to XOR with the the plaintext to encrypt it, it can be used with any bit length and padding is not needed. With CFB mode, however, it is critical to not reuse IVs, as that will directly leak the first block of the plaintext. In CBC mode, reusing IVs is still bad (will leak info about correlated inputs), but arguably not as bad.

huangapple
  • 本文由 发表于 2023年2月19日 14:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定