|
@@ -21,14 +21,14 @@ public class RSAUtils implements Constants {
|
|
public static RSAPrivateKey buildPrivateKey(String key)
|
|
public static RSAPrivateKey buildPrivateKey(String key)
|
|
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException {
|
|
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException {
|
|
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME);
|
|
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME);
|
|
- PKCS8EncodedKeySpec pkcs8eks = new PKCS8EncodedKeySpec(Base64Utils.decode(key));
|
|
|
|
|
|
+ PKCS8EncodedKeySpec pkcs8eks = new PKCS8EncodedKeySpec(Base64Util.decode(key));
|
|
return (RSAPrivateKey) keyFactory.generatePrivate(pkcs8eks);
|
|
return (RSAPrivateKey) keyFactory.generatePrivate(pkcs8eks);
|
|
}
|
|
}
|
|
|
|
|
|
public static RSAPublicKey buildPublicKey(String key)
|
|
public static RSAPublicKey buildPublicKey(String key)
|
|
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException {
|
|
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException {
|
|
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME);
|
|
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME);
|
|
- X509EncodedKeySpec x509eks = new X509EncodedKeySpec(Base64Utils.decode(key));
|
|
|
|
|
|
+ X509EncodedKeySpec x509eks = new X509EncodedKeySpec(Base64Util.decode(key));
|
|
return (RSAPublicKey) keyFactory.generatePublic(x509eks);
|
|
return (RSAPublicKey) keyFactory.generatePublic(x509eks);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -37,7 +37,7 @@ public class RSAUtils implements Constants {
|
|
IllegalBlockSizeException, BadPaddingException {
|
|
IllegalBlockSizeException, BadPaddingException {
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
|
|
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
|
|
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
|
|
- return Base64Utils.encode(cipher.doFinal(input.getBytes(CHARSET_NAME)));
|
|
|
|
|
|
+ return Base64Util.encode(cipher.doFinal(input.getBytes(CHARSET_NAME)));
|
|
}
|
|
}
|
|
|
|
|
|
public static String encrypt(byte[] input, RSAPrivateKey privateKey)
|
|
public static String encrypt(byte[] input, RSAPrivateKey privateKey)
|
|
@@ -45,7 +45,7 @@ public class RSAUtils implements Constants {
|
|
IllegalBlockSizeException, BadPaddingException {
|
|
IllegalBlockSizeException, BadPaddingException {
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
|
|
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
|
|
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
|
|
- return Base64Utils.encode(cipher.doFinal(input));
|
|
|
|
|
|
+ return Base64Util.encode(cipher.doFinal(input));
|
|
}
|
|
}
|
|
|
|
|
|
public static String decrypt(String input, RSAPublicKey publicKey)
|
|
public static String decrypt(String input, RSAPublicKey publicKey)
|
|
@@ -53,7 +53,7 @@ public class RSAUtils implements Constants {
|
|
UnsupportedEncodingException {
|
|
UnsupportedEncodingException {
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
|
|
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
|
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
|
- return new String(cipher.doFinal(Base64Utils.decode(input)), CHARSET_NAME);
|
|
|
|
|
|
+ return new String(cipher.doFinal(Base64Util.decode(input)), CHARSET_NAME);
|
|
}
|
|
}
|
|
|
|
|
|
public static byte[] decrypt2byte(String input, RSAPublicKey publicKey)
|
|
public static byte[] decrypt2byte(String input, RSAPublicKey publicKey)
|
|
@@ -61,7 +61,7 @@ public class RSAUtils implements Constants {
|
|
UnsupportedEncodingException {
|
|
UnsupportedEncodingException {
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
|
|
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
|
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
|
- return cipher.doFinal(Base64Utils.decode(input));
|
|
|
|
|
|
+ return cipher.doFinal(Base64Util.decode(input));
|
|
}
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
|
|
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
|
|
@@ -71,8 +71,8 @@ public class RSAUtils implements Constants {
|
|
keyPairGenerator.initialize(1024);
|
|
keyPairGenerator.initialize(1024);
|
|
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
|
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
|
// 分别得到公钥和私钥
|
|
// 分别得到公钥和私钥
|
|
- String publicKey = Base64Utils.encode(((RSAPublicKey) keyPair.getPublic()).getEncoded());
|
|
|
|
- String privateKey = Base64Utils.encode(((RSAPrivateKey) keyPair.getPrivate()).getEncoded());
|
|
|
|
|
|
+ String publicKey = Base64Util.encode(((RSAPublicKey) keyPair.getPublic()).getEncoded());
|
|
|
|
+ String privateKey = Base64Util.encode(((RSAPrivateKey) keyPair.getPrivate()).getEncoded());
|
|
System.out.println("publicKey:" + publicKey);
|
|
System.out.println("publicKey:" + publicKey);
|
|
System.out.println("privateKey:" + privateKey);
|
|
System.out.println("privateKey:" + privateKey);
|
|
}
|
|
}
|