openssl进行非对称加密和对称加密的php示例

确保开启php的openssl扩展:extension=php_openssl.dll

<?php

/**
 * @file
 * 作者:yunke url:http://blog.csdn.net/u011474028
 *
 */
header("Content-Type:text/html; charset=utf-8");

$key_file = "yunkeserver.key"; //私钥
$publickey_file = "yunkeserver.crt"; //证书文件
//$publickey_file="server.crt";//和私钥不匹配的证书文件
$data_file = "msg.txt"; //待加密数据文件

$private_key = openssl_get_privatekey(file_get_contents($key_file)); //获取私钥 非字符串类型  为资源类型
echo "私钥为:<br>" . $private_key . "<br><br>";
$public_key = openssl_get_publickey(file_get_contents($publickey_file)); //获取公钥 非字符串类型  为资源类型
echo "公钥为:<br>" . $public_key . "<br><br>";

/*
$str_key = ''; //KEY的字符串表示
if (openssl_pkey_export($private_key, $str_key)) { //需要正确配置openssl.cnf文件才可以成功
    echo "密钥的字符串表示为:<br>" . $str_key . "<br><br>";
} else {
    echo "密钥的字符串化失败:<br>";
    while ($msg = openssl_error_string()) {
        echo $msg . "<br />\n";
    }
    echo "<br />";
}
*/
 //print_r(openssl_pkey_get_details ( $public_key )); //输出密钥的详细信息 这里可以看出私钥里面是包含公钥的

$data = file_get_contents($data_file);
echo "明码数据为:<br>" . $data . "<br><br>";

$crypted_data = null; //密文
if (openssl_private_encrypt($data, $crypted_data, $private_key)) {
    echo "以下是私钥加密的密文:<br>" . $crypted_data . "<br><br>";
} else {
    echo "加密失败:<br>";
    while ($msg = openssl_error_string()) {
        echo $msg . "<br />\n";
    }
}
$decrypted_data = null; //加密还原后的明文
if (openssl_public_decrypt($crypted_data, $decrypted_data, $public_key)) {
    echo "以下是经过公钥解密后的明文:<br>" . $decrypted_data . "<br><br>";
} else {
    echo "解密失败:<br>";
    while ($msg = openssl_error_string()) {
        echo $msg . "<br />\n";
    }
}




//以上为非对称加密   以下演示对称加密

$key="123456"; //设置一个共享密码
$cipher='';//加密算法
$arr=openssl_get_cipher_methods(); //获取支持的加密算法 数组的key和值并没有对应关系
$cipher=$arr[20];
echo "选择的加密算法是:$cipher <br>\n";
$value=openssl_encrypt($data , $cipher,$key); //第四参数OPENSSL_RAW_DATA输出原始数据
echo "加密后的密文是:<br>".$value."<br><br>";
$old_data=openssl_decrypt ( $value,$cipher, $key);
echo "解密还原的明文:<br>".$old_data."<br><br>";

//print_r($arr);


































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 OpenSSL 进行对称加密和解密的示例代码: 加密: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> #define BITS 128 int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: %s <key> <plaintext>\n", argv[0]); return 1; } unsigned char key[BITS/8]; memset(key, 0, sizeof(key)); strncpy(key, argv[1], sizeof(key)); unsigned char plaintext[256]; memset(plaintext, 0, sizeof(plaintext)); strncpy(plaintext, argv[2], sizeof(plaintext)); unsigned char iv[AES_BLOCK_SIZE]; memset(iv, 0, sizeof(iv)); AES_KEY aes_key; AES_set_encrypt_key(key, BITS, &aes_key); unsigned char ciphertext[sizeof(plaintext)]; memset(ciphertext, 0, sizeof(ciphertext)); AES_cbc_encrypt(plaintext, ciphertext, sizeof(plaintext), &aes_key, iv, AES_ENCRYPT); printf("Ciphertext: %s\n", ciphertext); return 0; } ``` 解密: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> #define BITS 128 int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: %s <key> <ciphertext>\n", argv[0]); return 1; } unsigned char key[BITS/8]; memset(key, 0, sizeof(key)); strncpy(key, argv[1], sizeof(key)); unsigned char ciphertext[256]; memset(ciphertext, 0, sizeof(ciphertext)); strncpy(ciphertext, argv[2], sizeof(ciphertext)); unsigned char iv[AES_BLOCK_SIZE]; memset(iv, 0, sizeof(iv)); AES_KEY aes_key; AES_set_decrypt_key(key, BITS, &aes_key); unsigned char plaintext[sizeof(ciphertext)]; memset(plaintext, 0, sizeof(plaintext)); AES_cbc_encrypt(ciphertext, plaintext, sizeof(ciphertext), &aes_key, iv, AES_DECRYPT); printf("Plaintext: %s\n", plaintext); return 0; } ``` 这两个程序使用 CBC 模式进行加密和解密,并使用 128 位的 AES 密钥。使用时,分别传入密钥和需要加密/解密的文本即可。注意,加密后的密文可能包含不可打印字符,输出时需要进行转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值