Home > Backend Development > PHP Tutorial > PHP implements RSA encryption class example_PHP tutorial

PHP implements RSA encryption class example_PHP tutorial

WBOY
Release: 2016-07-13 10:00:19
Original
953 people have browsed it

Example of RSA encryption class implemented in php

This article mainly introduces the implementation of RSA encryption class in php. The example analyzes the techniques of php custom RSA class to implement encryption and decryption, which is very useful. Practical value, friends in need can refer to it

The example in this article describes the implementation of RSA encryption class in PHP. Share it with everyone for your reference. The specific analysis is as follows:

Signature, signature verification, and asymmetric encryption and decryption implemented through openssl need to be used with x.509 certificate (such as crt and pem) files.
Due to various reasons, this class is not very complete, and various tests are welcome!

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

/**

* RSA algorithm class

* Signature and ciphertext encoding: base64 string/hex string/binary string stream

* Padding method: PKCS1Padding (encryption and decryption)/NOPadding (decryption)

*

* Notice:Only accepts a single block. Block size is equal to the RSA key size!

* If the key length is 1024 bit, the data during encryption must be less than 128 bytes, plus the 11 bytes of PKCS1Padding itself, so the plaintext must be less than 117 bytes

*

* @author: linvo

* @version: 1.0.0

* @date: 2013/1/23

*/

class RSA{

private $pubKey = null;

private $priKey = null;

/**

* Custom error handling

*/

private function _error($msg){

die('RSA Error:' . $msg); //TODO

}

/**

* Constructor

*

* @param string public key file (passed in during signature verification and encryption)

* @param string private key file (passed in when signing and decrypting)

*/

public function __construct($public_key_file = '', $private_key_file = ''){

if ($public_key_file){

$this->_getPublicKey($public_key_file);

}

if ($private_key_file){

$this->_getPrivateKey($private_key_file);

}

}

/**

* Generate signature

*

* @param string signature material

* @param string signature encoding (base64/hex/bin)

* @return signature value

*/

public function sign($data, $code = 'base64'){

$ret = false;

if (openssl_sign($data, $ret, $this->priKey)){

$ret = $this->_encode($ret, $code);

}

return $ret;

}

/**

* Verify signature

*

* @param string signature material

* @param string signature value

* @param string signature encoding (base64/hex/bin)

* @return bool

*/

public function verify($data, $sign, $code = 'base64'){

$ret = false;

$sign = $this->_decode($sign, $code);

if ($sign !== false) {

switch (openssl_verify($data, $sign, $this->pubKey)){

case 1: $ret = true; break;

case 0:

case -1:

default: $ret = false;

}

}

return $ret;

}

/**

* Encryption

*

* @param string plain text

* @param string ciphertext encoding (base64/hex/bin)

* @param int filling method (it seems that PHP has a bug, so it currently only supports OPENSSL_PKCS1_PADDING)

* @return string ciphertext

*/

public function encrypt($data, $code = 'base64', $padding = OPENSSL_PKCS1_PADDING){

$ret = false;

if (!$this->_checkPadding($padding, 'en')) $this->_error('padding error');

if (openssl_public_encrypt($data, $result, $this->pubKey, $padding)){

$ret = $this->_encode($result, $code);

}

return $ret;

}

/**

* Decryption

*

* @param string ciphertext

* @param string ciphertext encoding (base64/hex/bin)

* @param int padding method (OPENSSL_PKCS1_PADDING / OPENSSL_NO_PADDING)

* @param bool Whether to flip the plaintext (When passing Microsoft CryptoAPI-generated RSA cyphertext, revert the bytes in the block)

* @return string plain text

*/

public function decrypt($data, $code = 'base64', $padding = OPENSSL_PKCS1_PADDING, $rev = false){

$ret = false;

$data = $this->_decode($data, $code);

if (!$this->_checkPadding($padding, 'de')) $this->_error('padding error');

if ($data !== false){

if (openssl_private_decrypt($data, $result, $this->priKey, $padding)){

$ret = $rev ? rtrim(strrev($result), "

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template