加密的字符串长度 [英] Length of Encrypted String

查看:419
本文介绍了加密的字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个数据库列将存储使用Triple DES加密的字符串。如何确定加密字符串列的长度?

I need to create a database column which will store a string encrypted using Triple DES. How do I determine the length of the encrypted string column?

(有答案的算法比三重DES等也欢迎。)

(Answers for algorithms other than Triple DES are also welcome.)

推荐答案

块密码,如DES,3DES和AES可以在字节块,才能操作。 DES和3DES上的8个字节和AES上的16字节的块的块进行操作。

Block-ciphers such as DES, 3DES and AES can only operate on blocks of bytes. DES and 3DES operate on block of 8 bytes and AES on blocks of 16 bytes.

要解决这个问题,你通常应用可逆的填充你的明文加密之前。这将主要永远是PKCS-padding(也称为PKCS5-或PKCS7,填充)。

To handle this, you usually apply a reversible padding to your plaintext before encrypting it. It will mostly always be "PKCS"-padding (also called PKCS5- or PKCS7-padding).

PKCS-填充增加了至少一个字节,使得所述填塞文本的长度整除与块长度(8字节为3DES)。填充字节的值是增加的字节数。 FX。 ABCDEF被填充到ABCDEF0505050505和0011223344556677被填充到0011223344566770808080808080808.请注意,这是很容易去除:你刚才看的填充字节的最后一个字节,验证它是1块长之间,并删除字节数从端(验证每个除去字节具有正确的值)。

PKCS-padding adds at least one byte such that the padded text has a length divisible with the block-length (8 bytes for 3DES). The value of the padding-bytes is the number of bytes added. Fx. ABCDEF is padded to ABCDEF0505050505 and 0011223344556677 is padded to 0011223344566770808080808080808. Note that this is easy to remove: you just look at the final byte of the padded bytes, verify that it is between 1 and the block-length, and remove that number of bytes from the end (verifying that each removed byte has the correct value).

最后,要回答你的问题:假设你正在使用3DES与CBC加密和PKCS - 填充 - 你可能是 - 长度为n的字符串加密的长度是:

Finally, to answer your question: Assuming you are using 3DES with CBC encryption and PKCS-padding - which you probably are - the encryption of a string of length n will have length:

n + 8 - (n % 8)

这篇关于加密的字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆