Skip to content

Commit e0aa349

Browse files
committed
初始化代码
1 parent 4c2f9ea commit e0aa349

File tree

6 files changed

+300
-0
lines changed

6 files changed

+300
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "qinqw/php-crypto",
3+
"description": "Provides an easy and secure way to encrypt/decrypt data with PHP using the Symmetric Cryptography method.(DES,RSA)",
4+
"homepage": "http://www.qinqiwei.com",
5+
"license": "Apache-2.0",
6+
"keywords": ["Crypt PHP DES 3DES RSA OPENSSL Qinqw"],
7+
"type": "project",
8+
"authors": [
9+
{
10+
"name": "Kevin",
11+
"email": "qinqiwei@hotmail.com"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.5.0"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Qinqw\\Crypto\\": "src/"
20+
}
21+
}
22+
}

examples/bootstrap.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Provides an easy and secure way to encrypt/decrypt data with PHP
4+
* using the Symmetric Cryptography method.(DES,3DES,OPENSSL)
5+
* bootstrap
6+
*
7+
* @category PHP_Crypto
8+
* @package Qinqw\Crypto
9+
* @author Kevin <qinqiwei@hotmail.com>
10+
* @license Apache License V2
11+
* @link https://github.com/qinqw/php-crypto
12+
*/
13+
14+
spl_autoload_register(
15+
function ($class) {
16+
// project-specific namespace prefix
17+
$prefix = 'Qinqw\\Crypto';
18+
19+
// base directory for the namespace prefix
20+
$base_dir = dirname(__DIR__) . '/src/';
21+
22+
// does the class use the namespace prefix?
23+
$len = strlen($prefix);
24+
if (strncmp($prefix, $class, $len) !== 0) {
25+
// no, move to the next registered autoloader
26+
return;
27+
}
28+
29+
// get the relative class name
30+
$relative_class = substr($class, $len);
31+
32+
// replace the namespace prefix with the base directory, replace namespace
33+
// separators with directory separators in the relative class name, append
34+
// with .php
35+
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
36+
37+
// if the file exists, require it
38+
if (file_exists($file)) {
39+
//require $file;
40+
include $file;
41+
}
42+
}
43+
);

examples/des.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Provides an easy and secure way to encrypt/decrypt data with PHP
4+
* using the Symmetric Cryptography method.(DES,3DES,OPENSSL)
5+
*
6+
* @category PHP_Crypto
7+
* @package Qinqw\Crypto
8+
* @author Kevin <qinqiwei@hotmail.com>
9+
* @license Apache License V2
10+
* @link https://github.com/qinqw/php-crypto
11+
*/
12+
13+
require 'bootstrap.php';
14+
use Qinqw\Crypto\DES;
15+
16+
$key = 'qinqiwei';
17+
$plaintext = 'aaaaaallllll';
18+
19+
$encrypt =DES::encrypt($plaintext,$key);
20+
var_dump($encrypt);
21+
22+
23+
$decrypt = DES::decrypt($encrypt,$key);
24+
25+
var_dump($decrypt);
26+
27+
$encrypt= 'igCw5Heuc1M=';
28+
$decrypt = DES::decrypt($encrypt);
29+
var_dump($decrypt);
30+
31+
// $ciphers_and_aliases = openssl_get_cipher_methods(true);
32+
// var_dump($ciphers_and_aliases);

src/DES.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Provides an easy and secure way to encrypt/decrypt data with PHP
4+
* using the Symmetric Cryptography method.(DES,3DES,OPENSSL)
5+
* bootstrap
6+
*
7+
* @category PHP_Crypto
8+
* @package Qinqw\Crypto
9+
* @author Kevin <qinqiwei@hotmail.com>
10+
* @license Apache License V2
11+
* @link https://github.com/qinqw/php-crypto
12+
*/
13+
namespace Qinqw\Crypto;
14+
15+
class DES
16+
{
17+
/**
18+
* des-cbc加密
19+
* @param string $data 要被加密的数据
20+
* @param string $key 加密使用的key
21+
*/
22+
public static function encrypt($data, $key=null)
23+
{
24+
if(strlen($key)!=8)
25+
{
26+
$key='x#a-y6nl';
27+
}
28+
$cipher = 'des-cbc';
29+
$ivlen = openssl_cipher_iv_length($cipher);
30+
$iv = $key;
31+
$options = 0;
32+
return openssl_encrypt($data, $cipher, $key, $options, $iv);
33+
}
34+
35+
/**
36+
* des-cbc解密
37+
* @param string $data 加密数据
38+
* @param string $key 加密使用的key
39+
*/
40+
public static function decrypt($data, $key=null)
41+
{
42+
if(strlen($key)!=8)
43+
{
44+
$key='x#a-y6nl';
45+
}
46+
$cipher = 'des-cbc';
47+
$ivlen = openssl_cipher_iv_length($cipher);
48+
$iv = $key;
49+
$options = 0;
50+
return openssl_decrypt($data, $cipher, $key, $options, $iv);
51+
}
52+
}

src/RSA.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* Provides an easy and secure way to encrypt/decrypt data with PHP
4+
* using the Symmetric Cryptography method.(DES,3DES,OPENSSL)
5+
* bootstrap
6+
*
7+
* @category PHP_Crypto
8+
* @package Qinqw\Crypto
9+
* @author Kevin <qinqiwei@hotmail.com>
10+
* @license Apache License V2
11+
* @link https://github.com/qinqw/php-crypto
12+
*/
13+
namespace Qinqw\Crypto;
14+
15+
class RSA {
16+
17+
private $privateKey = null;
18+
private $publicKey = null;
19+
20+
function __construct($publicKey, $privateKey) {
21+
$this->publicKey = $publicKey;
22+
$this->privateKey = $privateKey;
23+
}
24+
25+
/**
26+
* 私钥加密
27+
* @param string $data 要加密的数据
28+
* @return string 加密后的字符串
29+
*/
30+
public function privateKeyEncode($data) {
31+
$encrypted = '';
32+
$this->_needKey(2);
33+
$private_key = openssl_pkey_get_private($this->privateKey);
34+
$fstr = array();
35+
$array_data = $this->_splitEncode($data);//把要加密的信息 base64 encode后 等长放入数组
36+
foreach ($array_data as $value) {//理论上是可以只加密数组中的第一个元素 其他的不加密 因为只要一个解密不出来 整体也就解密不出来 这里先全部加密
37+
openssl_private_encrypt($value, $encrypted, $private_key); //私钥加密
38+
$fstr[] = $encrypted;//对数组中每个加密
39+
}
40+
return base64_encode(serialize($fstr));//序列化后base64_encode
41+
}
42+
43+
/**
44+
* 公钥加密
45+
* @param string $data 要加密的数据
46+
* @return string 加密后的字符串
47+
*/
48+
public function publicKeyEncode($data) {
49+
$encrypted = '';
50+
$this->_needKey(1);
51+
$public_key = openssl_pkey_get_public($this->publicKey);
52+
$fstr = array();
53+
$array_data = $this->_splitEncode($data);
54+
foreach ($array_data as $value) {
55+
openssl_public_encrypt($value, $encrypted, $public_key); //私钥加密
56+
$fstr[] = $encrypted;
57+
}
58+
return base64_encode(serialize($fstr));
59+
}
60+
61+
/**
62+
* 用公钥解密私钥加密内容
63+
* @param string $data 要解密的数据
64+
* @return string 解密后的字符串
65+
*/
66+
public function decodePrivateEncode($data) {
67+
$decrypted = '';
68+
$this->_needKey(1);
69+
$public_key = openssl_pkey_get_public($this->publicKey);
70+
$array_data = $this->_toArray($data);//数据base64_decode 后 反序列化成数组
71+
$str = '';
72+
foreach ($array_data as $value){
73+
openssl_public_decrypt($value, $decrypted, $public_key); //私钥加密的内容通过公钥可用解密出来
74+
$str .= $decrypted;//对数组中的每个元素解密 并拼接
75+
}
76+
return base64_decode($str);//把拼接的数据base64_decode 解密还原
77+
}
78+
79+
/**
80+
* 用私钥解密公钥加密内容
81+
* @param string $data 要解密的数据
82+
* @return string 解密后的字符串
83+
*/
84+
public function decodePublicEncode($data) {
85+
$decrypted = '';
86+
$this->_needKey(2);
87+
$private_key = openssl_pkey_get_private($this->privateKey);
88+
$array_data = $this->_toArray($data);
89+
$str = '';
90+
foreach ($array_data as $value){
91+
openssl_private_decrypt($value, $decrypted, $private_key); //私钥解密
92+
$str .= $decrypted;
93+
}
94+
return base64_decode($str);
95+
}
96+
97+
/**
98+
* 检查是否 含有所需配置文件
99+
* @param int 1 公钥 2 私钥
100+
* @return int 1
101+
* @throws Exception
102+
*/
103+
private function _needKey($type) {
104+
switch ($type) {
105+
case 1:
106+
if (empty($this->publicKey)) {
107+
throw new Exception('请配置公钥');
108+
}
109+
break;
110+
case 2:
111+
if (empty($this->privateKey)) {
112+
throw new Exception('请配置私钥');
113+
}
114+
break;
115+
}
116+
return 1;
117+
}
118+
119+
/**
120+
*
121+
* @param type $data
122+
* @return type
123+
*/
124+
private function _splitEncode($data) {
125+
$data = base64_encode($data); //加上base_64 encode 便于用于 分组
126+
$total_lenth = strlen($data);
127+
$per = 96;// 能整除2 和 3 RSA每次加密不能超过100个
128+
$dy = $total_lenth % $per;
129+
$total_block = $dy ? ($total_lenth / $per) : ($total_lenth / $per - 1);
130+
for ($i = 0; $i < $total_block; $i++) {
131+
$return[] = substr($data, $i * $per, $per);//把要加密的信息base64 后 按64长分组
132+
}
133+
return $return;
134+
}
135+
136+
/**
137+
*公钥加密并用 base64 serialize 过的 data
138+
* @param type $data base64 serialize 过的 data
139+
*/
140+
private function _toArray($data){
141+
$data = base64_decode($data);
142+
$array_data = unserialize($data);
143+
if(!is_array($array_data)){
144+
throw new Exception('数据加密不符');
145+
}
146+
return $array_data;
147+
}
148+
149+
}

0 commit comments

Comments
 (0)