Java??RSA??????????????????
???????????? ???????[ 2012/9/7 11:44:36 ] ????????
?????????飺
????RSA??????????????????????CFCA??????????????????RSA????????????????????????????????????????????????????????????????????????????????????????????????????????????RSA??????????????????????????????????????????????е???????????
????????RSA???????????????????????????????????±?
??????????÷????
?????? ????A??B??????????????A?????????
?????? A????????????????????????????????????????????????B??
?????? B?????????????A????????????????????????????????δ???????
?????? B????????????????????????????????????????????A??????????м??????????????????????????????
?????? A???B?????????????????????????????????????ɡ?
????????????????
????1?????????????????????????
/**
* ????? ????PrivateKey
* @param path ???????????·??
* @param password ?????????
* @return ??????PrivateKey
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws IOException
* @throws UnrecoverableKeyException
*/
private static PrivateKey getPrivateKey(String path??String password)
throws KeyStoreException?? NoSuchAlgorithmException?? CertificateException??
IOException?? UnrecoverableKeyException {
KeyStore ks = KeyStore.getInstance("PKCS12");
FileInputStream fis = new FileInputStream(path);
char[] nPassword = null;
if ((password == null) || password.trim().equals("")) {
nPassword = null;
} else {
nPassword = password.toCharArray();
}
ks.load(fis?? nPassword);
fis.close();
Enumeration<String> en = ks.aliases();
String keyAlias = null;
if (en.hasMoreElements()) {
keyAlias = (String) en.nextElement();
}
return (PrivateKey) ks.getKey(keyAlias?? nPassword);
}
????2?????????????????????????????????????????????????′???
/**
* ??????? ??????????£?BASE64(RSA(MD5(src)??privatekey))??????src????????????????
privatekey???????CFCA???????
* @param plainText ??????????
* @param path ?????·??
* @param password ?????????
* @return ???????????????
* @throws Exception
*/
public static String sign(String plainText??String path??String password)
throws Exception {
/*
* MD5????
*/
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(plainText.getBytes("utf-8"));
byte[] digestBytes = md5.digest();
/*
* ??????????? RSA
* Cipher???????????????????????RSA
*/
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//ENCRYPT_MODE??????????
cipher.init(Cipher.ENCRYPT_MODE?? getPrivateKey(path?? password));
//????
byte[] rsaBytes = cipher.doFinal(digestBytes);
//Base64????
return Base64.byteArrayToBase64(rsaBytes);
??????
???·???
??????????????????
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