Skip to content

Commit

Permalink
add Exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanshenjian committed Oct 13, 2015
1 parent 3f9296e commit c431641
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/yood/rsa/util/RSAUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.yood.rsa.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.math.BigInteger;
import java.security.*;
Expand Down Expand Up @@ -85,6 +86,8 @@ public static String getBase64PrivateKey(PrivateKey privateKey) {
}

public static byte[] encryptAsByteArray(String data, PublicKey publicKey) {
throwNullPointException(data);
throwNullPointException(publicKey);
try {
Cipher cipher = Cipher.getInstance(RSA_ECB_PKCS1_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
Expand All @@ -107,6 +110,8 @@ public static String encryptAsString(String data, String base64PublicKey) {
}

public static String decrypt(byte[] data, PrivateKey privateKey) {
throwNullPointException(data);
throwNullPointException(privateKey);
try {
Cipher cipher = Cipher.getInstance(RSA_ECB_PKCS1_PADDING);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
Expand All @@ -127,5 +132,11 @@ public static String decrypt(String data, PrivateKey privateKey) {
public static String decrypt(String data, String base64PrivateKey) {
return decrypt(Base64.decodeBase64(data), getPrivateKey(base64PrivateKey));
}

private static void throwNullPointException(Object obj) {
if (null == obj) {
throw new NullPointerException();
}
}
}

0 comments on commit c431641

Please sign in to comment.