Commit 03bc58c5 authored by yanzg's avatar yanzg

修改实体位置

parent 26394ccf
...@@ -37,7 +37,8 @@ public final class RsaHelper { ...@@ -37,7 +37,8 @@ public final class RsaHelper {
* *
* @throws Exception * @throws Exception
*/ */
public static void generatorKeyPair() throws Exception { public static void generatorKeyPair() {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA); KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA);
keyPairGen.initialize(KEYPAIR_LEN); keyPairGen.initialize(KEYPAIR_LEN);
KeyPair keyPair = keyPairGen.generateKeyPair(); KeyPair keyPair = keyPairGen.generateKeyPair();
...@@ -49,6 +50,9 @@ public final class RsaHelper { ...@@ -49,6 +50,9 @@ public final class RsaHelper {
keyBs = rsaPrivateKey.getEncoded(); keyBs = rsaPrivateKey.getEncoded();
String privateKey = encodeBase64(keyBs); String privateKey = encodeBase64(keyBs);
logger.info("生成的私钥:\t{}", privateKey); logger.info("生成的私钥:\t{}", privateKey);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
/** /**
...@@ -58,9 +62,13 @@ public final class RsaHelper { ...@@ -58,9 +62,13 @@ public final class RsaHelper {
* @throws Exception * @throws Exception
*/ */
private static PublicKey getPublicKey(String publicKey) throws Exception { private static PublicKey getPublicKey(String publicKey) throws Exception {
try {
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBase64(publicKey)); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBase64(publicKey));
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
return keyFactory.generatePublic(publicKeySpec); return keyFactory.generatePublic(publicKeySpec);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
/** /**
...@@ -69,10 +77,14 @@ public final class RsaHelper { ...@@ -69,10 +77,14 @@ public final class RsaHelper {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static PrivateKey getPrivateKey(String privateKey) throws Exception { private static PrivateKey getPrivateKey(String privateKey) {
try {
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodeBase64(privateKey)); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodeBase64(privateKey));
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
return keyFactory.generatePrivate(privateKeySpec); return keyFactory.generatePrivate(privateKeySpec);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
/** /**
...@@ -89,7 +101,8 @@ public final class RsaHelper { ...@@ -89,7 +101,8 @@ public final class RsaHelper {
Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm()); Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey); cipher.init(Cipher.ENCRYPT_MODE, publicKey);
cipher.update(source.getBytes(SystemContants.UTF8)); cipher.update(source.getBytes(SystemContants.UTF8));
String target = encodeBase64(cipher.doFinal()); byte[] bytes = cipher.doFinal();
String target = encodeBase64(bytes);
return target; return target;
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
...@@ -102,12 +115,17 @@ public final class RsaHelper { ...@@ -102,12 +115,17 @@ public final class RsaHelper {
* @param target * @param target
* @throws Exception * @throws Exception
*/ */
public static String decryptionByPublicKey(String target, String publicKeyStr) throws Exception { public static String decryptionByPublicKey(String target, String publicKeyStr) {
try {
byte[] bytes = decodeBase64(target);
PublicKey publicKey = getPublicKey(publicKeyStr); PublicKey publicKey = getPublicKey(publicKeyStr);
Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm()); Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey); cipher.init(Cipher.DECRYPT_MODE, publicKey);
cipher.update(decodeBase64(target)); cipher.update(bytes);
return new String(cipher.doFinal(), SystemContants.UTF8); return new String(cipher.doFinal(), SystemContants.UTF8);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
/** /**
...@@ -116,7 +134,8 @@ public final class RsaHelper { ...@@ -116,7 +134,8 @@ public final class RsaHelper {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static void verifyByPublicKey(String target, String sign, String publicKeyStr) throws Exception { public static void verifyByPublicKey(String target, String sign, String publicKeyStr) {
try {
PublicKey publicKey = getPublicKey(publicKeyStr); PublicKey publicKey = getPublicKey(publicKeyStr);
Signature signature = Signature.getInstance(ALGORITHM_SIGN); Signature signature = Signature.getInstance(ALGORITHM_SIGN);
signature.initVerify(publicKey); signature.initVerify(publicKey);
...@@ -126,6 +145,9 @@ public final class RsaHelper { ...@@ -126,6 +145,9 @@ public final class RsaHelper {
} else { } else {
logger.info("sign false"); logger.info("sign false");
} }
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
/** /**
...@@ -135,13 +157,17 @@ public final class RsaHelper { ...@@ -135,13 +157,17 @@ public final class RsaHelper {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static String encryptionByPrivateKey(String source, String privateKeyStr) throws Exception { public static String encryptionByPrivateKey(String source, String privateKeyStr) {
try {
PrivateKey privateKey = getPrivateKey(privateKeyStr); PrivateKey privateKey = getPrivateKey(privateKeyStr);
Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey); cipher.init(Cipher.ENCRYPT_MODE, privateKey);
cipher.update(source.getBytes(SystemContants.UTF8)); cipher.update(source.getBytes(SystemContants.UTF8));
String target = encodeBase64(cipher.doFinal()); String target = encodeBase64(cipher.doFinal());
return target; return target;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
/** /**
...@@ -169,13 +195,17 @@ public final class RsaHelper { ...@@ -169,13 +195,17 @@ public final class RsaHelper {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static String signByPrivateKey(String target, String privateKeyStr) throws Exception { public static String signByPrivateKey(String target, String privateKeyStr) {
try {
PrivateKey privateKey = getPrivateKey(privateKeyStr); PrivateKey privateKey = getPrivateKey(privateKeyStr);
Signature signature = Signature.getInstance(ALGORITHM_SIGN); Signature signature = Signature.getInstance(ALGORITHM_SIGN);
signature.initSign(privateKey); signature.initSign(privateKey);
signature.update(target.getBytes(SystemContants.UTF8)); signature.update(target.getBytes(SystemContants.UTF8));
String sign = encodeBase64(signature.sign()); String sign = encodeBase64(signature.sign());
return sign; return sign;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
/** /**
...@@ -185,9 +215,13 @@ public final class RsaHelper { ...@@ -185,9 +215,13 @@ public final class RsaHelper {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static String encodeBase64(byte[] source) throws Exception { private static String encodeBase64(byte[] source) {
try {
byte[] to = Base64Utils.encode(source); byte[] to = Base64Utils.encode(source);
return new String(to, SystemContants.UTF8); return new String(to, SystemContants.UTF8);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
/** /**
...@@ -197,8 +231,12 @@ public final class RsaHelper { ...@@ -197,8 +231,12 @@ public final class RsaHelper {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static byte[] decodeBase64(String target) throws Exception { private static byte[] decodeBase64(String target) {
try {
byte[] from = target.getBytes(SystemContants.UTF8); byte[] from = target.getBytes(SystemContants.UTF8);
return Base64Utils.decode(from); return Base64Utils.decode(from);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment