php mcrypt_generic_init,PHP: mcrypt_generic - Manual

本文介绍了如何在PHP和Perl中使用Blowfish算法的Cipher Block Chaining(CBC)模式进行加密和解密操作。关键点包括:Blowfish的密钥长度至少为8个字符,初始化向量(IV)必须是8个字符,填充方式为NULL,且PHP需要libmcrypt库大于等于2.4.9来与Perl保持兼容。示例代码展示了在两种语言中执行加密和解密的过程。
摘要由CSDN通过智能技术生成

I was able get php and perl to play together with blowfish using cipher block chaining.  The blowfish key needs to be atleast 8 chars (even though blowfish min is 8 bits, perl didn't like keys smaller than 8 chars) and max 56.  The iv must be exactly 8 chars and padding needs to be null because php pads with nulls.  Also, php needs libmcrypt >= 2.4.9 to be compatible with perl.

PERL

----

use Crypt::CBC;

$cipher = Crypt::CBC->new( {'key' => 'my secret key',

'cipher'=> 'Blowfish',

'iv' => '12345678',

'regenerate_key' => 0,

'padding' => 'null',

'prepend_iv' => 0

});

$cc = 'my secret text';

$encrypted = $cipher->encrypt($cc);

$decrypted = $cipher->decrypt($encrypted);

print "encrypted : ".$encrypted;

print "
";

print "decrypted : ".$decrypted;

PHP

---

$cc = 'my secret text';

$key = 'my secret key';

$iv = '12345678';

$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');

mcrypt_generic_init($cipher, $key, $iv);

$encrypted = mcrypt_generic($cipher,$cc);

mcrypt_generic_deinit($cipher);

mcrypt_generic_init($cipher, $key, $iv);

$decrypted = mdecrypt_generic($cipher,$encrypted);

mcrypt_generic_deinit($cipher);

echo "encrypted : ".$encrypted;

echo "
";

echo "decrypted : ".$decrypted;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值