org.treeleafj.xmax.safe.Rsa Maven / Gradle / Ivy
The newest version!
package org.treeleafj.xmax.safe;
import javax.crypto.Cipher;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* RSA 加解密工具
*
* @Author leaf
* 2015/9/12 0012 1:18.
*/
public class Rsa {
public static final String RSA = "RSA";
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
public static final String KEY_ALGORITHM_DETAIL = "RSA/ECB/PKCS1Padding";
public static byte[] sign(byte[] data, String privateKey) throws Exception {
return sign(data, Base64.decode(privateKey));
}
public static byte[] sign(byte[] data, byte[] privateKey) throws Exception {
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(priKey);
signature.update(data);
return signature.sign();
}
public static boolean verify(byte[] data, String publicKey, String signByte) throws Exception {
return verify(data, Base64.decode(publicKey), Base64.decode(signByte));
}
public static boolean verify(byte[] data, byte[] publicKey, byte[] signByte) throws Exception {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
java.security.PublicKey pubKey = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(pubKey);
signature.update(data);
return signature.verify(signByte);
}
public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws Exception {
return decryptByPrivateKey(data, Base64.decode(privateKey));
}
public static byte[] decryptByPrivateKey(byte[] data, byte[] privateKey) throws Exception {
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
Key _privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_DETAIL);
cipher.init(2, _privateKey);
return cipher.doFinal(data);
}
public static byte[] decryptByPublicKey(byte[] data, String publicKey) throws Exception {
return decryptByPublicKey(data, Base64.decode(publicKey));
}
public static byte[] decryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
Key _publicKey = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_DETAIL);
cipher.init(2, _publicKey);
return cipher.doFinal(data);
}
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
return encryptByPublicKey(data, Base64.decode(publicKey));
}
public static byte[] encryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
Key _publicKey = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_DETAIL);
cipher.init(1, _publicKey);
return cipher.doFinal(data);
}
public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
return encryptByPrivateKey(data, Base64.decode(privateKey));
}
public static byte[] encryptByPrivateKey(byte[] data, byte[] privateKey) throws Exception {
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
Key _privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_DETAIL);
cipher.init(1, _privateKey);
return cipher.doFinal(data);
}
}