md5WithRSA
来源:互联网 发布:性价比高的电钢琴知乎 编辑:程序博客网 时间:2024/05/21 00:54
/**
* 方法说明:<br>
* 获取指定公钥和私钥
*
* @return String[]
* @throws Exception
*/
public static String[] getKeyByJava() throws Exception {
String[] retKey = new String[2];
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
// 得到指定的公钥和私钥
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
generator.initialize(1024, new SecureRandom());
KeyPair pair = generator.generateKeyPair();
PublicKey pubKey = pair.getPublic();
PrivateKey privKey = pair.getPrivate();
byte[] pk = pubKey.getEncoded();
byte[] privk = privKey.getEncoded();
String strpk = new String(Base64Utils.base64Encode(pk)/*Base64.encodeBase64(pk)*/);
String strprivk = new String(Base64Utils.base64Encode(privk)/*Base64.encodeBase64(privk)*/);
System.out.println("公钥:" + Arrays.toString(pk));
System.out.println("私钥:" + Arrays.toString(privk));
System.out.println("公钥Base64编码:" + strpk);
System.out.println("私钥Base64编码:" + strprivk);
// 反向生成指定的公钥和私钥
X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(Base64Utils.base64Decode(strpk)/*Base64.decodeBase64(strpk.getBytes())*/);
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64Utils.base64Decode(strprivk)/*Base64.decodeBase64(strprivk.getBytes())*/);
KeyFactory keyf = KeyFactory.getInstance("RSA", "BC");
PublicKey pubkey2 = keyf.generatePublic(pubX509);
PrivateKey privkey2 = keyf.generatePrivate(priPKCS8);
System.out.println(pubKey.equals(pubkey2));
System.out.println(privKey.equals(privkey2));
byte[] signStr = SignUtils.doSign(privkey2, reqXml.getBytes("UTF-8"), "MD5withRSA");
boolean signRet = SignUtils.doVerify(pubkey2, reqXml.getBytes("UTF-8"), signStr, "MD5withRSA");
retKey[0] = strpk;
retKey[1] = strprivk;
return retKey;
}
public final class SignUtils {
/**
* 签名算法SHA1withRSA
*/
public final static String SIGN_ALGORITHM_SHA1WITHRSA = "SHA1withRSA";
/**
* 签名算法MD5withRSA
*/
public final static String SIGN_ALGORITHM_MD5WITHRSA = "MD5withRSA";
/**
* 方法说明:<br>
*
* @param keyPath 私钥路径
* @param keyPwd 私钥密码
* @param signAlg 签名算法
* @param textByte 原始报文字节
* @return byte[]
* @throws Exception
*/
public static byte[] sign(String keyPath, String keyPwd, String signAlg, byte[] textByte) throws Exception {
PrivateKey priKey = CertUtils.getPrivateKey(keyPath, keyPwd);
return doSign(priKey, textByte, signAlg);
}
/**
* 方法说明:<br>
*
* @param cerPath 公钥路径
* @param signAlg 签名算法
* @param textByte 原始报文字节
* @param signByte 签名字节
* @return boolean 校验结果
* @throws Exception
*/
public static boolean verify(String cerPath, String signAlg, byte[] textByte, byte[] signByte) throws Exception {
PublicKey pubKey = CertUtils.getPublicKey(cerPath);
return doVerify(pubKey, textByte, signByte, signAlg);
}
/**
* 方法说明:<br>
*
* @param priKey 私钥路径
* @param textByte 原始报文字节
* @param algorithm 签名算法
* @return byte[]
* @throws Exception
*/
public static byte[] doSign(PrivateKey priKey, byte[] textByte, String algorithm) throws Exception {
Signature sig = Signature.getInstance(algorithm);
sig.initSign(priKey);
sig.update(textByte);
return sig.sign();
}
/**
* 方法说明:<br>
*
* @param pubKey 公钥路径
* @param textByte 原始串字节
* @param signaByte 签名字节串
* @param algorithm 签名算法
* @return boolean 是否签名校验正确
* @throws Exception
*/
public static boolean doVerify(PublicKey pubKey, byte[] textByte, byte[] signaByte, String algorithm)
throws Exception {
Signature sig = Signature.getInstance(algorithm);
sig.initVerify(pubKey);
sig.update(textByte);
return sig.verify(signaByte);
}
- md5WithRSA
- C# MD5withRSA
- sha1withRSA md5withRSA分析
- C#实现MD5WITHRSA签名
- 数字签名算法为:MD5withRSA
- 数字签名算法中MD5withRSA
- MD5withRSA签名 DES加密解密
- php实现MD5withRSA签名算法
- DELPHI实现标准SHA1WithRSA、MD5WithRSA算法
- C#实现标准JAVA的Md5withRsa算法
- java中数字签名MD5withRSA和SHA1withRSA
- vue+webpack在“双十一”导购产品的技术实践(转发)
- IDEA搭建服务器(tomcat)及数据库(mysql)并创建webapp项目
- 5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果
- 【Android - 框架】之Retrofit+RxJava的使用
- web前端之HTML中a标签锚点
- md5WithRSA
- POJ2051 Argus 优先队列
- 【拥抱开源】十年之做C#屏幕截图工具全面开源
- 16.11.10
- Cisco's router and switch : Encryption and Crack
- Rectangle Area
- 多线程等待唤醒机制:从wait()和sleep()的差别说起
- Deeply-Recursive Convolutional Network for Image Super-Resolution 笔记
- javascript事件委托实现京东首页头部城市选择