Commit 2736f9ca authored by 技术部-任文超's avatar 技术部-任文超

RSA

parent 1d7c4cb4
...@@ -20,7 +20,7 @@ public class Rsa { ...@@ -20,7 +20,7 @@ public class Rsa {
private static int KEYSIZE = 1024; private static int KEYSIZE = 1024;
public static final String CHAR_ENCODING = "UTF-8"; public static final String CHAR_ENCODING = "UTF-8";
public static final String RSA_ALGORITHM = "Rsa/ECB/PKCS1Padding"; public static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
/** */ /** */
/** /**
...@@ -41,7 +41,7 @@ public class Rsa { ...@@ -41,7 +41,7 @@ public class Rsa {
/** RSA算法要求有一个可信任的随机数源 */ /** RSA算法要求有一个可信任的随机数源 */
SecureRandom sr = new SecureRandom(); SecureRandom sr = new SecureRandom();
/** 为RSA算法创建一个KeyPairGenerator对象 */ /** 为RSA算法创建一个KeyPairGenerator对象 */
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Rsa"); KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
/** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */ /** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */
kpg.initialize(KEYSIZE, sr); kpg.initialize(KEYSIZE, sr);
/** 生成密匙对 */ /** 生成密匙对 */
...@@ -143,7 +143,7 @@ public class Rsa { ...@@ -143,7 +143,7 @@ public class Rsa {
public static PublicKey getPublicKey(String key) throws Exception { public static PublicKey getPublicKey(String key) throws Exception {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec( X509EncodedKeySpec keySpec = new X509EncodedKeySpec(
Base64.decodeBase64(key.getBytes())); Base64.decodeBase64(key.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("Rsa"); KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec); PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey; return publicKey;
} }
...@@ -157,7 +157,7 @@ public class Rsa { ...@@ -157,7 +157,7 @@ public class Rsa {
public static PrivateKey getPrivateKey(String key) throws Exception { public static PrivateKey getPrivateKey(String key) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec( PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
Base64.decodeBase64(key.getBytes())); Base64.decodeBase64(key.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("Rsa"); KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec); PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey; return privateKey;
} }
...@@ -167,7 +167,7 @@ public class Rsa { ...@@ -167,7 +167,7 @@ public class Rsa {
try { try {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
Base64.decodeBase64(privateKey.getBytes())); Base64.decodeBase64(privateKey.getBytes()));
KeyFactory keyf = KeyFactory.getInstance("Rsa"); KeyFactory keyf = KeyFactory.getInstance("RSA");
PrivateKey priKey = keyf.generatePrivate(priPKCS8); PrivateKey priKey = keyf.generatePrivate(priPKCS8);
Signature signature = Signature.getInstance("SHA1WithRSA"); Signature signature = Signature.getInstance("SHA1WithRSA");
signature.initSign(priKey); signature.initSign(priKey);
...@@ -182,7 +182,7 @@ public class Rsa { ...@@ -182,7 +182,7 @@ public class Rsa {
public static boolean checkSign(String content, String sign, String publicKey) { public static boolean checkSign(String content, String sign, String publicKey) {
try { try {
KeyFactory keyFactory = KeyFactory.getInstance("Rsa"); KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = Base64.decode2(publicKey); byte[] encodedKey = Base64.decode2(publicKey);
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
Signature signature = Signature.getInstance("SHA1WithRSA"); Signature signature = Signature.getInstance("SHA1WithRSA");
......
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