|
@@ -37,16 +37,16 @@ public class RSAUtils {
|
|
|
private final static String publicKeyPath = "static/publicKey.txt";
|
|
|
private final static String privateKeyPath = "static/cxs-jwt.jks";
|
|
|
|
|
|
-
|
|
|
- private static String getKey(String filename) throws IOException {
|
|
|
- ClassPathResource resourcePublicKey = new ClassPathResource(filename);
|
|
|
- try {
|
|
|
- return FileUtil.readString(resourcePublicKey.getFile(), Charset.defaultCharset());
|
|
|
- } catch (IOException e) {
|
|
|
- log.warn("读取文件失败",e);
|
|
|
- throw e;
|
|
|
- }
|
|
|
- }
|
|
|
+ //
|
|
|
+// private static String getKey(String filename) throws IOException {
|
|
|
+// ClassPathResource resourcePublicKey = new ClassPathResource(filename);
|
|
|
+// try {
|
|
|
+// return FileUtil.readString(resourcePublicKey.getFile(), Charset.defaultCharset());
|
|
|
+// } catch (IOException e) {
|
|
|
+// log.warn("读取文件失败",e);
|
|
|
+// throw e;
|
|
|
+// }
|
|
|
+// }
|
|
|
public static Map<String, RSAKey> getKey() throws IOException, GeneralSecurityException {
|
|
|
Map<String,RSAKey> map = Maps.newHashMap();
|
|
|
ClassPathResource resource = new ClassPathResource(privateKeyPath);
|
|
@@ -58,20 +58,20 @@ public class RSAUtils {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
- public static RSAPrivateKey getPrivateKeyFromString(String key) throws IOException, GeneralSecurityException {
|
|
|
- String privateKeyPEM = key;
|
|
|
- privateKeyPEM = privateKeyPEM.replace("-----BEGIN PRIVATE KEY-----\n", "");
|
|
|
- privateKeyPEM = privateKeyPEM.replace("-----END PRIVATE KEY-----", "");
|
|
|
- byte[] encoded = Base64.decodeBase64(privateKeyPEM);
|
|
|
- KeyFactory kf = KeyFactory.getInstance("RSA");
|
|
|
- PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
|
|
|
- return (RSAPrivateKey) kf.generatePrivate(keySpec);
|
|
|
- }
|
|
|
+// public static RSAPrivateKey getPrivateKeyFromString(String key) throws IOException, GeneralSecurityException {
|
|
|
+// String privateKeyPEM = key;
|
|
|
+// privateKeyPEM = privateKeyPEM.replace("-----BEGIN PRIVATE KEY-----\n", "");
|
|
|
+// privateKeyPEM = privateKeyPEM.replace("-----END PRIVATE KEY-----", "");
|
|
|
+// byte[] encoded = Base64.decodeBase64(privateKeyPEM);
|
|
|
+// KeyFactory kf = KeyFactory.getInstance("RSA");
|
|
|
+// PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
|
|
|
+// return (RSAPrivateKey) kf.generatePrivate(keySpec);
|
|
|
+// }
|
|
|
|
|
|
|
|
|
- public static RSAPublicKey getPublicKey() throws IOException, GeneralSecurityException {
|
|
|
- return getPublicKeyFromString(getKey(publicKeyPath));
|
|
|
- }
|
|
|
+// public static RSAPublicKey getPublicKey() throws IOException, GeneralSecurityException {
|
|
|
+// return getPublicKeyFromString(getKey(publicKeyPath));
|
|
|
+// }
|
|
|
|
|
|
public static RSAPublicKey getPublicKeyFromString(String key) throws IOException, GeneralSecurityException {
|
|
|
String publicKeyPEM = key;
|
|
@@ -84,35 +84,35 @@ public class RSAUtils {
|
|
|
|
|
|
|
|
|
|
|
|
- public static String sign(PrivateKey privateKey, String message) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException {
|
|
|
- Signature sign = Signature.getInstance("SHA1withRSA");
|
|
|
- sign.initSign(privateKey);
|
|
|
- sign.update(message.getBytes("UTF-8"));
|
|
|
- return new String(Base64.encodeBase64(sign.sign()), "UTF-8");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static boolean verify(PublicKey publicKey, String message, String signature) throws SignatureException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
|
|
|
- Signature sign = Signature.getInstance("SHA1withRSA");
|
|
|
- sign.initVerify(publicKey);
|
|
|
- try {
|
|
|
- sign.update(message.getBytes("UTF-8"));
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return sign.verify(Base64.decodeBase64(signature.getBytes("UTF-8")));
|
|
|
- }
|
|
|
-
|
|
|
- public static String encrypt(String rawText, PublicKey publicKey) throws IOException, GeneralSecurityException {
|
|
|
- Cipher cipher = Cipher.getInstance("RSA");
|
|
|
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
|
|
- return Base64.encodeBase64String(cipher.doFinal(rawText.getBytes("UTF-8")));
|
|
|
- }
|
|
|
-
|
|
|
- public static String decrypt(String cipherText, PrivateKey privateKey) throws IOException, GeneralSecurityException {
|
|
|
- Cipher cipher = Cipher.getInstance("RSA");
|
|
|
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
|
|
- return new String(cipher.doFinal(Base64.decodeBase64(cipherText)), "UTF-8");
|
|
|
- }
|
|
|
+// public static String sign(PrivateKey privateKey, String message) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException {
|
|
|
+// Signature sign = Signature.getInstance("SHA1withRSA");
|
|
|
+// sign.initSign(privateKey);
|
|
|
+// sign.update(message.getBytes("UTF-8"));
|
|
|
+// return new String(Base64.encodeBase64(sign.sign()), "UTF-8");
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// public static boolean verify(PublicKey publicKey, String message, String signature) throws SignatureException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
|
|
|
+// Signature sign = Signature.getInstance("SHA1withRSA");
|
|
|
+// sign.initVerify(publicKey);
|
|
|
+// try {
|
|
|
+// sign.update(message.getBytes("UTF-8"));
|
|
|
+// } catch (UnsupportedEncodingException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// return sign.verify(Base64.decodeBase64(signature.getBytes("UTF-8")));
|
|
|
+// }
|
|
|
+
|
|
|
+// public static String encrypt(String rawText, PublicKey publicKey) throws IOException, GeneralSecurityException {
|
|
|
+// Cipher cipher = Cipher.getInstance("RSA");
|
|
|
+// cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
|
|
+// return Base64.encodeBase64String(cipher.doFinal(rawText.getBytes("UTF-8")));
|
|
|
+// }
|
|
|
+
|
|
|
+// public static String decrypt(String cipherText, PrivateKey privateKey) throws IOException, GeneralSecurityException {
|
|
|
+// Cipher cipher = Cipher.getInstance("RSA");
|
|
|
+// cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
|
|
+// return new String(cipher.doFinal(Base64.decodeBase64(cipherText)), "UTF-8");
|
|
|
+// }
|
|
|
|
|
|
}
|