2020年9月20日13:04:47
为什么需要加密解密算法?
数据加密的基本过程就是对原来为明文的文件或数据按某种算法进行处理,使其成为不可读的一段代码为“密文”,
使其只能在输入相应的密钥之后才能显示出原容,通过这样的途径来达到保护数据不被非法人窃取、阅读的目的。
该过程的逆过程为解密,即将该编码信息转化为其原来数据的过程。
比如api接口,数据传输,用户密码加密等、
php常用算法
环境 php 7.2.9
php的官方的加密解密扩展
https://www.php.net/manual/zh/refs.crypto.php
$p = ‘rasmuslerdorf‘; p(md5($p)); // $hash = password_hash($p, PASSWORD_DEFAULT); $hash = password_hash($p, PASSWORD_BCRYPT); p($hash); // $options = [ // ‘cost‘ => 12, // ]; // echo password_hash($p, PASSWORD_BCRYPT, $options); if (password_verify($p, $hash)) { echo ‘Password is ok!‘; } else { echo ‘Invalid password.‘; } p(hash_algos()); $bytes = random_bytes(5); p(bin2hex($bytes)); p(random_int(100, 999));
上面是几个比较常用的关于密码的加算法
如果你需要更多强大的请查看
https://www.php.net/manual/zh/function.openssl-encrypt.php
php基本把现存所有的openssl加密算法全部移植过来了,真的很不错
$ciphers = openssl_get_cipher_methods(); $ciphers_and_aliases = openssl_get_cipher_methods(true); $cipher_aliases = array_diff($ciphers_and_aliases, $ciphers); p($ciphers); p($ciphers_and_aliases); p($cipher_aliases);
Array ( [0] => AES-128-CBC [1] => AES-128-CBC-HMAC-SHA1 [2] => AES-128-CBC-HMAC-SHA256 [3] => AES-128-CFB [4] => AES-128-CFB1 [5] => AES-128-CFB8 [6] => AES-128-CTR [7] => AES-128-ECB [8] => AES-128-OCB [9] => AES-128-OFB [10] => AES-128-XTS [11] => AES-192-CBC [12] => AES-192-CFB [13] => AES-192-CFB1 [14] => AES-192-CFB8 [15] => AES-192-CTR [16] => AES-192-ECB [17] => AES-192-OCB [18] => AES-192-OFB [19] => AES-256-CBC [20] => AES-256-CBC-HMAC-SHA1 [21] => AES-256-CBC-HMAC-SHA256 [22] => AES-256-CFB [23] => AES-256-CFB1 [24] => AES-256-CFB8 [25] => AES-256-CTR [26] => AES-256-ECB [27] => AES-256-OCB [28] => AES-256-OFB [29] => AES-256-XTS [30] => BF-CBC [31] => BF-CFB [32] => BF-ECB [33] => BF-OFB [34] => CAMELLIA-128-CBC [35] => CAMELLIA-128-CFB [36] => CAMELLIA-128-CFB1 [37] => CAMELLIA-128-CFB8 [38] => CAMELLIA-128-CTR [39] => CAMELLIA-128-ECB [40] => CAMELLIA-128-OFB [41] => CAMELLIA-192-CBC [42] => CAMELLIA-192-CFB [43] => CAMELLIA-192-CFB1 [44] => CAMELLIA-192-CFB8 [45] => CAMELLIA-192-CTR [46] => CAMELLIA-192-ECB [47] => CAMELLIA-192-OFB [48] => CAMELLIA-256-CBC [49] => CAMELLIA-256-CFB [50] => CAMELLIA-256-CFB1 [51] => CAMELLIA-256-CFB8 [52] => CAMELLIA-256-CTR [53] => CAMELLIA-256-ECB [54] => CAMELLIA-256-OFB [55] => CAST5-CBC [56] => CAST5-CFB [57] => CAST5-ECB [58] => CAST5-OFB [59] => ChaCha20 [60] => ChaCha20-Poly1305 [61] => DES-CBC [62] => DES-CFB [63] => DES-CFB1 [64] => DES-CFB8 [65] => DES-ECB [66] => DES-EDE [67] => DES-EDE-CBC [68] => DES-EDE-CFB [69] => DES-EDE-OFB [70] => DES-EDE3 [71] => DES-EDE3-CBC [72] => DES-EDE3-CFB [73] => DES-EDE3-CFB1 [74] => DES-EDE3-CFB8 [75] => DES-EDE3-OFB [76] => DES-OFB [77] => DESX-CBC [78] => IDEA-CBC [79] => IDEA-CFB [80] => IDEA-ECB [81] => IDEA-OFB [82] => RC2-40-CBC [83] => RC2-64-CBC [84] => RC2-CBC [85] => RC2-CFB [86] => RC2-ECB [87] => RC2-OFB [88] => RC4 [89] => RC4-40 [90] => RC4-HMAC-MD5 [91] => SEED-CBC [92] => SEED-CFB [93] => SEED-ECB [94] => SEED-OFB [95] => aes-128-cbc [96] => aes-128-cbc-hmac-sha1 [97] => aes-128-cbc-hmac-sha256 [98] => aes-128-ccm [99] => aes-128-cfb [100] => aes-128-cfb1 [101] => aes-128-cfb8 [102] => aes-128-ctr [103] => aes-128-ecb [104] => aes-128-gcm [105] => aes-128-ocb [106] => aes-128-ofb [107] => aes-128-xts [108] => aes-192-cbc [109] => aes-192-ccm [110] => aes-192-cfb [111] => aes-192-cfb1 [112] => aes-192-cfb8 [113] => aes-192-ctr [114] => aes-192-ecb [115] => aes-192-gcm [116] => aes-192-ocb [117] => aes-192-ofb [118] => aes-256-cbc [119] => aes-256-cbc-hmac-sha1 [120] => aes-256-cbc-hmac-sha256 [121] => aes-256-ccm [122] => aes-256-cfb [123] => aes-256-cfb1 [124] => aes-256-cfb8 [125] => aes-256-ctr [126] => aes-256-ecb [127] => aes-256-gcm [128] => aes-256-ocb [129] => aes-256-ofb [130] => aes-256-xts [131] => bf-cbc [132] => bf-cfb [133] => bf-ecb [134] => bf-ofb [135] => camellia-128-cbc [136] => camellia-128-cfb [137] => camellia-128-cfb1 [138] => camellia-128-cfb8 [139] => camellia-128-ctr [140] => camellia-128-ecb [141] => camellia-128-ofb [142] => camellia-192-cbc [143] => camellia-192-cfb [144] => camellia-192-cfb1 [145] => camellia-192-cfb8 [146] => camellia-192-ctr [147] => camellia-192-ecb [148] => camellia-192-ofb [149] => camellia-256-cbc [150] => camellia-256-cfb [151] => camellia-256-cfb1 [152] => camellia-256-cfb8 [153] => camellia-256-ctr [154] => camellia-256-ecb [155] => camellia-256-ofb [156] => cast5-cbc [157] => cast5-cfb [158] => cast5-ecb [159] => cast5-ofb [160] => chacha20 [161] => chacha20-poly1305 [162] => des-cbc [163] => des-cfb [164] => des-cfb1 [165] => des-cfb8 [166] => des-ecb [167] => des-ede [168] => des-ede-cbc [169] => des-ede-cfb [170] => des-ede-ofb [171] => des-ede3 [172] => des-ede3-cbc [173] => des-ede3-cfb [174] => des-ede3-cfb1 [175] => des-ede3-cfb8 [176] => des-ede3-ofb [177] => des-ofb [178] => desx-cbc [179] => id-aes128-CCM [180] => id-aes128-GCM [181] => id-aes128-wrap [182] => id-aes128-wrap-pad [183] => id-aes192-CCM [184] => id-aes192-GCM [185] => id-aes192-wrap [186] => id-aes192-wrap-pad [187] => id-aes256-CCM [188] => id-aes256-GCM [189] => id-aes256-wrap [190] => id-aes256-wrap-pad [191] => id-smime-alg-CMS3DESwrap [192] => idea-cbc [193] => idea-cfb [194] => idea-ecb [195] => idea-ofb [196] => rc2-40-cbc [197] => rc2-64-cbc [198] => rc2-cbc [199] => rc2-cfb [200] => rc2-ecb [201] => rc2-ofb [202] => rc4 [203] => rc4-40 [204] => rc4-hmac-md5 [205] => seed-cbc [206] => seed-cfb [207] => seed-ecb [208] => seed-ofb )
Array ( [0] => AES-128-CBC [1] => AES-128-CBC-HMAC-SHA1 [2] => AES-128-CBC-HMAC-SHA256 [3] => AES-128-CFB [4] => AES-128-CFB1 [5] => AES-128-CFB8 [6] => AES-128-CTR [7] => AES-128-ECB [8] => AES-128-OCB [9] => AES-128-OFB [10] => AES-128-XTS [11] => AES-192-CBC [12] => AES-192-CFB [13] => AES-192-CFB1 [14] => AES-192-CFB8 [15] => AES-192-CTR [16] => AES-192-ECB [17] => AES-192-OCB [18] => AES-192-OFB [19] => AES-256-CBC [20] => AES-256-CBC-HMAC-SHA1 [21] => AES-256-CBC-HMAC-SHA256 [22] => AES-256-CFB [23] => AES-256-CFB1 [24] => AES-256-CFB8 [25] => AES-256-CTR [26] => AES-256-ECB [27] => AES-256-OCB [28] => AES-256-OFB [29] => AES-256-XTS [30] => AES128 [31] => AES192 [32] => AES256 [33] => BF [34] => BF-CBC [35] => BF-CFB [36] => BF-ECB [37] => BF-OFB [38] => CAMELLIA-128-CBC [39] => CAMELLIA-128-CFB [40] => CAMELLIA-128-CFB1 [41] => CAMELLIA-128-CFB8 [42] => CAMELLIA-128-CTR [43] => CAMELLIA-128-ECB [44] => CAMELLIA-128-OFB [45] => CAMELLIA-192-CBC [46] => CAMELLIA-192-CFB [47] => CAMELLIA-192-CFB1 [48] => CAMELLIA-192-CFB8 [49] => CAMELLIA-192-CTR [50] => CAMELLIA-192-ECB [51] => CAMELLIA-192-OFB [52] => CAMELLIA-256-CBC [53] => CAMELLIA-256-CFB [54] => CAMELLIA-256-CFB1 [55] => CAMELLIA-256-CFB8 [56] => CAMELLIA-256-CTR [57] => CAMELLIA-256-ECB [58] => CAMELLIA-256-OFB [59] => CAMELLIA128 [60] => CAMELLIA192 [61] => CAMELLIA256 [62] => CAST [63] => CAST-cbc [64] => CAST5-CBC [65] => CAST5-CFB [66] => CAST5-ECB [67] => CAST5-OFB [68] => ChaCha20 [69] => ChaCha20-Poly1305 [70] => DES [71] => DES-CBC [72] => DES-CFB [73] => DES-CFB1 [74] => DES-CFB8 [75] => DES-ECB [76] => DES-EDE [77] => DES-EDE-CBC [78] => DES-EDE-CFB [79] => DES-EDE-ECB [80] => DES-EDE-OFB [81] => DES-EDE3 [82] => DES-EDE3-CBC [83] => DES-EDE3-CFB [84] => DES-EDE3-CFB1 [85] => DES-EDE3-CFB8 [86] => DES-EDE3-ECB [87] => DES-EDE3-OFB [88] => DES-OFB [89] => DES3 [90] => DESX [91] => DESX-CBC [92] => IDEA [93] => IDEA-CBC [94] => IDEA-CFB [95] => IDEA-ECB [96] => IDEA-OFB [97] => RC2 [98] => RC2-40-CBC [99] => RC2-64-CBC [100] => RC2-CBC [101] => RC2-CFB [102] => RC2-ECB [103] => RC2-OFB [104] => RC4 [105] => RC4-40 [106] => RC4-HMAC-MD5 [107] => SEED [108] => SEED-CBC [109] => SEED-CFB [110] => SEED-ECB [111] => SEED-OFB [112] => aes-128-cbc [113] => aes-128-cbc-hmac-sha1 [114] => aes-128-cbc-hmac-sha256 [115] => aes-128-ccm [116] => aes-128-cfb [117] => aes-128-cfb1 [118] => aes-128-cfb8 [119] => aes-128-ctr [120] => aes-128-ecb [121] => aes-128-gcm [122] => aes-128-ocb [123] => aes-128-ofb [124] => aes-128-xts [125] => aes-192-cbc [126] => aes-192-ccm [127] => aes-192-cfb [128] => aes-192-cfb1 [129] => aes-192-cfb8 [130] => aes-192-ctr [131] => aes-192-ecb [132] => aes-192-gcm [133] => aes-192-ocb [134] => aes-192-ofb [135] => aes-256-cbc [136] => aes-256-cbc-hmac-sha1 [137] => aes-256-cbc-hmac-sha256 [138] => aes-256-ccm [139] => aes-256-cfb [140] => aes-256-cfb1 [141] => aes-256-cfb8 [142] => aes-256-ctr [143] => aes-256-ecb [144] => aes-256-gcm [145] => aes-256-ocb [146] => aes-256-ofb [147] => aes-256-xts [148] => aes128 [149] => aes128-wrap [150] => aes192 [151] => aes192-wrap [152] => aes256 [153] => aes256-wrap [154] => bf [155] => bf-cbc [156] => bf-cfb [157] => bf-ecb [158] => bf-ofb [159] => blowfish [160] => camellia-128-cbc [161] => camellia-128-cfb [162] => camellia-128-cfb1 [163] => camellia-128-cfb8 [164] => camellia-128-ctr [165] => camellia-128-ecb [166] => camellia-128-ofb [167] => camellia-192-cbc [168] => camellia-192-cfb [169] => camellia-192-cfb1 [170] => camellia-192-cfb8 [171] => camellia-192-ctr [172] => camellia-192-ecb [173] => camellia-192-ofb [174] => camellia-256-cbc [175] => camellia-256-cfb [176] => camellia-256-cfb1 [177] => camellia-256-cfb8 [178] => camellia-256-ctr [179] => camellia-256-ecb [180] => camellia-256-ofb [181] => camellia128 [182] => camellia192 [183] => camellia256 [184] => cast [185] => cast-cbc [186] => cast5-cbc [187] => cast5-cfb [188] => cast5-ecb [189] => cast5-ofb [190] => chacha20 [191] => chacha20-poly1305 [192] => des [193] => des-cbc [194] => des-cfb [195] => des-cfb1 [196] => des-cfb8 [197] => des-ecb [198] => des-ede [199] => des-ede-cbc [200] => des-ede-cfb [201] => des-ede-ecb [202] => des-ede-ofb [203] => des-ede3 [204] => des-ede3-cbc [205] => des-ede3-cfb [206] => des-ede3-cfb1 [207] => des-ede3-cfb8 [208] => des-ede3-ecb [209] => des-ede3-ofb [210] => des-ofb [211] => des3 [212] => des3-wrap [213] => desx [214] => desx-cbc [215] => id-aes128-CCM [216] => id-aes128-GCM [217] => id-aes128-wrap [218] => id-aes128-wrap-pad [219] => id-aes192-CCM [220] => id-aes192-GCM [221] => id-aes192-wrap [222] => id-aes192-wrap-pad [223] => id-aes256-CCM [224] => id-aes256-GCM [225] => id-aes256-wrap [226] => id-aes256-wrap-pad [227] => id-smime-alg-CMS3DESwrap [228] => idea [229] => idea-cbc [230] => idea-cfb [231] => idea-ecb [232] => idea-ofb [233] => rc2 [234] => rc2-128 [235] => rc2-40 [236] => rc2-40-cbc [237] => rc2-64 [238] => rc2-64-cbc [239] => rc2-cbc [240] => rc2-cfb [241] => rc2-ecb [242] => rc2-ofb [243] => rc4 [244] => rc4-40 [245] => rc4-hmac-md5 [246] => seed [247] => seed-cbc [248] => seed-cfb [249] => seed-ecb [250] => seed-ofb )
c/cpp的加密解密算法库
https://github.com/openssl/openssl/tree/master/crypto
可以参考对照下基本全部移植过来了
还一个就是非常知名的Crypto++
官网 https://www.cryptopp.com/
https://github.com/weidai11/cryptopp
原文:https://www.cnblogs.com/zx-admin/p/13699793.html