栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用python加密数据,用php解密

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用python加密数据,用php解密

python加密

from Crypto.Cipher import AESimport base64import os# the block size for the cipher object; must be 16, 24, or 32 for AESBLOCK_SIZE = 32BLOCK_SZ = 14# the character used for padding--with a block cipher such as AES, the value# you encrypt must be a multiple of BLOCK_SIZE in length.  This character is# used to ensure that your value is always a multiple of BLOCK_SIZEPADDING = '{'# one-liner to sufficiently pad the text to be encryptedpad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING# one-liners to encrypt/enpre and decrypt/depre a string# encrypt with AES, enpre with base64EnpreAES = lambda c, s: base64.b64enpre(c.encrypt(pad(s)))DepreAES = lambda c, e: c.decrypt(base64.b64depre(e)).rstrip(PADDING)secret = "332SECRETabc1234"iv = "HELLOWORLD123456"cipher=AES.new(key=secret,mode=AES.MODE_CBC,IV=iv)my_text_to_enpre = "password"enpred = EnpreAES(cipher, my_text_to_enpre)print 'Encrypted string:', enpred

php解密(注意,编码的文本只是从上面的python打印中复制/粘贴的)

<?php$enc = "x3OZjCAL944N/awRHSrmRBy9P4VLTptbkFdEl2Ao8gk=";$secret = "332SECRETabc1234"; // same secret as python$iv="HELLOWORLD123456";  // same iv as python$padding = "{";  //same padding as pythonfunction decrypt_data($data, $iv, $key) {    $cypher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');    if(is_null($iv)) {        $ivlen = mcrypt_enc_get_iv_size($cypher);        $iv = substr($data, 0, $ivlen);        $data = substr($data, $ivlen);    }    // initialize encryption handle    if (mcrypt_generic_init($cypher, $key, $iv) != -1) { // decrypt $decrypted = mdecrypt_generic($cypher, $data); // clean up mcrypt_generic_deinit($cypher); mcrypt_module_close($cypher); return $decrypted;    }    return false;}$res = decrypt_data(base64_depre($enc), $iv, $secret);print rtrim($res,$padding);?>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/574820.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号