概述
importjavax.crypto.Cipher;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.security.KeyStore;importjava.security.PrivateKey;importjava.security.PublicKey;importjava.security.Signature;importjava.security.cert.Certificate;importjava.security.cert.CertificateFactory;public classSha1withRSAUtil {private static final String publicKeyFileName = System.getProperty("user.dir") + File.separator + "pubkey.cer";private static final String privateKeyFileName = System.getProperty("user.dir") + File.separator + "private.pfx";private static final String pfxPassword = "123";//私钥文件获取时设置的密钥
private static String aliasName = "003";//alias名称
/*** 签名
*
*@return签名后经过base64处理的字符串
*@throwsException*/
public staticString sign(String str) {
String base64Sign= "";
InputStream fis= null;try{
fis= newFileInputStream(privateKeyFileName);
KeyStore keyStore= KeyStore.getInstance("PKCS12");char[] pscs =pfxPassword.toCharArray();
keyStore.load(fis, pscs);
PrivateKey priKey=(PrivateKey) (keyStore.getKey(aliasName, pscs));//签名
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initSign(priKey);byte[] bysData = str.getBytes("UTF-8");
sign.update(bysData);byte[] signByte =sign.sign();
BASE64Encoder encoder= newBASE64Encoder();
base64Sign=encoder.encode(signByte);
}catch(Exception e) {
e.printStackTrace();
}finally{if (fis != null) {try{
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}returnbase64Sign;
}/*** 数据验证
*
*@paramsignStr 加密后的数据
*@paramverStr 原始字符
*@return
*/
public static booleanverify(String signStr, String verStr)throwsException {boolean verfy = false;
InputStream fis= null;try{
fis= newFileInputStream(publicKeyFileName);
CertificateFactory cf= CertificateFactory.getInstance("x509");
Certificate cerCert=cf.generateCertificate(fis);
PublicKey pubKey=cerCert.getPublicKey();
BASE64Decoder decoder= newBASE64Decoder();byte[] signed =decoder.decodeBuffer(signStr);
Signature sign= Signature.getInstance("SHA1withRSA");
sign.initVerify(pubKey);
sign.update(verStr.getBytes("UTF-8"));
verfy=sign.verify(signed);
}catch(Exception e) {
e.printStackTrace();
}finally{if (fis != null) {try{
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}returnverfy;
}/*** 通过公钥文件进行加密数据
*
*@return加密后经过base64处理的字符串*/
public static String encrypt(String source) throwsException {
InputStream fis= null;try{
fis= newFileInputStream(publicKeyFileName);
CertificateFactory cf= CertificateFactory.getInstance("x509");
Certificate cerCert=cf.generateCertificate(fis);
PublicKey pubKey=cerCert.getPublicKey();
Cipher cipher= Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);byte[] sbt =source.getBytes();byte[] epByte =cipher.doFinal(sbt);
BASE64Encoder encoder= newBASE64Encoder();
String epStr=encoder.encode(epByte);returnepStr;
}finally{if (fis != null) {try{
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 通过私钥文件进行解密数据
*
*@return解密后的明文字符串*/
public static String decode(String source) throwsException {
BASE64Decoder b64d= newBASE64Decoder();byte[] keyByte =b64d.decodeBuffer(source);
InputStream fis= null;try{
fis= newFileInputStream(privateKeyFileName);
KeyStore keyStore= KeyStore.getInstance("PKCS12");char[] pscs =pfxPassword.toCharArray();
keyStore.load(fis, pscs);
PrivateKey priKey=(PrivateKey) (keyStore.getKey(aliasName, pscs));
Cipher cipher= Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);byte[] epByte =cipher.doFinal(keyByte);return new String(epByte, "UTF-8");
}finally{if (fis != null) {try{
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
最后
以上就是活力小松鼠为你收集整理的java rsa 私钥 解密_java RSA实现私钥签名、公钥验签、私钥加密数据、公钥解密数据...的全部内容,希望文章能够帮你解决java rsa 私钥 解密_java RSA实现私钥签名、公钥验签、私钥加密数据、公钥解密数据...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复