我是靠谱客的博主 欣喜星月,最近开发中收集的这篇文章主要介绍java 16进制公钥_16进制公钥,PHP RSA验证数据问题,跪求大神来解答,我所有的园豆都给你???!!!!...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

public class RSA {

private static final char[] HEX_LOOKUP_STRING = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',

'd', 'e', 'f'};

public static final String SIGN_ALGORITHMS = "SHA1WithRSA";

/**

* 将字节数组转换为16进制字符串的形式.

*/

public static final String bytesToHexStr(byte[] bcd) {

StringBuffer s = new StringBuffer(bcd.length * 2);

for (int i = 0; i < bcd.length; i++) {

s.append(HEX_LOOKUP_STRING[(bcd[i] >>> 4) & 0x0f]);

s.append(HEX_LOOKUP_STRING[bcd[i] & 0x0f]);

}

return s.toString();

}

/**

* 将16进制字符串还原为字节数组.

*/

public static final byte[] hexStrToBytes(String s) {

byte[] bytes;

bytes = new byte[s.length() / 2];

for (int i = 0; i < bytes.length; i++) {

bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);

}

return bytes;

}

/**

* 得到私钥对象

*

* @param key 密钥字符串(经过16进制编码)

* @throws Exception

*/

public static PrivateKey getPrivateKey(String key) throws Exception {

byte[] keyBytes = hexStrToBytes(key.trim());

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

return keyFactory.generatePrivate(keySpec);

}

/**

* 得到公钥对象

*

* @param key 密钥字符串(经过16进制编码)

* @throws Exception

*/

public static PublicKey getPublicKey(String key) throws Exception {

byte[] keyBytes = hexStrToBytes(key.trim());

X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

return keyFactory.generatePublic(keySpec);

}

/**

* 本方法使用SHA1withRSA签名算法产生签名

*

* @param String priKey 签名时使用的私钥(16进制编码)

* @param String src 签名的原字符串

* @return String 签名的返回结果(16进制编码)。当产生签名出错的时候,返回null。

* @throws PayException

*/

public static String sign(PrivateKey privateKey, String src) throws Exception {

Signature sigEng = Signature.getInstance(SIGN_ALGORITHMS);

sigEng.initSign(privateKey);

sigEng.update(src.getBytes("UTF-8"));

byte[] signature = sigEng.sign();

return bytesToHexStr(signature);

}

/**

* 本方法使用SHA1withRSA签名算法验证签名

*

* @param String pubKey 验证签名时使用的公钥(16进制编码)

* @param String sign 签名结果(16进制编码)

* @param String src 签名的原字符串

* @throws PayException 验证失败时抛出异常

*/

public static final void verify(PublicKey publicKey, String sign, String src) throws Exception {

Signature sigEng = Signature.getInstance("SHA1withRSA");

sigEng.initVerify(publicKey);

sigEng.update(src.getBytes("UTF-8"));

byte[] sign1 = hexStrToBytes(sign);

if (!sigEng.verify(sign1)) {

throw new Exception("verify failed");

}

}

/**

* 本方法用于产生1024位RSA公私钥对。

*

* @return 私钥、公钥

*/

private static String[] genRSAKeyPair() throws Exception {

KeyPairGenerator rsaKeyGen = null;

KeyPair rsaKeyPair = null;

System.out.println("Generating a pair of RSA key ... ");

rsaKeyGen = KeyPairGenerator.getInstance("RSA");

SecureRandom random = new SecureRandom();

random.setSeed(("" + System.currentTimeMillis() * Math.random() * Math.random()).getBytes());

rsaKeyGen.initialize(1024, random);

rsaKeyPair = rsaKeyGen.genKeyPair();

PublicKey rsaPublic = rsaKeyPair.getPublic();

PrivateKey rsaPrivate = rsaKeyPair.getPrivate();

String privateAndPublic[] = new String[2];

privateAndPublic[0] = bytesToHexStr(rsaPrivate.getEncoded());

privateAndPublic[1] = bytesToHexStr(rsaPublic.getEncoded());

System.out.println("私钥:" + privateAndPublic[0]);

System.out.println("公钥:" + privateAndPublic[1]);

System.out.println("1024-bit RSA key GENERATED.");

return privateAndPublic;

}

/**

* 请在java工程中跑此main函数

*

* @param args

*/

public static void main(String[] args) {

try {

String[] privateAndPublic = genRSAKeyPair();

PrivateKey privateKey = getPrivateKey(privateAndPublic[0]);

PublicKey publicKey = getPublicKey(privateAndPublic[1]);

String signSrc = URLEncoder.encode(

"urstestc@163.com1您好20101227105117201012271131591203.86.63.98203.86.63.9854416", "UTF-8");

String sign = sign(privateKey, signSrc);

System.out.println("原始串:" + signSrc);

System.out.println("签名:" + sign);

verify(publicKey, sign, signSrc);

System.out.println("验证签名成功");

} catch (Exception e) {

e.printStackTrace();

}

}

}

这是生成的方法

最后

以上就是欣喜星月为你收集整理的java 16进制公钥_16进制公钥,PHP RSA验证数据问题,跪求大神来解答,我所有的园豆都给你???!!!!...的全部内容,希望文章能够帮你解决java 16进制公钥_16进制公钥,PHP RSA验证数据问题,跪求大神来解答,我所有的园豆都给你???!!!!...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部