如何解决当PHP中的密钥为整数时,Java的解密密钥是什么
现有的PHP系统(由另一家公司开发)具有如下加密代码。
$mopen = mcrypt_module_open(MCRYPT_RIJNDAEL_256,'','cfb','');
mcrypt_generic_init($mopen,keygenerator(),$iv);
$cipherText = mcrypt_generic($mopen,$imageContent);
在上面的代码的第二行中,第二个参数是键。“ keygenerator()”的返回类型为int,介于10-99之间。
mcrypt_generic_init(资源$ td,字符串$ key,字符串$ iv):int
我必须使用Java解密生成的文件。我正在使用bouncycastle库,必须将密钥作为字节数组提供。 如果“ keygenerator()”生成32作为密钥。我如何在Java中将其作为硬编码值提供。 如果我使用以下代码。
String key = "32";
cipher.init(cryptoType,new ParametersWithIV(new KeyParameter(key.getBytes()),iv.trim().getBytes()));
它给了我以下错误。
线程“ main”中的异常java.lang.IllegalArgumentException:键 长度不是128/160/192/224/256位。
解决方法
MCRYPT_RIJNDAEL_256
支持16、24和32字节密钥。如果密钥大小介于两者之间,则使用0值进行填充,直到达到下一个有效密钥长度为止。
如果密钥长度为两位数,则下一个有效密钥大小为16个字节,因此填充为用14个0值完成。
同样适用于IV,其长度必须与块大小相对应(Rijndael-256为32字节)。如果较短,则该键也将填充0值(尽管通常会在此处显示警告)。
如果密钥太长(大于32个字节),它将被截断。同样适用于IV。
以下PHP代码演示了这一点:
function encrypt($key,$iv){
$mopen = mcrypt_module_open(MCRYPT_RIJNDAEL_256,'','cfb','');
mcrypt_generic_init($mopen,$key,$iv);
$cipherText = mcrypt_generic($mopen,"The quick brown fox jumps over the lazy dog");
print(base64_encode($cipherText) . "\n");
}
$key = 32; // 0-padded to 16 bytes
$iv = 32; // 0-padded to 32 bytes (Rijndael-256 blocksize),mostly a warning is displayed
encrypt($key,$iv); // Dgfd2xT2NQ1ULob3mOX+JBPQo57JUIxabtt+TX8wnzYWhKtt/6ltY2Z/yA==
$key = "32";
$iv = "32";
encrypt($key,$iv); // Dgfd2xT2NQ1ULob3mOX+JBPQo57JUIxabtt+TX8wnzYWhKtt/6ltY2Z/yA==
$key = "32\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
$iv = "32\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
encrypt($key,$iv); // Dgfd2xT2NQ1ULob3mOX+JBPQo57JUIxabtt+TX8wnzYWhKtt/6ltY2Z/yA==
在Java代码中,必须指定 exact 键和IV大小。请注意,CFB是流密码模式,因此不使用填充。 PHP应用CFB-8(8位模式下的CFB),必须在Java代码中对此进行指定。以下Java / BouncyCastle代码解密密文:
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.RijndaelEngine;
import org.bouncycastle.crypto.modes.CFBBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
...
String ciphertextB64 = "Dgfd2xT2NQ1ULob3mOX+JBPQo57JUIxabtt+TX8wnzYWhKtt/6ltY2Z/yA==";
byte[] ciphertext = Base64.getDecoder().decode(ciphertextB64);
byte[] key = "32\0\0\0\0\0\0\0\0\0\0\0\0\0\0".getBytes(StandardCharsets.UTF_8);
byte[] iv = "32\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0".getBytes(StandardCharsets.UTF_8);
BufferedBlockCipher bufferedBlockCipher = new BufferedBlockCipher(new CFBBlockCipher(new RijndaelEngine(256),8)); // CFB in 8 bit mode
CipherParameters cipherParams = new ParametersWithIV(new KeyParameter(key),iv);
bufferedBlockCipher.init(false,cipherParams);
byte[] decryptedBuffer = new byte[bufferedBlockCipher.getOutputSize(ciphertext.length)];
int processed = bufferedBlockCipher.processBytes(ciphertext,ciphertext.length,decryptedBuffer,0);
processed += bufferedBlockCipher.doFinal(decryptedBuffer,processed);
System.out.println(new String(decryptedBuffer,processed,StandardCharsets.UTF_8)); // The quick brown fox jumps over the lazy dog
,
我接受Topaco的回答。但是我提供密钥的方式没有什么不同。
String key = "33320000000000000000000000000000";
byte[] byteKey = new BigInteger(key,16).toByteArray();
cipher.init(cryptoType,new ParametersWithIV(new KeyParameter(byteKey),iv.trim().getBytes()));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。