>백엔드 개발 >PHP 튜토리얼 >PHP如何实现可以与在线加密解密工具交互的des加密算法

PHP如何实现可以与在线加密解密工具交互的des加密算法

WBOY
WBOY원래의
2016-06-06 20:11:391748검색

使用php做des加密,加密出来的密文,可以在des在线加密解密工具上解密出来

回复内容:

使用php做des加密,加密出来的密文,可以在des在线加密解密工具上解密出来

  • 不同的加密方式可得出不同的结果可导致最终的数据并不通用尽管是同一个算法

PHP自带DES和3DES的加解密

PHP自身支持的加密方式列表(依赖mcrypt):http://cn2.php.net/manual/zh/mcrypt.ciph...

<code class="php">echo mcrypt_get_key_size(MCRYPT_DES,MCRYPT_MODE_CFB);
// des cfb 秘钥长度为8位

// 秘钥
$key = 11111111;

// 创建IV
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_CFB),MCRYPT_RAND);

// 加密 使用CFB模式
$data = mcrypt_encrypt(MCRYPT_DES, $key, '加密的数据', MCRYPT_MODE_CFB, $iv);

var_dump($data);</code>

另外PHP的OpenSSL扩展也可以直接用于加密 相关函数:openssl_encrypt

<code class="php">// 获取OpenSSL支持的加密参数列表
print_r(openssl_get_cipher_methods());
Array
(
    [0] => AES-128-CBC
    [1] => AES-128-CFB
    [2] => AES-128-CFB1
    [3] => AES-128-CFB8
    [4] => AES-128-CTR
    [5] => AES-128-ECB
    [6] => AES-128-OFB
    [7] => AES-128-XTS
    [8] => AES-192-CBC
    [9] => AES-192-CFB
    [10] => AES-192-CFB1
    [11] => AES-192-CFB8
    [12] => AES-192-CTR
    [13] => AES-192-ECB
    [14] => AES-192-OFB
    [15] => AES-256-CBC
    [16] => AES-256-CFB
    [17] => AES-256-CFB1
    [18] => AES-256-CFB8
    [19] => AES-256-CTR
    [20] => AES-256-ECB
    [21] => AES-256-OFB
    [22] => AES-256-XTS
    [23] => BF-CBC
    [24] => BF-CFB
    [25] => BF-ECB
    [26] => BF-OFB
    [27] => CAMELLIA-128-CBC
    [28] => CAMELLIA-128-CFB
    [29] => CAMELLIA-128-CFB1
    [30] => CAMELLIA-128-CFB8
    [31] => CAMELLIA-128-ECB
    [32] => CAMELLIA-128-OFB
    [33] => CAMELLIA-192-CBC
    [34] => CAMELLIA-192-CFB
    [35] => CAMELLIA-192-CFB1
    [36] => CAMELLIA-192-CFB8
    [37] => CAMELLIA-192-ECB
    [38] => CAMELLIA-192-OFB
    [39] => CAMELLIA-256-CBC
    [40] => CAMELLIA-256-CFB
    [41] => CAMELLIA-256-CFB1
    [42] => CAMELLIA-256-CFB8
    [43] => CAMELLIA-256-ECB
    [44] => CAMELLIA-256-OFB
    [45] => CAST5-CBC
    [46] => CAST5-CFB
    [47] => CAST5-ECB
    [48] => CAST5-OFB
    [49] => DES-CBC
    [50] => DES-CFB
    [51] => DES-CFB1
    [52] => DES-CFB8
    [53] => DES-ECB
    [54] => DES-EDE
    [55] => DES-EDE-CBC
    [56] => DES-EDE-CFB
    [57] => DES-EDE-OFB
    [58] => DES-EDE3
    [59] => DES-EDE3-CBC
    [60] => DES-EDE3-CFB
    [61] => DES-EDE3-CFB1
    [62] => DES-EDE3-CFB8
    [63] => DES-EDE3-OFB
    [64] => DES-OFB
    [65] => DESX-CBC
    [66] => GOST 28147-89
    [67] => IDEA-CBC
    [68] => IDEA-CFB
    [69] => IDEA-ECB
    [70] => IDEA-OFB
    [71] => RC2-40-CBC
    [72] => RC2-64-CBC
    [73] => RC2-CBC
    [74] => RC2-CFB
    [75] => RC2-ECB
    [76] => RC2-OFB
    [77] => RC4
    [78] => RC4-40
    [79] => RC4-HMAC-MD5
    [80] => SEED-CBC
    [81] => SEED-CFB
    [82] => SEED-ECB
    [83] => SEED-OFB
    [84] => aes-128-cbc
    [85] => aes-128-ccm
    [86] => aes-128-cfb
    [87] => aes-128-cfb1
    [88] => aes-128-cfb8
    [89] => aes-128-ctr
    [90] => aes-128-ecb
    [91] => aes-128-gcm
    [92] => aes-128-ofb
    [93] => aes-128-xts
    [94] => aes-192-cbc
    [95] => aes-192-ccm
    [96] => aes-192-cfb
    [97] => aes-192-cfb1
    [98] => aes-192-cfb8
    [99] => aes-192-ctr
    [100] => aes-192-ecb
    [101] => aes-192-gcm
    [102] => aes-192-ofb
    [103] => aes-256-cbc
    [104] => aes-256-ccm
    [105] => aes-256-cfb
    [106] => aes-256-cfb1
    [107] => aes-256-cfb8
    [108] => aes-256-ctr
    [109] => aes-256-ecb
    [110] => aes-256-gcm
    [111] => aes-256-ofb
    [112] => aes-256-xts
    [113] => bf-cbc
    [114] => bf-cfb
    [115] => bf-ecb
    [116] => bf-ofb
    [117] => camellia-128-cbc
    [118] => camellia-128-cfb
    [119] => camellia-128-cfb1
    [120] => camellia-128-cfb8
    [121] => camellia-128-ecb
    [122] => camellia-128-ofb
    [123] => camellia-192-cbc
    [124] => camellia-192-cfb
    [125] => camellia-192-cfb1
    [126] => camellia-192-cfb8
    [127] => camellia-192-ecb
    [128] => camellia-192-ofb
    [129] => camellia-256-cbc
    [130] => camellia-256-cfb
    [131] => camellia-256-cfb1
    [132] => camellia-256-cfb8
    [133] => camellia-256-ecb
    [134] => camellia-256-ofb
    [135] => cast5-cbc
    [136] => cast5-cfb
    [137] => cast5-ecb
    [138] => cast5-ofb
    [139] => des-cbc
    [140] => des-cfb
    [141] => des-cfb1
    [142] => des-cfb8
    [143] => des-ecb
    [144] => des-ede
    [145] => des-ede-cbc
    [146] => des-ede-cfb
    [147] => des-ede-ofb
    [148] => des-ede3
    [149] => des-ede3-cbc
    [150] => des-ede3-cfb
    [151] => des-ede3-cfb1
    [152] => des-ede3-cfb8
    [153] => des-ede3-ofb
    [154] => des-ofb
    [155] => desx-cbc
    [156] => gost89
    [157] => gost89-cnt
    [158] => id-aes128-CCM
    [159] => id-aes128-GCM
    [160] => id-aes128-wrap
    [161] => id-aes192-CCM
    [162] => id-aes192-GCM
    [163] => id-aes192-wrap
    [164] => id-aes256-CCM
    [165] => id-aes256-GCM
    [166] => id-aes256-wrap
    [167] => id-smime-alg-CMS3DESwrap
    [168] => idea-cbc
    [169] => idea-cfb
    [170] => idea-ecb
    [171] => idea-ofb
    [172] => rc2-40-cbc
    [173] => rc2-64-cbc
    [174] => rc2-cbc
    [175] => rc2-cfb
    [176] => rc2-ecb
    [177] => rc2-ofb
    [178] => rc4
    [179] => rc4-40
    [180] => rc4-hmac-md5
    [181] => seed-cbc
    [182] => seed-cfb
    [183] => seed-ecb
    [184] => seed-ofb
)</code>
<code class="php">// 使用openssl加密数据
$string = openssl_encrypt('要加密的数据','des-cfb','秘钥des为8位',0,'IV des为8位');
var_dump($string);</code>

主要是des的算法,然后是发送一个请求,返回结果而已。
网上应该有类似的算法,推荐一个:
http://www.cnblogs.com/wangchuang/archiv...
(百度搜索,找到的)

跟其他语言交互的时候,对方语言直接先base64解密再进行des解密就可以获得原始数据了.python,ios是可以,其他语言没测试,即des_decrypt(base64_decrypt($pass_string))就可了

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.