简体   繁体   English

使用phpseclib AES加密字符串

[英]Using phpseclib AES to encrypt string

Ok so here's the sample code from the page http://phpseclib.sourceforge.net/crypt/examples.html 好的,这是页面http://phpseclib.sourceforge.net/crypt/examples.html中的示例代码

<?php
include('Crypt/AES.php');
include('Crypt/Random.php');

$cipher = new Crypt_AES(); // could use CRYPT_AES_MODE_CBC
// keys are null-padded to the closest valid size
// longer than the longest key and it's truncated
//$cipher->setKeyLength(128);
$cipher->setKey('abcdefghijklmnop');
// the IV defaults to all-NULLs if not explicitly defined
$cipher->setIV(crypt_random_string($cipher->getBlockLength() >> 3));

$size = 10 * 1024;
$plaintext = str_repeat('a', $size);

echo $cipher->decrypt($cipher->encrypt($plaintext));
?>

Before anything, why is the line //$cipher->setKeyLength(128); 在此之前,为什么行//$cipher->setKeyLength(128); is being commented out? 被注释掉了吗?

And if I want to set my key to '1234568790', is there anything I should do? 如果我要将密钥设置为“ 1234568790”,我应该做些什么? Because it's much shorter than they length of the key in the example above (abcdefghijklmnop). 因为它比上面示例(abcdefghijklmnop)中密钥的长度短得多。

And finally if the plaintext that I want to encrypt is short, something like "my name is james bond", is there anything extra that I should do? 最后,如果我要加密的明文很短,例如“我的名字是詹姆斯·邦德”,我还有什么需要做的吗? Because from the code above, it seems the plaintext's length should be 10 x 1024. (Why is that?) 因为从上面的代码来看,纯文本的长度似乎应该是10 x1024。(为什么?)

Before anything, why is the line //$cipher->setKeyLength(128); 在此之前,为什么行//$cipher->setKeyLength(128); is being commented out? 被注释掉了吗?

Say you pass a 17 byte key to phpseclib via setKey . 假设您通过setKey将一个17字节的密钥传递给setKey What do you suppose ought to happen? 您认为应该发生什么? In the 1.0 and 2.0 versions if setKeyLength isn't called then the key is null-padded to 24 bytes (eg. 192-bits). 在1.0和2.0版本中,如果未调用setKeyLength则将密钥空填充为24个字节(例如192位)。 But if setKeyLength is called it'll be truncated to 16 bytes. 但是,如果setKeyLength 它将被截断为16个字节。 In the master branch (which, as I understand it, will eventually become the 3.0 branch), an exception is thrown if the key length isn't valid. 在master分支(据我所知最终将成为3.0分支)中,如果密钥长度无效,则会引发异常。 In that version it may still be desirable to call setKeyLength if, for example, if you want to set the key length as being 128-bits in one part of the code and then actually set the key in another part of the code. 在该版本中,例如,如果要在代码的一部分中将密钥长度设置为128位,然后在代码的另一部分中实际设置密钥,则可能仍然需要调用setKeyLength ie. 即。 you could do the length checking or phpseclib could do the length checking. 您可以进行长度检查,或者phpseclib可以进行长度检查。

And if I want to set my key to '1234568790', is there anything I should do? 如果我要将密钥设置为“ 1234568790”,我应该做些什么? Because it's much shorter than they length of the key in the example above (abcdefghijklmnop). 因为它比上面示例(abcdefghijklmnop)中密钥的长度短得多。

1234568790 isn't technically long enough to be a valid key. 1234568790在技​​术上还不够长,无法成为有效密钥。 It's 80 bytes long. 它的长度为80个字节。 The smallest key AES supports is 128-bits. AES支持的最小密钥为128位。 Maybe you should consider doing something like setPassword('1234568790') instead of setKey('1234568790') . 也许您应该考虑做类似setPassword('1234568790')setKey('1234568790')而不是setKey('1234568790') setPassword will use a key-derivation function to generate a key from the password. setPassword将使用密钥派生功能从密码生成密钥。

In phpseclib 1.0 and 2.0 setKey will, none-the-less, accept that as a key. 但是,在phpseclib 1.0和2.0中, setKey将接受它作为键。 It'll just null pad the key. 只需将键无效即可。 The master branch otoh will throw an exception since the key isn't actually long enough. 主键otoh将抛出异常,因为密钥实际上不够长。

And finally if the plaintext that I want to encrypt is short, something like "my name is james bond", is there anything extra that I should do? 最后,如果我要加密的明文很短,例如“我的名字是詹姆斯·邦德”,我还有什么需要做的吗? Because from the code above, it seems the plaintext's length should be 10 x 1024. (Why is that?) 因为从上面的代码来看,纯文本的长度似乎应该是10 x1024。(为什么?)

10 * 1024 is just an example. 10 * 1024仅是示例。 phpseclib, by default, will use PKCS7 padding to make strings long enough. 默认情况下,phpseclib将使用PKCS7填充来使字符串足够长。 eg. 例如。 your string is 21 bytes long (if I counted correctly). 您的字符串长21个字节(如果我算得正确的话)。 So if you call $cipher->encrypt('my name is james bond') then chr(11) will be appending to the plaintext 11 times, by phpseclib and then the string will be encrypted. 因此,如果您调用$cipher->encrypt('my name is james bond')chr(11)将被phpseclib附加到纯文本11次,然后该字符串将被加密。 This is because block algorithms need to have the the plaintext be a multiple of the block length. 这是因为块算法需要使明文为块长度的倍数。 So the ciphertext will be 32 bytes long. 因此,密文长度为32个字节。 When you then call $cipher->decrypt('...') you'll get the orig 21-byte string back. 然后,当您调用$cipher->decrypt('...')您将获得原始的21字节字符串。 I could elaborate further on PKCS7 padding, but I think I've answered your immediate question. 我可以进一步详细介绍PKCS7填充,但是我想我已经回答了您的紧迫问题。

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

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