Commit 03bc58c5 authored by yanzg's avatar yanzg

修改实体位置

parent 26394ccf
...@@ -37,18 +37,22 @@ public final class RsaHelper { ...@@ -37,18 +37,22 @@ public final class RsaHelper {
* *
* @throws Exception * @throws Exception
*/ */
public static void generatorKeyPair() throws Exception { public static void generatorKeyPair() {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA); try {
keyPairGen.initialize(KEYPAIR_LEN); KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA);
KeyPair keyPair = keyPairGen.generateKeyPair(); keyPairGen.initialize(KEYPAIR_LEN);
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); KeyPair keyPair = keyPairGen.generateKeyPair();
byte[] keyBs = rsaPublicKey.getEncoded(); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
String publicKey = encodeBase64(keyBs); byte[] keyBs = rsaPublicKey.getEncoded();
logger.info("生成的公钥:\t{}", publicKey); String publicKey = encodeBase64(keyBs);
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); logger.info("生成的公钥:\t{}", publicKey);
keyBs = rsaPrivateKey.getEncoded(); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
String privateKey = encodeBase64(keyBs); keyBs = rsaPrivateKey.getEncoded();
logger.info("生成的私钥:\t{}", privateKey); String privateKey = encodeBase64(keyBs);
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 {
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBase64(publicKey)); try {
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBase64(publicKey));
return keyFactory.generatePublic(publicKeySpec); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
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) {
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodeBase64(privateKey)); try {
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodeBase64(privateKey));
return keyFactory.generatePrivate(privateKeySpec); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
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) {
PublicKey publicKey = getPublicKey(publicKeyStr); try {
Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm()); byte[] bytes = decodeBase64(target);
cipher.init(Cipher.DECRYPT_MODE, publicKey); PublicKey publicKey = getPublicKey(publicKeyStr);
cipher.update(decodeBase64(target)); Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
return new String(cipher.doFinal(), SystemContants.UTF8); cipher.init(Cipher.DECRYPT_MODE, publicKey);
cipher.update(bytes);
return new String(cipher.doFinal(), SystemContants.UTF8);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} }
/** /**
...@@ -116,15 +134,19 @@ public final class RsaHelper { ...@@ -116,15 +134,19 @@ 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) {
PublicKey publicKey = getPublicKey(publicKeyStr); try {
Signature signature = Signature.getInstance(ALGORITHM_SIGN); PublicKey publicKey = getPublicKey(publicKeyStr);
signature.initVerify(publicKey); Signature signature = Signature.getInstance(ALGORITHM_SIGN);
signature.update(target.getBytes(SystemContants.UTF8)); signature.initVerify(publicKey);
if (signature.verify(decodeBase64(sign))) { signature.update(target.getBytes(SystemContants.UTF8));
logger.info("sign true"); if (signature.verify(decodeBase64(sign))) {
} else { logger.info("sign true");
logger.info("sign false"); } else {
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) {
PrivateKey privateKey = getPrivateKey(privateKeyStr); try {
Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); PrivateKey privateKey = getPrivateKey(privateKeyStr);
cipher.init(Cipher.ENCRYPT_MODE, privateKey); Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
cipher.update(source.getBytes(SystemContants.UTF8)); cipher.init(Cipher.ENCRYPT_MODE, privateKey);
String target = encodeBase64(cipher.doFinal()); cipher.update(source.getBytes(SystemContants.UTF8));
return target; String target = encodeBase64(cipher.doFinal());
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) {
PrivateKey privateKey = getPrivateKey(privateKeyStr); try {
Signature signature = Signature.getInstance(ALGORITHM_SIGN); PrivateKey privateKey = getPrivateKey(privateKeyStr);
signature.initSign(privateKey); Signature signature = Signature.getInstance(ALGORITHM_SIGN);
signature.update(target.getBytes(SystemContants.UTF8)); signature.initSign(privateKey);
String sign = encodeBase64(signature.sign()); signature.update(target.getBytes(SystemContants.UTF8));
return sign; String sign = encodeBase64(signature.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) {
byte[] to = Base64Utils.encode(source); try {
return new String(to, SystemContants.UTF8); byte[] to = Base64Utils.encode(source);
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) {
byte[] from = target.getBytes(SystemContants.UTF8); try {
return Base64Utils.decode(from); byte[] from = target.getBytes(SystemContants.UTF8);
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