php bouncy castle,BouncyCastle RSA加解密类 C#版 兼容JAVA PHP(RSAHelper) | 学步园

1 package com.jjlg.webinf;

2

3 import java.io.File;

4 import java.io.FileReader;

5 import java.security.Key;

6 import java.security.SecureRandom;

7 import java.security.Security;

8

9 import javax.crypto.Cipher;

10

11 import org.bouncycastle.jce.provider.BouncyCastleProvider;

12 import org.bouncycastle.openssl.PEMReader;

13 import org.bouncycastle.openssl.PasswordFinder;

14

15 import com.jjlg.MyFile;

16 import com.jjlg.MyString;

17

18 import sun.misc.BASE64Decoder;

19 import sun.misc.BASE64Encoder;

20

21 /**22 * RSA23 *24 *@authorShaoJiang25 *26 */

27 public class PhpRsa

28 {

29

30     public static void main(String[] args)

31     {

32         try

33         {

34

35             //字符串截取测试36 //String str = "1234567890";37 //System.out.println(str.substring(0, 5));38 //System.out.println(str.substring(5, 10));39 40 //加密解密测试41             String text = "一二三四五六七八九十1234567890一二三四五六七八九十1234567890一二三四五六七八九十1234567890一二三四五六七八九十1234567890一二三四五六七八九十1234567890";

42             String encodeString = PhpRsa.encrypt(text);

43             String decodeString = PhpRsa.decrypt(encodeString);

44

45             System.out.println("text=" + text);

46             System.out.println("encodeString=" + encodeString);

47             System.out.println("decodeString=" + decodeString);

48

49             String p = "gO9NZbjwx7bf+MfWPKYP2WkZI72jUwz/EC031V+WkWLEigo04vbvsRyPxv0wJYYVuJ3xQk7OgonTWYfDa3EGXVN45j64SMhxhOdN5242h+ke3GJpyrBUWi/waVc/conAL46sNe6tCtGW7iU/EOl415XoUTX6ns7LdHDRPsxj3d8=";

50             System.out.println(PhpRsa.decrypt(p));

51

52             //String zi =53 //"山西省太原市小店区abc山西省太原市小23店区平阳路与平阳路西二efa巷交叉口往北米新康隆456商城好的789你好";54 //zi = new String(zi.getBytes(), "utf-8");55 //System.out.println(zi);56 //57 //String t = (new BASE64Encoder()).encode(zi.getBytes("utf-8"));58 //String w = new String((new BASE64Decoder()).decodeBuffer(new59 //String(t.getBytes())), "utf-8");60 //String e = t.replace("\r\n", "");//61 //"5bGx6KW/55yB5aSq5Y6f5biC5bCP5bqX5Yy6YWJj5bGx6KW/55yB5aSq5Y6f5biC5bCPMjPlupfljLrlubPpmLPot6/kuI7lubPpmLPot6/opb/kuoxlZmHlt7fkuqTlj4nlj6PlvoDljJfnsbPmlrDlurfpmoY0NTbllYbln47lpb3nmoQ3ODnkvaDlpb0=";62 //e = new String((new BASE64Decoder()).decodeBuffer(e));63 //t = t.replace("\r\n", "");64 //System.out.println(t);65 //System.out.println(t.length());66 //System.out.println(w);67 //System.out.println(w.length());68 //System.out.println(new String(e.getBytes(), "utf-8"));69 70 //中文编码测试71 //String str = "王某某";72 //System.out.println("1:" + new String(str.getBytes("utf-8"),73 //"gb2312"));74 75 //System.out.println("1:" + new String(str.getBytes("gb2312"),76 //"utf-8"));77 78 //String str2 =79 //"o0glTLeRAaE1LYj6P/jHPQrzUhKozQWSDHSkyv+HbyluAIE7Ao3KPXGWMG2Rg7SAY+G6yCuOQn4DAmwM4QnbQn+I/CUUVCcz8JTco6S6++3I2luZfTMYee6e+KsC+YHXY2VinJ7ubN6hCtKWLckbC68oXBXtKEHfPQ48vZnmcWk=";80 //System.out.println(PhpRsa.decrypt(str2));81

82         }

83         catch (Exception e)

84         {

85             e.printStackTrace();

86         }

87     }

88

89     public static String encrypt(String text)

90     {

91         String value = "";

92

93         //System.out.println("encrypt-start:-----------------------------------------------------");94

95         try

96         {

97             if (text != null && text.length() > 0 && !text.equals(""))

98             {

99                 Security.addProvider(new BouncyCastleProvider());

100

101                 PhpRsa rsa = new PhpRsa();

102                 String path = rsa.getClass().getResource("").getPath();

103                 //System.out.println(path + "PublicKey.pem");104                 Key publicKey = translatePublicKey(new File(path + "PublicKey.pem"));

105

106                 text = (new BASE64Encoder()).encode(text.getBytes("utf-8"));

107                 text = text.replace("\r", "");

108                 text = text.replace("\n", "");

109                 //System.out.println(text);110

111                 int len = 117;

112                 int m = text.length() / len;

113                 if (m * len != text.length())

114                 {

115                     m = m + 1;

116                 }

117

118                 for (int i = 0; i 

119                 {

120                     String temp = "";

121

122                     if (i 

123                     {

124                         temp = text.substring(i * len, (i + 1) * len);

125                     }

126                     else

127                     {

128                         temp = text.substring(i * len);

129                     }

130

131                     //System.out.println(temp);132 //System.out.println(temp.length());133

134                     temp = (new BASE64Encoder()).encode(PhpRsa.encrypt(temp.getBytes(), publicKey));

135                     temp = temp.replace("\r", "");

136                     temp = temp.replace("\n", "");

137

138                     value += temp;

139                 }

140

141                 //System.out.println("encrypt-text:" + text);142 //System.out.println("encrypt-value:" + value);143             }

144

145         }

146         catch (Exception e)

147         {

148             e.printStackTrace();

149         }

150

151         //System.out.println("encrypt-end:-----------------------------------------------------");152

153         return value;

154     }

155

156     public static String decrypt(String text)

157     {

158         String value = "";

159

160         //System.out.println("decrypt-start:-----------------------------------------------------");161

162         try

163         {

164             if (text != null && text.length() > 0 && !text.equals(""))

165             {

166                 Security.addProvider(new BouncyCastleProvider());

167

168                 PhpRsa rsa = new PhpRsa();

169                 String path = rsa.getClass().getResource("").getPath();

170                 String password = MyFile.readTxt(path + "Password.pem", "utf-8").replace("\n", "");

171

172                 Key privateKey = translatePrivateKey(new File(path + "PrivateKey.pem"), password);

173

174                 //分段解密175                 int len = 172;

176                 int m = text.length() / len;

177                 if (m * len != text.length())

178                 {

179                     m = m + 1;

180                 }

181

182                 for (int i = 0; i 

183                 {

184                     String temp = "";

185

186                     if (i 

187                     {

188                         temp = text.substring(i * len, (i + 1) * len);

189                     }

190                     else

191                     {

192                         temp = text.substring(i * len);

193                     }

194

195                     //System.out.println(temp);196 //System.out.println(temp.length());197                     temp = PhpRsa.decode64(temp);

198

199                     //System.out.println(temp);200 //System.out.println(temp.length());201

202                     temp = new String(PhpRsa.decrypt((new BASE64Decoder()).decodeBuffer(temp), privateKey), "utf-8");

203

204                     //System.out.println(temp);205

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值