php hex加密解密方法,php hex加密解密函数使用例子

加密解密

//Hex(Hex(Md5((原文+密钥).getBytes(“utf-8”))).getBytes(“utf-8”))

function String2Hex($string){

$hex='';

for ($i=0; $i < strlen($string); $i++){

$hex .= dechex(ord($string[$i]));

}

return $hex;

}

function Hex2String($hex){

$string='';

for ($i=0; $i < strlen($hex)-1; $i+=2){

$string .= chr(hexdec($hex[$i].$hex[$i+1]));

}

return $string;

}

// example:

$hex = String2Hex("test sentence...");

$data = array(

'appId' => 'SHAN-GUO-CHI-BI',

'data' =>'{"appId":"SHAN-GUO-CHI-BI","cardNo":"6006012000000001"}',

'sig'=>'4331443139334331313645464642444544364334343742374236364335454243',

);

$key = '111111111111111111111111';

$data['sig'] = String2Hex(strtoupper(md5($data['data'] . $key)));

例子

PHP 转换输出字符串为 Hex Oct

通过下面代码,可以把PHP输出的字符串换行为16进制和8进制。

通过上述代码,把字符串 "http://www.111com.net" 转换为"\x68\164\x74\160\x3a\57\x2f\142\x6c\157\x67\56\x6c\151\x78\151\x70\150\x70\56\x63\157\x6d"

PHP实现来基数位字符串转换十六进制,偶数位字符串转换为八进制。

下图是ASCII码表,可以简单的核对10进制、8进制和16进制与127常用字符的关系。

2016040109293698.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
hex编码实现AES加密解密功能示例可以按照以下步骤进行: 1. 导入所需的模块: ```python import binascii from Crypto.Cipher import AES from Crypto.Random import get_random_bytes ``` 2. 定义AES加密和解密函数: ```python def encrypt(plain_text, key): cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(plain_text.encode()) return binascii.hexlify(nonce + ciphertext + tag).decode() def decrypt(cipher_text, key): cipher_data = binascii.unhexlify(cipher_text) nonce = cipher_data[:16] ciphertext = cipher_data[16:-16] tag = cipher_data[-16:] cipher = AES.new(key, AES.MODE_EAX, nonce) decrypted_text = cipher.decrypt_and_verify(ciphertext, tag) return decrypted_text.decode() ``` 3. 生成随机密钥: ```python key = get_random_bytes(16) ``` 4. 加密和解密示例数据: ```python plain_text = "This is a sample plaintext." cipher_text = encrypt(plain_text, key) decrypted_text = decrypt(cipher_text, key) ``` 完整的代码示例如下: ```python import binascii from Crypto.Cipher import AES from Crypto.Random import get_random_bytes def encrypt(plain_text, key): cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(plain_text.encode()) return binascii.hexlify(nonce + ciphertext + tag).decode() def decrypt(cipher_text, key): cipher_data = binascii.unhexlify(cipher_text) nonce = cipher_data[:16] ciphertext = cipher_data[16:-16] tag = cipher_data[-16:] cipher = AES.new(key, AES.MODE_EAX, nonce) decrypted_text = cipher.decrypt_and_verify(ciphertext, tag) return decrypted_text.decode() key = get_random_bytes(16) plain_text = "This is a sample plaintext." cipher_text = encrypt(plain_text, key) decrypted_text = decrypt(cipher_text, key) print("Plain text:", plain_text) print("Encrypted text:", cipher_text) print("Decrypted text:", decrypted_text) ``` 这样,就可以使用hex编码实现AES加密解密功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值