Java加密与解密的艺术~数字签名~RSA实现
生活随笔
收集整理的這篇文章主要介紹了
Java加密与解密的艺术~数字签名~RSA实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
RSA 實(shí)現(xiàn)
/*** 2008-6-11*/ package org.zlex.chapter09_1;import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;import java.util.HashMap; import java.util.Map;/*** RSA安全編碼組件* * @author 梁棟* @version 1.0*/ public abstract class RSACoder {/*** 數(shù)字簽名* 密鑰算法*/public static final String KEY_ALGORITHM = "RSA";/*** 數(shù)字簽名* 簽名/驗(yàn)證算法*/public static final String SIGNATURE_ALGORITHM = "SHA1withRSA";/*** 公鑰*/private static final String PUBLIC_KEY = "RSAPublicKey";/*** 私鑰*/private static final String PRIVATE_KEY = "RSAPrivateKey";/*** RSA密鑰長(zhǎng)度 默認(rèn)1024位,* 密鑰長(zhǎng)度必須是64的倍數(shù), * 范圍在512至65536位之間。*/private static final int KEY_SIZE = 512;/*** 簽名* * @param data* 待簽名數(shù)據(jù)* @param privateKey* 私鑰* @return byte[] 數(shù)字簽名* @throws Exception*/public static byte[] sign(byte[] data, byte[] privateKey) throws Exception {// 轉(zhuǎn)換私鑰材料PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);// 實(shí)例化密鑰工廠KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);// 取私鑰匙對(duì)象PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);// 實(shí)例化SignatureSignature signature = Signature.getInstance(SIGNATURE_ALGORITHM);// 初始化Signaturesignature.initSign(priKey);// 更新signature.update(data);// 簽名return signature.sign();}/*** 校驗(yàn)* * @param data* 待校驗(yàn)數(shù)據(jù)* @param publicKey* 公鑰* @param sign* 數(shù)字簽名* * @return boolean 校驗(yàn)成功返回true 失敗返回false* @throws Exception* */public static boolean verify(byte[] data, byte[] publicKey, byte[] sign)throws Exception {// 轉(zhuǎn)換公鑰材料X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);// 實(shí)例化密鑰工廠KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);// 生成公鑰PublicKey pubKey = keyFactory.generatePublic(keySpec);// 實(shí)例化SignatureSignature signature = Signature.getInstance(SIGNATURE_ALGORITHM);// 初始化Signaturesignature.initVerify(pubKey);// 更新signature.update(data);// 驗(yàn)證return signature.verify(sign);}/*** 取得私鑰* * @param keyMap* @return* @throws Exception*/public static byte[] getPrivateKey(Map<String, Object> keyMap)throws Exception {Key key = (Key) keyMap.get(PRIVATE_KEY);return key.getEncoded();}/*** 取得公鑰* * @param keyMap* @return* @throws Exception*/public static byte[] getPublicKey(Map<String, Object> keyMap)throws Exception {Key key = (Key) keyMap.get(PUBLIC_KEY);return key.getEncoded();}/*** 初始化密鑰* * @return Map 密鑰對(duì)兒 Map* @throws Exception*/public static Map<String, Object> initKey() throws Exception {// 實(shí)例化密鑰對(duì)兒生成器KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);// 初始化密鑰對(duì)兒生成器keyPairGen.initialize(KEY_SIZE);// 生成密鑰對(duì)兒KeyPair keyPair = keyPairGen.generateKeyPair();// 公鑰RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();// 私鑰RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();// 封裝密鑰Map<String, Object> keyMap = new HashMap<String, Object>(2);keyMap.put(PUBLIC_KEY, publicKey);keyMap.put(PRIVATE_KEY, privateKey);return keyMap;} }RSA 示例?
/*** 2008-6-11*/ package org.zlex.chapter09_1;import static org.junit.Assert.*;import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Hex; import org.junit.Before; import org.junit.Test;import java.util.Map;/*** RSA數(shù)字簽名校驗(yàn)* * @author 梁棟* @version 1.0*/ public class RSACoderTest {/*** 公鑰*/private byte[] publicKey;/*** 私鑰*/private byte[] privateKey;/*** 初始化密鑰* * @throws Exception*/@Beforepublic void initKey() throws Exception {Map<String, Object> keyMap = RSACoder.initKey();publicKey = RSACoder.getPublicKey(keyMap);privateKey = RSACoder.getPrivateKey(keyMap);System.err.println("公鑰: \n" + Base64.encodeBase64String(publicKey));System.err.println("私鑰: \n" + Base64.encodeBase64String(privateKey));}/*** 校驗(yàn)* * @throws Exception*/@Testpublic void testSign() throws Exception {String inputStr = "RSA數(shù)字簽名";byte[] data = inputStr.getBytes();// 產(chǎn)生簽名byte[] sign = RSACoder.sign(data, privateKey);System.err.println("簽名:\n" + Hex.encodeHexString(sign));// 驗(yàn)證簽名boolean status = RSACoder.verify(data, publicKey, sign);System.err.println("狀態(tài):\n" + status);assertTrue(status);}} 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Java加密与解密的艺术~数字签名~RSA实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python编程从入门到实践~文件写入
- 下一篇: java 界面艺术字,Java 在Wor