Java加密与解密的艺术~数字签名~ECDSA实现
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java加密与解密的艺术~数字签名~ECDSA实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                ?
ECDSA 實現(xiàn)
/*** 2009-10-10*/ package org.zlex.chapter09_3;import java.math.BigInteger; 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.Security; import java.security.Signature; import java.security.interfaces.ECPrivateKey; import java.security.interfaces.ECPublicKey; import java.security.spec.ECFieldFp; import java.security.spec.ECParameterSpec; import java.security.spec.ECPoint; import java.security.spec.EllipticCurve; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map;import org.bouncycastle.jce.provider.BouncyCastleProvider;/*** ECDSA安全編碼組件* * @author 梁棟* @version 1.0* @since 1.0*/ public abstract class ECDSACoder {/*** 數(shù)字簽名 密鑰算法*/private static final String KEY_ALGORITHM = "ECDSA";/*** 數(shù)字簽名 簽名/驗證算法* * Bouncy Castle支持以下7種算法* NONEwithECDSA * RIPEMD160withECDSA * SHA1withECDSA* SHA224withECDSA * SHA256withECDSA * SHA384withECDSA * SHA512withECDSA*/private static final String SIGNATURE_ALGORITHM = "SHA512withECDSA";/*** 公鑰*/private static final String PUBLIC_KEY = "ECDSAPublicKey";/*** 私鑰*/private static final String PRIVATE_KEY = "ECDSAPrivateKey";/*** 初始化密鑰* * @return Map 密鑰Map* @throws Exception*/public static Map<String, Object> initKey() throws Exception {// 加入BouncyCastleProvider支持Security.addProvider(new BouncyCastleProvider());BigInteger p = new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839");ECFieldFp ecFieldFp = new ECFieldFp(p);BigInteger a = new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc",16);BigInteger b = new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a",16);EllipticCurve ellipticCurve = new EllipticCurve(ecFieldFp, a, b);BigInteger x = new BigInteger("110282003749548856476348533541186204577905061504881242240149511594420911");BigInteger y = new BigInteger("869078407435509378747351873793058868500210384946040694651368759217025454");ECPoint g = new ECPoint(x, y);BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307");ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, g,n, 1);// 實例化密鑰對兒生成器KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM);// 初始化密鑰對兒生成器kpg.initialize(ecParameterSpec, new SecureRandom());// 生成密鑰對兒KeyPair keypair = kpg.generateKeyPair();ECPublicKey publicKey = (ECPublicKey) keypair.getPublic();ECPrivateKey privateKey = (ECPrivateKey) keypair.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();}/*** 簽名* * @param data* 待簽名數(shù)據(jù)* @param privateKey* 私鑰* @return byte[] 數(shù)字簽名* @throws Exception*/public static byte[] sign(byte[] data, byte[] privateKey) throws Exception {// 加入BouncyCastleProvider支持Security.addProvider(new BouncyCastleProvider());// 轉(zhuǎn)換私鑰材料PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);// 實例化密鑰工廠KeyFactory keyFactory = KeyFactory.getInstance(KEY_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 {// 加入BouncyCastleProvider支持Security.addProvider(new BouncyCastleProvider());// 轉(zhuǎn)換公鑰材料X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);// 實例化密鑰工廠KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);// 生成公鑰PublicKey pubKey = keyFactory.generatePublic(keySpec);// 實例化SignatureSignature signature = Signature.getInstance(SIGNATURE_ALGORITHM);// 初始化Signaturesignature.initVerify(pubKey);// 更新signature.update(data);// 驗證return signature.verify(sign);} }ECDSA 示例
/*** 2008-6-11*/ package org.zlex.chapter09_3;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;/*** ECDSA數(shù)字簽名校驗* * @author 梁棟* @version 1.0*/ public class ECDSACoderTest {/*** 公鑰*/private byte[] publicKey;/*** 私鑰*/private byte[] privateKey;/*** 初始化密鑰* * @throws Exception*/@Beforepublic void initKey() throws Exception {Map<String, Object> keyMap = ECDSACoder.initKey();publicKey = ECDSACoder.getPublicKey(keyMap);privateKey = ECDSACoder.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 = "ECDSA 數(shù)字簽名";byte[] data = inputStr.getBytes();// 產(chǎn)生簽名byte[] sign = ECDSACoder.sign(data, privateKey);System.err.println("簽名:\r" + Hex.encodeHexString(sign));// 驗證簽名boolean status = ECDSACoder.verify(data, publicKey, sign);System.err.println("狀態(tài):\r" + status);assertTrue(status);}}總結(jié)
以上是生活随笔為你收集整理的Java加密与解密的艺术~数字签名~ECDSA实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: JNDI配置原理详解
- 下一篇: ajax工作中使用模板
