PHP DES加密中与C#/ActionScript/Java互操作的算法

搜索DES加密 不一致 好像能搜索到很多。
差异一般产生在加密方式,而且是你没指定的加密方式,或者zeroIV初始向量,填充模式等等,你不指定,各个程序和库处理的方式就会不一样,这就是产生差异的原因。
以PHP的mcrypt_cbc函数为例

string mcrypt_cbc ( string $cipher , string $key , string $data , int $mode [, string $iv ] )

cbc是工作模式,DES一共有电子密码本模式(ECB)、加密分组链接模式(CBC)、加密反馈模式(CFB)和输出反馈模式(OFB)四种模式,
在使用 CFB 及 OFB 二种模式时,必须要向量初始化 (Initialization vector, IV),CBC 模式也可以使用向量初始化。向量初始化的值在加解密时必须是独一无二的,同时也要保持相同。当加密后的资料输出时,也可同时输出密码钥匙 (例如存在文件中);或者也可以将向量初始化的值与加密后的资料一起输出。

目前PHP支持的cipher有

MCRYPT_3DES
MCRYPT_ARCFOUR_IV (libmcrypt > 2.4.x only)
MCRYPT_ARCFOUR (libmcrypt > 2.4.x only)
MCRYPT_BLOWFISH
MCRYPT_CAST_128
MCRYPT_CAST_256
MCRYPT_CRYPT
MCRYPT_DES
MCRYPT_DES_COMPAT (libmcrypt 2.2.x only)
MCRYPT_ENIGMA (libmcrypt > 2.4.x only, alias for MCRYPT_CRYPT)
MCRYPT_GOST
MCRYPT_IDEA (non-free)
MCRYPT_LOKI97 (libmcrypt > 2.4.x only)
MCRYPT_MARS (libmcrypt > 2.4.x only, non-free)
MCRYPT_PANAMA (libmcrypt > 2.4.x only)
MCRYPT_RIJNDAEL_128 (libmcrypt > 2.4.x only)
MCRYPT_RIJNDAEL_192 (libmcrypt > 2.4.x only)
MCRYPT_RIJNDAEL_256 (libmcrypt > 2.4.x only)
MCRYPT_RC2
MCRYPT_RC4 (libmcrypt 2.2.x only)
MCRYPT_RC6 (libmcrypt > 2.4.x only)
MCRYPT_RC6_128 (libmcrypt 2.2.x only)
MCRYPT_RC6_192 (libmcrypt 2.2.x only)
MCRYPT_RC6_256 (libmcrypt 2.2.x only)
MCRYPT_SAFER64
MCRYPT_SAFER128
MCRYPT_SAFERPLUS (libmcrypt > 2.4.x only)
MCRYPT_SERPENT(libmcrypt > 2.4.x only)
MCRYPT_SERPENT_128 (libmcrypt 2.2.x only)
MCRYPT_SERPENT_192 (libmcrypt 2.2.x only)
MCRYPT_SERPENT_256 (libmcrypt 2.2.x only)
MCRYPT_SKIPJACK (libmcrypt > 2.4.x only)
MCRYPT_TEAN (libmcrypt 2.2.x only)
MCRYPT_THREEWAY
MCRYPT_TRIPLEDES (libmcrypt > 2.4.x only)
MCRYPT_TWOFISH (for older mcrypt 2.x versions, or mcrypt > 2.4.x )
MCRYPT_TWOFISH128 (TWOFISHxxx are available in newer 2.x versions, but not in the 2.4.x versions)
MCRYPT_TWOFISH192
MCRYPT_TWOFISH256
MCRYPT_WAKE (libmcrypt > 2.4.x only)
MCRYPT_XTEA (libmcrypt > 2.4.x only)

其中PKCS5填充PHP就不支持,这里有个实现方法 http://www.php.net/manual/zh/ref.mcrypt.php#69782
C#版本的算法默认一搜索一大堆 ,这里就不再贴了。
PHP相关的代码 可以看我的这个Gitcafe

Author Info :
  • From:PHP DES加密中与C#/ActionScript/Java互操作的算法
  • URL:http://blog.ihipop.info/2012/04/3160.html
  • Please Reserve This Link,Thanks!
  • 《PHP DES加密中与C#/ActionScript/Java互操作的算法》上有1条评论

    Google搜索代理进行回复 取消回复

    电子邮件地址不会被公开。 必填项已用*标注