Java加密与解密的艺术~数字签名~DSA实现
生活随笔
收集整理的這篇文章主要介紹了
Java加密与解密的艺术~数字签名~DSA实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?DSA 實現(xiàn)
/*** 2008-6-13*/ package org.zlex.chapter09_2;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.SecureRandom; import java.security.Signature; import java.security.interfaces.DSAPrivateKey; import java.security.interfaces.DSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map;/*** DSA安全編碼組件* * @author 梁棟* @version 1.0*/ public abstract class DSACoder {/*** 數(shù)字簽名密鑰算法*/public static final String ALGORITHM = "DSA";/*** 數(shù)字簽名* 簽名/驗證算法*/public static final String SIGNATURE_ALGORITHM = "SHA1withDSA";/*** 公鑰*/private static final String PUBLIC_KEY = "DSAPublicKey";/*** 私鑰*/private static final String PRIVATE_KEY = "DSAPrivateKey";/*** DSA密鑰長度 * 默認(rèn)1024位, * 密鑰長度必須是64的倍數(shù), * 范圍在512至1024位之間(含)*/private static final int KEY_SIZE = 1024;/*** 簽名* * @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);// 實例化密鑰工廠KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);// 生成私鑰對象PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);// 實例化SignatureSignature signature = Signature.getInstance(SIGNATURE_ALGORITHM);// 初始化Signaturesignature.initSign(priKey);// 更新signature.update(data);// 簽名return signature.sign();}/*** 校驗* * @param data* 待校驗數(shù)據(jù)* @param publicKey* 公鑰* @param sign* 數(shù)字簽名* * @return boolean 校驗成功返回true 失敗返回false* @throws Exception* */public static boolean verify(byte[] data, byte[] publicKey, byte[] sign)throws Exception {// 還原公鑰// 轉(zhuǎn)換公鑰材料X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);// 實例化密鑰工廠KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);// 取公鑰匙對象PublicKey pubKey = keyFactory.generatePublic(keySpec);// 實例話SignatureSignature signature = Signature.getInstance(SIGNATURE_ALGORITHM);// 初始化Signaturesignature.initVerify(pubKey);// 更新signature.update(data);// 驗證return signature.verify(sign);}/*** 生成密鑰* * @return 密鑰對象* @throws Exception*/public static Map<String, Object> initKey() throws Exception {// 初始化密鑰對兒生成器KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);// 實例化密鑰對兒生成器keygen.initialize(KEY_SIZE, new SecureRandom());// 實例化密鑰對兒KeyPair keys = keygen.genKeyPair();DSAPublicKey publicKey = (DSAPublicKey) keys.getPublic();DSAPrivateKey privateKey = (DSAPrivateKey) keys.getPrivate();// 封裝密鑰Map<String, Object> map = new HashMap<String, Object>(2);map.put(PUBLIC_KEY, publicKey);map.put(PRIVATE_KEY, privateKey);return map;}/*** 取得私鑰* * @param keyMap* 密鑰Map* @return byte[] 私鑰* @throws Exception*/public static byte[] getPrivateKey(Map<String, Object> keyMap)throws Exception {Key key = (Key) keyMap.get(PRIVATE_KEY);return key.getEncoded();}/*** 取得公鑰* * @param keyMap* 密鑰Map* @return byte[] 公鑰* @throws Exception*/public static byte[] getPublicKey(Map<String, Object> keyMap)throws Exception {Key key = (Key) keyMap.get(PUBLIC_KEY);return key.getEncoded();} }DSA 示例
/*** 2009-5-7*/ package org.zlex.chapter09_2;import static org.junit.Assert.*;import java.util.Map;import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Hex; import org.junit.Before; import org.junit.Test;/*** DSA簽名校驗* * @author 梁棟* @version 1.0*/ public class DSACoderTest {/*** 公鑰*/private byte[] publicKey;/*** 私鑰*/private byte[] privateKey;/*** 初始化密鑰* * @throws Exception*/@Beforepublic void initKey() throws Exception {Map<String, Object> keyMap = DSACoder.initKey();publicKey = DSACoder.getPublicKey(keyMap);privateKey = DSACoder.getPrivateKey(keyMap);System.err.println("公鑰: \n" + Base64.encodeBase64String(publicKey));System.err.println("私鑰: \n" + Base64.encodeBase64String(privateKey));}/*** 校驗簽名* * @throws Exception*/@Testpublic void test() throws Exception {String inputStr = "DSA數(shù)字簽名";byte[] data = inputStr.getBytes();// 產(chǎn)生簽名byte[] sign = DSACoder.sign(data, privateKey);System.err.println("簽名:\r" + Hex.encodeHexString(sign));// 驗證簽名boolean status = DSACoder.verify(data, publicKey, sign);System.err.println("狀態(tài):\r" + status);assertTrue(status);}}總結(jié)
以上是生活随笔為你收集整理的Java加密与解密的艺术~数字签名~DSA实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用php编写xml,PHP 读取和编写
- 下一篇: html语言鼠标悬停特效,CSS3鼠标悬