php openssl非对称加密解密实例,PHP基于openssl实现的非对称加密操作示例

本文实例讲述了PHP基于openssl实现的非对称加密操作。分享给大家供大家参考,具体如下:

使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密。

1.安装openssl和php的openssl扩展

2.生成私钥:openssl genrsa 用于生成rsa私钥文件,生成是可以指定私钥长度和密码保护

openssl genrsa -out rsa_private_key.pem 1024

3.生成公钥:rsa命令用于处理RSA密钥、格式转换和打印信息

openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

4.这里我们使用私钥加密,公钥解密

/**

* 密钥文件的路径

*/

$privateKeyFilePath = 'rsa_private_key.pem';

/**

* 公钥文件的路径

*/

$publicKeyFilePath = 'rsa_public_key.pem';

extension_loaded('openssl') or die('php需要openssl扩展支持');

(file_exists($privateKeyFilePath) && file_exists($publicKeyFilePath)) or die('密钥或者公钥的文件路径不正确');

/**

* 生成Resource类型的密钥,如果密钥文件内容被破坏,openssl_pkey_get_private函数返回false

*/

$privateKey = openssl_pkey_get_private(file_get_contents($privateKeyFilePath));

/**

* 生成Resource类型的公钥,如果公钥文件内容被破坏,openssl_pkey_get_public函数返回false

*/

$publicKey = openssl_pkey_get_public(file_get_contents($publicKeyFilePath));

($privateKey && $publicKey) or die('密钥或者公钥不可用');

/**

* 原数据

*/

$originalData = '加密前hahahaha';

/**

* 加密以后的数据,用于在网路上传输

*/

$encryptData = '';

echo '原数据为:', $originalData, PHP_EOL;

///用私钥加密

if (openssl_private_encrypt($originalData, $encryptData, $privateKey)) {

/**

* 加密后 可以base64_encode后方便在网址中传输 或者打印 否则打印为乱码

*/

echo '加密成功,加密后数据(base64_encode后)为:', base64_encode($encryptData), PHP_EOL;

} else {

die('加密失败');

}

///用公钥解密

/**

* 解密以后的数据

*/

$decryptData ='';

if (openssl_public_decrypt($encryptData, $decryptData, $publicKey)) {

echo '解密成功,解密后数据为:', $decryptData, PHP_EOL;

} else {

die('解密成功');

}

PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:

在线DES加密/解密工具http://tools.jb51.net/password/des_encode

MD5在线加密工具:http://tools.jb51.net/password/CreateMD5Password

在线散列/哈希算法加密工具:http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:http://tools.jb51.net/password/sha_encode

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php加密方法总结》、《PHP编码与转码操作技巧汇总》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《php正则表达式用法总结》

希望本文所述对大家PHP程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,实现了基于 OpenSSL对称加密解密: ```c++ #include <iostream> #include <openssl/evp.h> #include <openssl/aes.h> #include <openssl/rand.h> #define KEY_SIZE 32 #define IV_SIZE 16 int aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int len, ciphertext_len; if (!(ctx = EVP_CIPHER_CTX_new())) return -1; if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv)) return -1; if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) return -1; ciphertext_len = len; if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) return -1; ciphertext_len += len; EVP_CIPHER_CTX_free(ctx); return ciphertext_len; } int aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) { EVP_CIPHER_CTX *ctx; int len, plaintext_len, ret; if (!(ctx = EVP_CIPHER_CTX_new())) return -1; if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, key, iv)) return -1; if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) return -1; plaintext_len = len; ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len); EVP_CIPHER_CTX_free(ctx); if (ret > 0) { plaintext_len += len; return plaintext_len; } else { return -1; } } int main() { unsigned char key[KEY_SIZE], iv[IV_SIZE]; unsigned char plaintext[] = "Hello, world!"; unsigned char ciphertext[sizeof(plaintext) + AES_BLOCK_SIZE]; unsigned char decryptedtext[sizeof(plaintext)]; int decryptedtext_len, ciphertext_len; // Generate random key and iv RAND_bytes(key, KEY_SIZE); RAND_bytes(iv, IV_SIZE); // Encrypt the plaintext ciphertext_len = aes_encrypt(plaintext, sizeof(plaintext), key, iv, ciphertext); // Decrypt the ciphertext decryptedtext_len = aes_decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext); // Print the results std::cout << "Plaintext: " << plaintext << std::endl; std::cout << "Ciphertext: " << ciphertext << std::endl; std::cout << "Decrypted text: " << decryptedtext << std::endl; return 0; } ``` 该代码使用了 AES-256-CBC 对称加密算法,生成一个随机的 256 位密钥和 128 位 IV。加密解密时使用相同的密钥和 IV。注意,此代码中的明文和密文都不包括终止符。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值