php opensll加解密类

<?php

$pri = "-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCzJc4RrAqaH2Es02XQ91Cqp/JK0yX893JQwFT2eAusnwQ90Bwu
FQWdcZyKHP+I7XWMZc0qT/V/p51cq1d0OarPsuikth/43qP+1IWTYTv1i967pDnQ
jXKbnm1dEMvjmS5hi7b/WwNfTi/VdAGLN0nxiHlX4+czjNHA3pCSQ9hCzwIDAQAB
AoGAIF7aZ3MQz1yQMItaQHIPz2Zdb4vLt3P24gR+1bJHWb+6AnlSPjlqNFLNGF+5
BMLzlJToh5a/I3vkAVSsjsHEzUl8OpbZm10toxH9UMpGd+2OdVYpceurRzIZ9/MF
MuMBjv/+xukP4Nh8H6ekMGRMfoZtICAlj/kJxh7YlJlINfECQQDaarmknxzvw25k
0SyLVsPffL3PQLHUZonFrwksGmk0XlTxgr+oUapi4SGVWuBkOjt7x4w8ktkiAtOZ
tNtE3EALAkEA0flEzYg/cf170iUDtFEmWFLiBI8ajFC1rljaE4lFS67YCX+L1wnd
5iQy29gV0qcxKme4bes+eUw+Jmeue0UuzQJBAJMJW/ZV4y4Jm4ylQF6BR5VxLXqT
a+O4L7F0FzGoigBmLLr1MT8H0zODqY8ZQYxXo2m9uUuh9CiwTHDiTuZ5e2cCQGQ7
3ylSUh8jR1mAy+znFCvznOtaOO0kliYdSjUrQUYot/6IwYOstR3ox9KEq548q863
ZqYqN0+GIuEIe5Vi6D0CQQCp8fTMRCNtEAfRvYTV8PgxNE8pyIvntIeUT+MM+LGy
4Xh2zYs7BiXeGx5mIT57QSYUsQKidYcneFyTl81oSgTQ
-----END RSA PRIVATE KEY-----";

$pub = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCzJc4RrAqaH2Es02XQ91Cqp/JK
0yX893JQwFT2eAusnwQ90BwuFQWdcZyKHP+I7XWMZc0qT/V/p51cq1d0OarPsuik
th/43qP+1IWTYTv1i967pDnQjXKbnm1dEMvjmS5hi7b/WwNfTi/VdAGLN0nxiHlX
4+czjNHA3pCSQ9hCzwIDAQAB
-----END PUBLIC KEY-----";

$rsa = new mycrypt($pub,$pri);
$encryptContent = $rsa -> encrypt(‘hankangkang‘);
echo $encryptContent.‘<br>‘;
$decryptContent = $rsa -> decrypt($encryptContent);
echo $decryptContent.‘<br>‘;

class mycrypt {

public $pubkey;
public $privkey;

function __construct($pub,$pri) {
$this->pubkey = $pub;
$this->privkey = $pri;
}

public function encrypt($data) {
if (openssl_public_encrypt($data, $encrypted, $this->pubkey))
$data = base64_encode($encrypted);
else
throw new Exception(‘Unable to encrypt data. Perhaps it is bigger than the key size?‘);

return $data;
}

public function decrypt($data) {
if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey))
$data = $decrypted;
else
$data = ‘‘;

return $data;
}

}

时间: 2024-10-11 09:11:15

php opensll加解密类的相关文章

一个java的DES加解密类转换成C#

原文:一个java的DES加解密类转换成C# 一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //import java.util.regex.Pattern; //import java.util.Hashtable; import javax.crypto.*; import javax.crypto.spec.*; import sun.misc.*; /** * des加密解密 */ pu

PHP版3DES加解密类

<?php /** * * PHP版3DES加解密类 * * 可与java的3DES(DESede)加密方式兼容 * * @Author:蓝凤(ilanfeng.com) * * @version: V0.1 2011.02.18 * */ class Cc3des{ //加密的时候只用替换key就行了,ecb模式不需要提供iv值 public $key = "0123456789QWEQWEEWQQ1234"; public $iv = "33889955"

php rsa 非对称加解密类

<?php header("Content-Type: text/html;charset=utf-8"); /* 生成公钥.私钥对,私钥加密的内容能通过公钥解密(反过来亦可以) 下载开源RSA密钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,进入其中的bin目录,执行以下命令: openssl genrsa -out rsa_private_key.pem 1024 #生成原始 RSA私钥文件 rsa_private_key.pem opens

【基础分享】C#封装的几个加解密类

前段时间一个业余项目涉及到字符串和文件的加密,所以写了封装了几个给予DES,AES,MD5的加密解密类. 直接看代码 DesSecurity.cs /// <summary> /// 基于DES加密解密 /// </summary> public static class DesSecurity { public static byte[] Encrypt( byte[] data ,string key , bool ispaddingzeros = true, bool isE

php des 对称加解密类

<?php header("Content-Type: text/html;charset=utf-8"); /** * des 对称加解密 */ class des { private $key = ''; private $cipher = MCRYPT_DES; //加解密算法 private $modes = MCRYPT_MODE_ECB; //算法模式 private $iv = ''; //初始化向量 /** * 密钥 */ public function __co

DesUtils 加解密类使用

/** * 加密解密类 */ import javax.crypto.Cipher;import java.security.Key;import java.text.SimpleDateFormat;import java.util.Date; public class DesUtils { /** * 字符串默认键值 */ private static String strDefaultKey = "national"; /** * 加密工具 */ private Cipher e

Base64 加解密类的使用

/** * Created by Administrator on 2017/12/2 0002. */import java.io.UnsupportedEncodingException; public class Base64 { /** * Chunk size per RFC 2045 section 6.8. * <p/> * <p>The {@value} character limit does not count the trailing CRLF, but co

超强php加解密扩展:cryptopp

cryptopp是一个用c++编写的超强加解密扩展.性能优异可靠.完全可以替换所有PHP编写的加解密类.不仅如此,它的提供的编程接口非常简明易用,可说是PHP程序员必备扩展. cryptopp扩展的接口如下: class cryptopp { public function __construct($param); public function setkey($param); public function encode($param); public function decode($par

Java 使用AES/CBC/PKCS7Padding 加解密字符串

介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别要实现在java端用PKCS7Padding填充,需要用到bouncycastle组件来实现 所以需要一个jar 来支持.bcprov-jdk16-146.jar 下载地址:http://central.maven.org/maven2/org/bouncycastle/bcprov-jdk16/1.46/bcprov-jdk16-1.46.jar