pem加密php,PHP :分享一个公钥/私钥加密类

基础代码<?php

/**

* @author: suifengtec coolwp.com

* @date:   2015-07-24 09:22:18

* @last Modified by:   suifengtec coolwp.com

* @last Modified time: 2015-07-24 10:22:00

*/

final class Encryption_a1{

protected static $_instance = null;

protected static $private_key = null;

protected static $public_pem = null;

public static function instance() {

if ( is_null( self::$_instance ) ) {

self::$_instance = new self();

}

return self::$_instance;

}

public function __clone() {

_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'cwp' ), '0.9.0' );

}

public function __wakeup() {

_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'cwp' ), '0.9.0' );

}

public function __construct() {

if ( is_null( self::$public_pem ) ) {

$public_pem = getcwd().'../_key/public.pem';

self::$public_pem =  openssl_get_publickey( file_get_contents( $public_pem ) );

}

if ( is_null( self::$private_key ) ) {

$private_key = getcwd().'../_key/private.key';

self::$private_key = openssl_get_privatekey( file_get_contents( $private_key ) );

}

}

public function en($data){

openssl_seal($data, $encrypted, $e, array(self::$public_pem));

$r = array(

'pwd'  => base64_encode($encrypted),

'data' => base64_encode($e[0]),

);

return $r;

}

public function de($args){

if(!is_array($args)||!isset($args['data'])||!isset($args['pwd'])){

return false;

}

extract($args);

$pwd = base64_decode($pwd);

$data = base64_decode($data);

$r = null;

openssl_open($pwd, $r, $data, self::$private_key);

return $r;

}

}/*//CLASS*/

$GLOBALS['Encryption_a1'] = Encryption_a1::instance();

?>

使用require_once(getcwd().'/class-m-1.php');

global $Encryption_a1;

$m = $Encryption_a1;

$str = '被加密的数据在这里!R#%$#^$#T^$#!!!!!';

echo '
要被加密的数据:
 '.$str;

$data1 = $m->en($str);

echo '
加密后返回的数组:
 ';

print_r($data1);

echo '
解密后:
 ';

$str1 = $m->de($data1);

echo $str1;

同志们,信息加密传递迫在眉睫啊!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于您提出的问题,我可以为您解答。在Java中,您可以使用BouncyCastle库来实现从pem文件读取公钥私钥,以及对数据进行加密和解密的功能。 首先,您需要通过以下命令将BouncyCastle库导入您的项目中: ``` <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.68</version> </dependency> ``` 然后,您可以使用以下代码实现从pem文件读取公钥: ``` Security.addProvider(new BouncyCastleProvider()); FileReader fileReader = new FileReader("public_key.pem"); PEMParser pemParser = new PEMParser(fileReader); SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(pemParser.readObject()); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyInfo.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); ``` 同样地,您也可以使用以下代码实现从pem文件读取私钥: ``` Security.addProvider(new BouncyCastleProvider()); FileReader fileReader = new FileReader("private_key.pem"); PEMParser pemParser = new PEMParser(fileReader); Object object = pemParser.readObject(); JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC"); PrivateKey privateKey = null; if (object instanceof PEMKeyPair) { PEMKeyPair pemKeyPair = (PEMKeyPair) object; privateKey = converter.getPrivateKey(pemKeyPair.getPrivateKeyInfo()); } else if (object instanceof PEMEncryptedKeyPair) { PEMEncryptedKeyPair pemEncryptedKeyPair = (PEMEncryptedKeyPair) object; PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build("your_password".toCharArray()); PEMKeyPair pemKeyPair = pemEncryptedKeyPair.decryptKeyPair(decProv); privateKey = converter.getPrivateKey(pemKeyPair.getPrivateKeyInfo()); } ``` 接下来,您可以使用以下代码实现数据加密: ``` Security.addProvider(new BouncyCastleProvider()); byte[] data = "Hello World!".getBytes(); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedData = cipher.doFinal(data); ``` 最后,您可以使用以下代码实现数据解密: ``` Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptedData = cipher.doFinal(encryptedData); ``` 希望这可以帮助您解决问题!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值