Java實(shí)現(xiàn)ECDSA簽名算法
ECDSA簽名算法
package com.albedo.security;/** * DSA 加解密實(shí)現(xiàn) */public class ECDSAUtils extends Base { //字符編碼 public static final String ALGORITHM = 'EC'; public static final String SIGN_ALGORITHM = 'SHA1withECDSA'; /** * ECDSA 驗(yàn)簽 * * @param sign 加密簽名 * @param str 加密字符串 * @param publicKey 公鑰 * @return 密文 * @throws Exception 加密過程中的異常信息 */ public static boolean verify(String sign, String str, String publicKey) throws Exception { return verify(sign, str, publicKey, ALGORITHM, SIGN_ALGORITHM); } /** * ECDSA 簽名 * * @param str 加密字符串 * @param privateKey 私鑰 * @return 銘文 * @throws Exception 解密過程中的異常信息 */ public static String sign(String str, String privateKey) throws Exception { return sign(str, privateKey, ALGORITHM, SIGN_ALGORITHM); } public static void main(String[] args) throws Exception { String publicKey = getPublicKey(ALGORITHM, 512); String privateKey = getPrivateKey(ALGORITHM, 512); String message = '我要測試DSA'; String sign = sign(message, privateKey); System.out.println(verify(sign, message, publicKey)); }}
基礎(chǔ)代碼
package com.albedo.security;import com.albedo.num.ByteUtils;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.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.Objects;class Base { static KeyPair keyPair; /** * 生成密鑰實(shí)際方法,可以使用多種方式 * 一篇文檔提供一下多種方式 * { 'DSA', 'SHA1withDSA', '1024' }, { 'DSA', 'SHA256withDSA', '1024' }, * { 'DSA', 'SHA256withDSA', '2048' }, { 'RSA', 'SHA256withRSA', '1024' }, * { 'RSA', 'SHA256withRSA', '2048' }, { 'RSA', 'SHA256withRSA', '3192' }, * { 'RSA', 'SHA512withRSA', '1024' }, { 'RSA', 'SHA512withRSA', '2048' }, * { 'RSA', 'SHA512withRSA', '3192' }, { 'RSA', 'MD5withRSA', '1024' }, * { 'RSA', 'MD5withRSA', '2048' }, * { 'RSA', 'MD5withRSA', '3192' }, { 'EC', 'SHA1withECDSA', '128' }, * { 'EC', 'SHA1withECDSA', '256' }, * { 'EC', 'SHA256withECDSA', '128' }, { 'EC', 'SHA256withECDSA', '256' }, * { 'EC', 'SHA512withECDSA', '128' }, { 'EC', 'SHA512withECDSA', '256' }, * * @param algorithm * @param bit * @return * @throws Exception */ protected static KeyPair createKey(String algorithm, int bit) throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm); keyPairGenerator.initialize(bit); KeyPair keyPair = keyPairGenerator.generateKeyPair(); return keyPair; } /** * 獲取公鑰 * * @return * @throws Exception */ public static String getPublicKey(String algorithm,int bit) throws Exception { if (Objects.isNull(keyPair)) { keyPair = createKey(algorithm,bit); } return ByteUtils.byteArr2HexStr(keyPair.getPublic().getEncoded()); } /** * 獲取私鑰 * * @return * @throws Exception */ public static String getPrivateKey(String algorithm,int bit) throws Exception { if (Objects.isNull(keyPair)) { keyPair = createKey(algorithm,bit); } return ByteUtils.byteArr2HexStr(keyPair.getPrivate().getEncoded()); } /** * 非對(duì)稱加密簽名 * @param str * @param privateKey * @param algorithm * @param signAlgorithm * @return * @throws Exception */ public static String sign(String str, String privateKey, String algorithm, String signAlgorithm) throws Exception { PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ByteUtils.hexstr2ByteArr(privateKey)); KeyFactory keyFactory = KeyFactory.getInstance(algorithm); PrivateKey dsaPrivateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Signature signature = Signature.getInstance(signAlgorithm); signature.initSign(dsaPrivateKey); signature.update(str.getBytes()); return ByteUtils.byteArr2HexStr(signature.sign()); } /** * 非對(duì)稱加密驗(yàn)證 * @param sign * @param str * @param publicKey * @param algorithm * @param signAlgorithm * @return * @throws Exception */ public static boolean verify(String sign, String str, String publicKey,String algorithm,String signAlgorithm) throws Exception { //base64編碼的公鑰 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ByteUtils.hexstr2ByteArr(publicKey)); KeyFactory keyFactory = KeyFactory.getInstance(algorithm); PublicKey dsaPublicKey = keyFactory.generatePublic(x509EncodedKeySpec); Signature signature = Signature.getInstance(signAlgorithm); signature.initVerify(dsaPublicKey); signature.update(str.getBytes()); return signature.verify(ByteUtils.hexstr2ByteArr(sign)); }}
以上就是Java實(shí)現(xiàn)ECDSA簽名算法的詳細(xì)內(nèi)容,更多關(guān)于Java ECDSA簽名算法的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. chat.asp聊天程序的編寫方法3. CSS 使用Sprites技術(shù)實(shí)現(xiàn)圓角效果4. phpstudy apache開啟ssi使用詳解5. 詳解瀏覽器的緩存機(jī)制6. ASP中if語句、select 、while循環(huán)的使用方法7. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?8. HTML中的XML數(shù)據(jù)島記錄編輯與添加9. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法10. 推薦一個(gè)好看Table表格的css樣式代碼詳解
