php crypt mysql password_php crypt函数加密和解密的实例分享

php crypt函数加密与解密,代码如下:

// +-----------------------

// | Wiki Framework |

// +-----------------------

// $string 明文 或 密文 必填

// $isEncrypt 是否加密 可选,默认为加密

// $key 密匙 可选,默认为空格

// $b = dencrypt($a,TRUE,'123');

// $c = dencrypt($b,false,'123');

// 采用SHA1生成密匙簿,超过300个字符使用ZLIB压缩

//by www.jbxue.com

function dencrypt($string, $isEncrypt = true, $key = KEY_SPACE) {

if (!isset($string{0}) || !isset($key{0})) {

return false;

}

$dynKey = $isEncrypt ? hash('sha1', microtime(true)) : substr($string, 0, 40);

$fixedKey = hash('sha1', $key);

$dynKeyPart1 = substr($dynKey, 0, 20);

$dynKeyPart2 = substr($dynKey, 20);

$fixedKeyPart1 = substr($fixedKey, 0, 20);

$fixedKeyPart2 = substr($fixedKey, 20);

$key = hash('sha1', $dynKeyPart1 . $fixedKeyPart1 . $dynKeyPart2 . $fixedKeyPart2);

$string = $isEncrypt ? $fixedKeyPart1 . $string . $dynKeyPart2 : (isset($string{339}) ?

gzuncompress(base64_decode(substr($string, 40))) : base64_decode(substr($string, 40)));

$n = 0;

$result = '';

$len = strlen($string);

for ($n = 0; $n < $len; $n++) {

$result .= chr(ord($string{$n}) ^ ord($key{$n % 40}));

}

return $isEncrypt ? $dynKey . str_replace('=', '', base64_encode($n > 299 ? gzcompress($result) : $result)) :

substr($result, 20, -20);

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Mbed TLS库中的`mbedtls_cipher_crypt`来实现AES-256-CBC-PKCS#7的加密解密功能,你需要按照以下步骤进行: 1. **初始化Mbed TLS库**:在使用Mbed TLS库之前,你需要调用`mbedtls_cipher_init`和`mbedtls_cipher_setup`函数来初始化加密上下文。 2. **设置加密算法和模式**:使用`mbedtls_cipher_setkey`函数来设置加密算法和密钥,以及使用`mbedtls_cipher_set_iv`函数来设置初始向量(IV)。 3. **选择加密还是解密模式**:根据你的需求,选择加密模式或解密模式。可以使用`MBEDTLS_ENCRYPT`参数表示加密模式,或者使用`MBEDTLS_DECRYPT`参数表示解密模式。 4. **执行加密解密操作**:调用`mbedtls_cipher_crypt`函数来执行加密解密操作。将输入数据(明文或密文)和输出缓冲区的地址传递给该函数。 5. **清理资源**:在完成加密解密操作后,调用`mbedtls_cipher_free`函数释放加密上下文。 下面是一个示例代码,展示如何使用Mbed TLS库的`mbedtls_cipher_crypt`函数进行AES-256-CBC-PKCS#7的加密解密: ```c #include "mbedtls/cipher.h" #define KEY_SIZE 32 // AES-256使用32字节的密钥 #define IV_SIZE 16 // 使用16字节的初始向量 void aes_256_cbc_encrypt(const unsigned char *key, const unsigned char *iv, const unsigned char *input, unsigned char *output, size_t input_len) { mbedtls_cipher_context_t ctx; unsigned char padding[16 - (input_len % 16)]; // PKCS#7 填充 mbedtls_cipher_init(&ctx); mbedtls_cipher_setup(&ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC)); mbedtls_cipher_setkey(&ctx, key, KEY_SIZE * 8, MBEDTLS_ENCRYPT); mbedtls_cipher_set_iv(&ctx, iv, IV_SIZE); mbedtls_cipher_crypt(&ctx, iv, IV_SIZE, input, input_len, output); mbedtls_cipher_free(&ctx); } void aes_256_cbc_decrypt(const unsigned char *key, const unsigned char *iv, const unsigned char *input, unsigned char *output, size_t input_len) { mbedtls_cipher_context_t ctx; mbedtls_cipher_init(&ctx); mbedtls_cipher_setup(&ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC)); mbedtls_cipher_setkey(&ctx, key, KEY_SIZE * 8, MBEDTLS_DECRYPT); mbedtls_cipher_set_iv(&ctx, iv, IV_SIZE); mbedtls_cipher_crypt(&ctx, iv, IV_SIZE, input, input_len, output); mbedtls_cipher_free(&ctx); } ``` 在上述示例代码中,`aes_256_cbc_encrypt`函数用于执行AES-256-CBC-PKCS#7的加密操作,`aes_256_cbc_decrypt`函数用于执行解密操作。你需要提供密钥、初始向量、输入数据和输出缓冲区。注意,输入数据的长度必须是16的倍数,否则需要进行PKCS#7填充。 请注意,示例代码仅用于演示加密解密操作的基本流程,实际使用时还应考虑错误处理、内存管理和适用于你的特定环境的其他安全措施。确保在使用Mbed TLS库时参考其文档和示例代码,以了解更多关于使用`mbedtls_cipher_crypt`函数的详细信息和最佳实践。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值