Skip to content

Instantly share code, notes, and snippets.

@shau-lok
Created March 24, 2016 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shau-lok/a17b09e8f163bf630fa2 to your computer and use it in GitHub Desktop.
Save shau-lok/a17b09e8f163bf630fa2 to your computer and use it in GitHub Desktop.
RSA加密
public class RSAEncryptionUtil {
public static final String ALGORITHM = "RSA";
public static final int KEY_LENGTH = 2048;
public static KeyPair generateRSAKey() {
KeyPairGenerator kpg;
try {
kpg = KeyPairGenerator.getInstance(ALGORITHM);
kpg.initialize(KEY_LENGTH);
return kpg.genKeyPair();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
/**
* RSA Encrypt
*
* @param plain
* @param publicKey
* @return
*/
public static byte[] encryptRSA(byte[] plain, PublicKey publicKey) {
byte[] enc = null;
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
enc = cipher.doFinal(plain);
} catch (Exception e) {
e.printStackTrace();
}
return enc;
}
/**
* RSA decrypt
*
* @param enc
* @param privateKey
* @return
*/
public static byte[] decryptRSA(byte[] enc, PrivateKey privateKey) {
byte[] plain = null;
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
plain = cipher.doFinal(enc);
} catch (Exception e) {
e.printStackTrace();
}
return plain;
}
/**
* keyBytes公钥还原
*
* @param keyBytes
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public static PublicKey getPublicKey(byte[] keyBytes) throws NoSuchAlgorithmException, InvalidKeySpecException {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* keyBytes私钥还原
*
* @param keyBytes
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public static PrivateKey getPrivateKey(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
public static PublicKey getPublicKey(String modulus, String publicExponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
BigInteger bigIntModulus = new BigInteger(modulus);
BigInteger bigIntPublicExponent = new BigInteger(publicExponent);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPublicExponent);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
public static PrivateKey getPrivateKey(String modulus, String privateExponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
BigInteger bigIntModulus = new BigInteger(modulus);
BigInteger bigIntPrivateExponent = new BigInteger(privateExponent);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
public static PublicKey loadPublicKey(String publicKeyStr) {
try {
byte[] buffer = Base64.decode(publicKeyStr, Base64.DEFAULT);
KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance(ALGORITHM);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return null;
}
public static PrivateKey loadPrivateKey(String privateKeyStr) {
try {
byte[] buffer = Base64.decode(privateKeyStr, Base64.DEFAULT);
KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance(ALGORITHM);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
return keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return null;
}
public static PublicKey loadPublicKey(InputStream inputStream) {
try {
return loadPublicKey(readKey(inputStream));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static PrivateKey loadPrivateKey(InputStream inputStream) {
try {
return loadPrivateKey(readKey(inputStream));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static String readKey(InputStream inputStream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String readLine;
StringBuilder builder = new StringBuilder();
while ((readLine = reader.readLine()) != null) {
if (readLine.charAt(0) != '-') {
builder.append(readLine);
builder.append("\r");
}
}
return builder.toString();
}
public static void printPublicKey(PublicKey publicKey) {
StringBuilder builder = new StringBuilder();
RSAPublicKey key = (RSAPublicKey) publicKey;
builder.append("RSA公钥Key: \n");
builder.append("Modulus.length= ").append(key.getModulus().bitLength()).append("\n");
builder.append("Modulus= ").append(key.getModulus().toString()).append("\n");
builder.append("PublicExponent.length= ").append(key.getPublicExponent().bitLength()).append("\n");
builder.append("PublicExponent= ").append(key.getPublicExponent().toString()).append("\n");
// TODO: 3/21/16 print
}
public static void printPrivateKey(PrivateKey privateKey) {
StringBuilder builder = new StringBuilder();
RSAPrivateKey key = (RSAPrivateKey) privateKey;
builder.append("RSA私钥Key: \n");
builder.append("Modulus.length= ").append(key.getModulus().bitLength()).append("\n");
builder.append("Modulus= ").append(key.getModulus().toString()).append("\n");
builder.append("PrivateExponent.length= ").append(key.getPrivateExponent().bitLength()).append("\n");
builder.append("PrivateExponent= ").append(key.getPrivateExponent().toString()).append("\n");
// TODO: 3/21/16 print
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment