php 如何在php中使用Blowfish算法解密?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5580960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to decrypt using Blowfish algorithm in php?
提问by Sangam254
I am supposed to write a PHP script to decrypt Blowfish encrypted data.
我应该编写一个 PHP 脚本来解密 Blowfish 加密的数据。
The data I am receiving for decryption is encrypted by another application (I have no access to it).
我收到的用于解密的数据由另一个应用程序加密(我无法访问它)。
The data decrypts fine when am check it using a javascript script (blowfish.js).
当我使用 javascript 脚本 (blowfish.js) 检查数据时,数据解密得很好。
How can I decrypt the data in php?
如何解密php中的数据?
I have tried the mcrypt
function in PHP. The code works fine if I encrypt and decrypt using the same code. If I decrypt an encrypted code (in another app) it gives junk.
我已经mcrypt
在 PHP 中尝试过该功能。如果我使用相同的代码加密和解密,则代码工作正常。如果我解密加密代码(在另一个应用程序中),它会产生垃圾。
No idea about what mode to set.
不知道要设置什么模式。
Can anyone suggest on the code below or any PHP BlowFish code without using mcrypt?
任何人都可以在不使用 mcrypt 的情况下对下面的代码或任何 PHP BlowFish 代码提出建议吗?
<?php
class Encryption
{
static $cypher = 'blowfish';
static $mode = 'cfb';
static $key = '12345678';
public function encrypt($plaintext)
{
$td = mcrypt_module_open(self::$cypher, '', self::$mode, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, self::$key, $iv);
$crypttext = mcrypt_generic($td, $plaintext);
mcrypt_generic_deinit($td);
return $iv.$crypttext;
}
public function decrypt($crypttext)
{
$plaintext = "";
$td = mcrypt_module_open(self::$cypher, '', self::$mode, '');
$ivsize = mcrypt_enc_get_iv_size($td);
$iv = substr($crypttext, 0, $ivsize);
$crypttext = substr($crypttext, $ivsize);
if ($iv)
{
mcrypt_generic_init($td, self::$key, $iv);
$plaintext = mdecrypt_generic($td, $crypttext);
}
return $plaintext;
}
}
$encrypted_text = Encryption::encrypt('this text is unencrypted');
echo "ENCRY=".$encrypted_text;echo "<br/>";
////I am using this part(decryption) coz data already encryption
// Encrypted text from app
$encrypted_text = '29636E7ADA7081E7F5D73121C45E20D5';
// Decrypt text
$decrypted_text = Encryption::decrypt($encrypted_text);
echo "ENCRY=".$decrypted_text;echo "<br/>";
?>
回答by Jon
The $iv
you use when decrypting must be the same as the Initialization Vectorused when encrypting the data. Your own functions transfer this information by prepending the IV to the ciphertext (return $iv.$crypttext;
), but the other application might not do so.
在$iv
您解密必须为同一时使用的初始化向量加密数据时使用。您自己的函数通过将 IV 附加到密文 ( return $iv.$crypttext;
)来传输此信息,但其他应用程序可能不会这样做。
You need to find out what IV the other app uses, and pass that to your own code. Since the decrypt
function reads the IV from the beginning of the ciphertext you can simply prepend it.
您需要找出其他应用程序使用的 IV,并将其传递给您自己的代码。由于该decrypt
函数从密文的开头读取 IV,因此您可以简单地添加它。
Also, you can test a bit by encrypting the same text with your encrypt
function and with the other application. If the outputs do not have the same length (your own is larger), then the app is not including the IV inside the ciphertext and you must obtain this information in another manner.
此外,您可以通过使用您的encrypt
函数和其他应用程序加密相同的文本来进行一些测试。如果输出的长度不同(您自己的长度更大),则应用程序不包含密文中的 IV,您必须以其他方式获取此信息。
And of course the cipher mode used (CFB) must be the same between your code and the other app.
当然,您的代码和其他应用程序之间使用的密码模式 (CFB) 必须相同。
回答by Aidan
There's a nice, easy to implement solution here:
这里有一个很好的,易于实施的解决方案:
回答by RobertPitt
There is a PEAR Library for creating blowfish encyptions that allow you to choose weather to use MCRYPT Libs, or purely native PHP:
有一个用于创建河豚加密的 PEAR 库,允许您选择天气来使用 MCRYPT 库或纯原生 PHP:
you may view the Library here: Crypt_Blowfish 1.1.0RC2
您可以在此处查看库:Crypt_Blowfish 1.1.0RC2
Select the PHP.php file will show you the source code to do this hard coded in native PHP.
选择 PHP.php 文件将向您显示在本机 PHP 中进行硬编码的源代码。