Java??RSA??????????????????
???????????? ???????[ 2012/9/7 11:44:36 ] ????????
????3??B??????????????A???????????????????????ù????N??E???????
??????????????N??E??????PublicKey?????£?
/**
* ??????n??e??????
* @param modulus ???n??
* @param publicExponent ???e??
* @return ??????PublicKey
* @throws Exception
*/
public static PublicKey getPublickKey(String modulus?? String publicExponent)
throws Exception {
KeySpec publicKeySpec = new RSAPublicKeySpec(
new BigInteger(modulus?? 16)?? new BigInteger(publicExponent?? 16));
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey publicKey = factory.generatePublic(publicKeySpec);
return publicKey;
}
??????????PublicKey????????????????????£?
/**
* ?ù???????????
* @param message ??????????
* @param cipherText ???
* @param pubKeyn ???n??
* @param pubKeye ???e??
* @return boolean ???????true??????false
* @throws Exception
*/
public static boolean verify(String message?? String cipherText??String pubKeyn??
String pubKeye) throws Exception {
Cipher c4 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// ???????????Cipher??????г??????DECRYPT_MODE?????????
c4.init(Cipher.DECRYPT_MODE?? getPublickKey(pubKeyn??pubKeye));
// ????
byte[] desDecTextBytes = c4.doFinal(Base64.base64ToByteArray(cipherText));
// ????????????е?MD5
String md5Digest1 = Base64.byteArrayToBase64(desDecTextBytes);
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(message.getBytes("utf-8"));
byte[] digestBytes = md5.digest();
// ?????????????е?MD5
String md5Digest2 = Base64.byteArrayToBase64(digestBytes);
// ??????
if (md5Digest1.equals(md5Digest2)) {
return true;
} else {
return false;
}
}
?????????????????????
????4?????????.cer????????????????
/**
* ??????cer
* @param path .cer?????·?? ?磺c:/abc.cer
* @return base64???????
* @throws IOException
* @throws CertificateException
*/
public static String getPublicKey(String path) throws IOException??
CertificateException{
InputStream inStream = new FileInputStream(path);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int ch;
String res = "";
while ((ch = inStream.read()) != -1) {
out.write(ch);
}
byte[] result = out.toByteArray();
res = Base64.byteArrayToBase64(result);
return res;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11