public class SignProvider {
private SignProvider() {
}
/**
*
* Description:校验数字签名,此方法不会抛出任务异常,成功返回true,失败返回false,要求全部参数不能为空
*
* @param pubKeyText
* 公钥,base64编码
* @param plainText
* 明文
* @param signTest
* 数字签名的密文,base64编码
* @return 校验成功返回true 失败返回false
* @author 孙钰佳
* @since:2007-12-27 上午09:33:55
*/
public static boolean verify(byte[] pubKeyText, String plainText,
byte[] signText) {
try {
// 取公钥匙对象
java.security.PublicKey pubKey = getPublicKey(Base64
.decode(pubKeyText));
// 解密由base64编码的数字签名
byte[] signed = Base64.decode(signText);
java.security.Signature signatureChecker = java.security.Signature
.getInstance("SHA1WithRSA");
signatureChecker.initVerify(pubKey);
signatureChecker.update(plainText.getBytes());
// 验证签名是否正常
if (signatureChecker.verify(signed))
return true;
else
return false;
} catch (Throwable e) {
System.out.println("校验签名失败");
e.printStackTrace();
return false;
}
}
/**
* 从公钥数据取得公钥
*
* @param bPubKeyInput
* @return
*/
public static PublicKey getPublicKey(byte[] bPubKeyInput) {
PublicKey rsaPubKey = null;
byte[] bX509PubKeyHeader = { 48, -127, -97, 48, 13, 6, 9, 42, -122, 72,
-122, -9, 13, 1, 1, 1, 5, 0, 3, -127, -115, 0 };
try {
byte[] bPubKey = new byte[bPubKeyInput.length
+ bX509PubKeyHeader.length];
System.arraycopy(bX509PubKeyHeader, 0, bPubKey, 0,
bX509PubKeyHeader.length);
System.arraycopy(bPubKeyInput, 0, bPubKey,
bX509PubKeyHeader.length, bPubKeyInput.length);
X509EncodedKeySpec rsaKeySpec = new X509EncodedKeySpec(bPubKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
rsaPubKey = keyFactory.generatePublic(rsaKeySpec);
} catch (Exception e) {
e.printStackTrace();
}
return rsaPubKey;
}
}
private SignProvider() {
}
/**
*
* Description:校验数字签名,此方法不会抛出任务异常,成功返回true,失败返回false,要求全部参数不能为空
*
* @param pubKeyText
* 公钥,base64编码
* @param plainText
* 明文
* @param signTest
* 数字签名的密文,base64编码
* @return 校验成功返回true 失败返回false
* @author 孙钰佳
* @since:2007-12-27 上午09:33:55
*/
public static boolean verify(byte[] pubKeyText, String plainText,
byte[] signText) {
try {
// 取公钥匙对象
java.security.PublicKey pubKey = getPublicKey(Base64
.decode(pubKeyText));
// 解密由base64编码的数字签名
byte[] signed = Base64.decode(signText);
java.security.Signature signatureChecker = java.security.Signature
.getInstance("SHA1WithRSA");
signatureChecker.initVerify(pubKey);
signatureChecker.update(plainText.getBytes());
// 验证签名是否正常
if (signatureChecker.verify(signed))
return true;
else
return false;
} catch (Throwable e) {
System.out.println("校验签名失败");
e.printStackTrace();
return false;
}
}
/**
* 从公钥数据取得公钥
*
* @param bPubKeyInput
* @return
*/
public static PublicKey getPublicKey(byte[] bPubKeyInput) {
PublicKey rsaPubKey = null;
byte[] bX509PubKeyHeader = { 48, -127, -97, 48, 13, 6, 9, 42, -122, 72,
-122, -9, 13, 1, 1, 1, 5, 0, 3, -127, -115, 0 };
try {
byte[] bPubKey = new byte[bPubKeyInput.length
+ bX509PubKeyHeader.length];
System.arraycopy(bX509PubKeyHeader, 0, bPubKey, 0,
bX509PubKeyHeader.length);
System.arraycopy(bPubKeyInput, 0, bPubKey,
bX509PubKeyHeader.length, bPubKeyInput.length);
X509EncodedKeySpec rsaKeySpec = new X509EncodedKeySpec(bPubKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
rsaPubKey = keyFactory.generatePublic(rsaKeySpec);
} catch (Exception e) {
e.printStackTrace();
}
return rsaPubKey;
}
}