java js rsa加密算法_RSA AES 前端JS与后台JAVA的加密解密的是实现

这段代码实现了一个RSA加密工具类,包括生成RSA密钥对、保存和加载密钥文件、解码用户名和密码等功能。使用了Apache Commons Codec和BouncyCastle Provider进行编码和解码操作。
摘要由CSDN通过智能技术生成

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 package org.common.kit;

2

3 import java.io.File;

4 import java.io.FileInputStream;

5 import java.io.FileOutputStream;

6 import java.io.ObjectInputStream;

7 import java.io.ObjectOutputStream;

8 import java.math.BigInteger;

9 import java.security.InvalidParameterException;

10 import java.security.KeyFactory;

11 import java.security.KeyPair;

12 import java.security.KeyPairGenerator;

13 import java.security.NoSuchAlgorithmException;

14 import java.security.PrivateKey;

15 import java.security.Provider;

16 import java.security.PublicKey;

17 import java.security.SecureRandom;

18 import java.security.interfaces.RSAPrivateKey;

19 import java.security.interfaces.RSAPublicKey;

20 import java.security.spec.InvalidKeySpecException;

21 import java.security.spec.RSAPrivateKeySpec;

22 import java.security.spec.RSAPublicKeySpec;

23 import java.util.Date;

24

25 import javax.crypto.Cipher;

26

27 import org.apache.commons.codec.DecoderException;

28 import org.apache.commons.codec.binary.Hex;

29 import org.apache.commons.io.FileUtils;

30 import org.apache.commons.io.IOUtils;

31 import org.apache.commons.lang3.StringUtils;

32 import org.apache.commons.lang3.time.DateFormatUtils;

33 import org.apache.log4j.Logger;

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

35

36 public abstract class RSA

37 {

38

39 private static final Logger LOGGER = Logger.getLogger(RSA.class);

40

41 private static final String ALGORITHOM = "RSA";

42 private static final String RSA_PAIR_FILENAME = "/__RSA_PAIR.txt";

43 private static final int KEY_SIZE = 1024;

44 private static final Provider DEFAULT_PROVIDER = new BouncyCastleProvider();

45

46 private static KeyPairGenerator keyPairGen = null;

47 private static KeyFactory keyFactory = null;

48

49 private static KeyPair oneKeyPair = null;

50

51 private static File rsaPairFile = null;

52

53 static

54 {

55 try

56 {

57 keyPairGen = KeyPairGenerator.getInstance("RSA", DEFAULT_PROVIDER);

58 keyFactory = KeyFactory.getInstance("RSA", DEFAULT_PROVIDER);

59 } catch (NoSuchAlgorithmException ex)

60 {

61 LOGGER.error(ex.getMessage());

62 }

63 rsaPairFile = new File(getRSAPairFilePath());

64 }

65

66 /***

67 *

68 * 返回 解码后的 username pwd

69 *

70 * add by 12

71 *

72 * @param key

73 * @return

74 */

75 public static String[] decryptUsernameAndPwd(String key)

76 {

77

78 key = RSA.decryptStringByJs(key);

79

80 try

81 {

82 String username = key.substring(key.indexOf("=") + 1, key.indexOf("&"));

83 String pwd = key.substring(key.lastIndexOf("=") + 1, key.length());

84 return new String[] { username, pwd };

85 } catch (Exception e)

86 {

87 return null;

88 }

89

90 }

91

92 private static synchronized KeyPair generateKeyPair()

93 {

94 try

95 {

96 keyPairGen.initialize(1024, new SecureRandom(DateFormatUtils.format(new Date(), "yyyyMMdd").getBytes()));

97 oneKeyPair = keyPairGen.generateKeyPair();

98 saveKeyPair(oneKeyPair);

99 return oneKeyPair;

100 } catch (InvalidParameterException ex)

101 {

102 LOGGER.error("KeyPairGenerator does not support a key length of 1024.", ex);

103 } catch (NullPointerException ex)

104 {

105 LOGGER.error("RSAUtils#KEY_PAIR_GEN is null, can not generate KeyPairGenerator instance.", ex);

106 }

107 return null;

108 }

109

110 private static String getRSAPairFilePath()

111 {

112 String urlPath = RSA.class.getResource("/").getPath();

113 String str = new File(urlPath).getParent() + "/__RSA_PAIR.txt";

114

115 urlPath = null;

116

117 return str;

118 }

119

120 private static boolean isCreateKeyPairFile()

121 {

122 boolean createNewKeyPair = false;

123 if ((!rsaPairFile.exists()) || (rsaPairFile.isDirectory()))

124 {

125 createNewKeyPair = true;

126 }

127 return createNewKeyPair;

128 }

129

130 private static void saveKeyPair(KeyPair keyPair)

131 {

132 FileOutputStream fos = null;

133 ObjectOutputStream oos = null;

134 try

135 {

136 fos = FileUtils.openOutputStream(rsaPairFile);

137 oos = new ObjectOutputStream(fos);

138 oos.writeObject(keyPair);

139 } catch (Exception ex)

140 {

141 ex.printStackTrace();

142 } finally

143 {

144 IOUtils.closeQuietly(oos);

145 IOUtils.closeQuietly(fos);

146 }

147

148 fos = null;

149 oos = null;

150 }

151

152 public static KeyPair getKeyPair()

153 {

154 if (isCreateKeyPairFile()) { return generateKeyPair(); }

155 if (oneKeyPair != null) { return oneKeyPair; }

156 return readKeyPair();

157 }

158

159 private static KeyPair readKeyPair()

160 {

161 FileInputStream fis = null;

162 ObjectInputStream ois = null;

163 try

164 {

165 fis = FileUtils.openInputStream(rsaPairFile);

166 ois = new ObjectInputStream(fis);

167 oneKeyPair = (KeyPair) ois.readObject();

168 return oneKeyPair;

169 } catch (Exception ex)

170 {

171 ex.printStackTrace();

172 } finally

173 {

174 IOUtils.closeQuietly(ois);

175 IOUtils.closeQuietly(fis);

176 }

177

178 fis = null;

179 ois = null;

180 return null;

181 }

182

183 public static RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent)

184 {

185 RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));

186 try

187 {

188 return (RSAPublicKey) keyFactory.generatePublic(publicKeySpec);

189 } catch (InvalidKeySpecException ex)

190 {

191 LOGGER.error("RSAPublicKeySpec is unavailable.", ex);

192 } catch (NullPointerException ex)

193 {

194 LOGGER.error("RSAUtils#KEY_FACTORY is null, can not generate KeyFactory instance.", ex);

195 }

196 publicKeySpec = null;

197 modulus = (byte[]) null;

198 publicExponent = (byte[]) null;

199 return null;

200 }

201

202 public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent)

203 {

204 RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger(privateExponent));

205 try

206 {

207 return (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);

208 } catch (InvalidKeySpecException ex)

209 {

210 LOGGER.error("RSAPrivateKeySpec is unavailable.", ex);

211 } catch (NullPointerException ex)

212 {

213 LOGGER.error("RSAUtils#KEY_FACTORY is null, can not generate KeyFactory instance.", ex);

214 }

215 privateKeySpec = null;

216 modulus = (byte[]) null;

217 privateExponent = (byte[]) null;

218 return null;

219 }

220

221 public static RSAPrivateKey getRSAPrivateKey(String hexModulus, String hexPrivateExponent)

222 {

223 if ((StringUtils.isBlank(hexModulus)) || (StringUtils.isBlank(hexPrivateExponent)))

224 {

225 if (LOGGER.isDebugEnabled())

226 {

227 LOGGER.debug("hexModulus and hexPrivateExponent cannot be empty. RSAPrivateKey value is null to return.");

228 }

229 return null;

230 }

231 byte[] modulus = (byte[]) null;

232 byte[] privateExponent = (byte[]) null;

233 try

234 {

235 modulus = Hex.decodeHex(hexModulus.toCharArray());

236 privateExponent = Hex.decodeHex(hexPrivateExponent.toCharArray());

237 } catch (DecoderException ex)

238 {

239 LOGGER.error("hexModulus or hexPrivateExponent value is invalid. return null(RSAPrivateKey).");

240 }

241 if ((modulus != null) && (privateExponent != null)) { return generateRSAPrivateKey(modulus, privateExponent); }

242 return null;

243 }

244

245 public static RSAPublicKey getRSAPublidKey(String hexModulus, String hexPublicExponent)

246 {

247 if ((StringUtils.isBlank(hexModulus)) || (StringUtils.isBlank(hexPublicExponent)))

248 {

249 if (LOGGER.isDebugEnabled())

250 {

251 LOGGER.debug("hexModulus and hexPublicExponent cannot be empty. return null(RSAPublicKey).");

252 }

253 return null;

254 }

255 byte[] modulus = (byte[]) null;

256 byte[] publicExponent = (byte[]) null;

257 try

258 {

259 modulus = Hex.decodeHex(hexModulus.toCharArray());

260 publicExponent = Hex.decodeHex(hexPublicExponent.toCharArray());

261 } catch (DecoderException ex)

262 {

263 LOGGER.error("hexModulus or hexPublicExponent value is invalid. return null(RSAPublicKey).");

264 }

265 if ((modulus != null) && (publicExponent != null)) { return generateRSAPublicKey(modulus, publicExponent); }

266 return null;

267 }

268

269 public static byte[] encrypt(PublicKey publicKey, byte[] data) throws Exception

270 {

271 Cipher ci = Cipher.getInstance("RSA", DEFAULT_PROVIDER);

272 ci.init(1, publicKey);

273 return ci.doFinal(data);

274 }

275

276 public static byte[] decrypt(PrivateKey privateKey, byte[] data) throws Exception

277 {

278 Cipher ci = Cipher.getInstance("RSA", DEFAULT_PROVIDER);

279 ci.init(2, privateKey);

280 return ci.doFinal(data);

281 }

282

283 public static String encryptString(PublicKey publicKey, String plaintext)

284 {

285 if ((publicKey == null) || (plaintext == null)) { return null; }

286 byte[] data = plaintext.getBytes();

287 try

288 {

289 byte[] en_data = encrypt(publicKey, data);

290 return new String(Hex.encodeHex(en_data));

291 } catch (Exception ex)

292 {

293 LOGGER.error(ex.getCause().getMessage());

294 }

295 return null;

296 }

297

298 public static String encryptString(String plaintext)

299 {

300 if (plaintext == null) { return null; }

301 byte[] data = plaintext.getBytes();

302 KeyPair keyPair = getKeyPair();

303 try

304 {

305 byte[] en_data = encrypt((RSAPublicKey) keyPair.getPublic(), data);

306 return new String(Hex.encodeHex(en_data));

307 } catch (NullPointerException ex)

308 {

309 LOGGER.error("keyPair cannot be null.");

310 } catch (Exception ex)

311 {

312 LOGGER.error(ex.getCause().getMessage());

313 }

314 return null;

315 }

316

317 public static String decryptString(PrivateKey privateKey, String encrypttext)

318 {

319 if ((privateKey == null) || (StringUtils.isBlank(encrypttext))) return null;

320 try

321 {

322 byte[] en_data = Hex.decodeHex(encrypttext.toCharArray());

323 byte[] data = decrypt(privateKey, en_data);

324 return new String(data);

325 } catch (Exception ex)

326 {

327 LOGGER.error(String.format("\"%s\" Decryption failed. Cause: %s", new Object[] { encrypttext, ex.getCause().getMessage() }));

328 }

329 return null;

330 }

331

332 public static String decryptString(String encrypttext)

333 {

334 if (StringUtils.isBlank(encrypttext)) { return null; }

335 KeyPair keyPair = getKeyPair();

336 try

337 {

338 byte[] en_data = Hex.decodeHex(encrypttext.toCharArray());

339 byte[] data = decrypt((RSAPrivateKey) keyPair.getPrivate(), en_data);

340 return new String(data);

341 } catch (NullPointerException ex)

342 {

343 LOGGER.error("keyPair cannot be null.");

344 } catch (Exception ex)

345 {

346 LOGGER.error(String.format("\"%s\" Decryption failed. Cause: %s", new Object[] { encrypttext, ex.getMessage() }));

347 }

348 return null;

349 }

350

351 public static String decryptStringByJs(String encrypttext)

352 {

353 String text = decryptString(encrypttext);

354 if (text == null) { return null; }

355 return StringUtils.reverse(text);

356 }

357

358 public static RSAPublicKey getDefaultPublicKey()

359 {

360 KeyPair keyPair = getKeyPair();

361 if (keyPair != null) { return (RSAPublicKey) keyPair.getPublic(); }

362 return null;

363 }

364

365 public static RSAPrivateKey getDefaultPrivateKey()

366 {

367 KeyPair keyPair = getKeyPair();

368 if (keyPair != null) { return (RSAPrivateKey) keyPair.getPrivate(); }

369 return null;

370 }

371 }

48304ba5e6f9fe08f3fa1abda7d326ab.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值