Java實(shí)現(xiàn)ECDSA簽名算法
更新時(shí)間:2020年10月10日 16:21:04 作者:重設(shè)代碼的天空
這篇文章主要介紹了Java實(shí)現(xiàn)ECDSA簽名算法,幫助大家更好得利用Java實(shí)現(xiàn)機(jī)器學(xué)習(xí)算法,感興趣的朋友可以了解下
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 = "我要測(cè)試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)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis-Plus 之selectMaps、selectObjs、selectCount、selectO
本文主要介紹了MyBatis-Plus 之selectMaps、selectObjs、selectCount、selectOne的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03關(guān)于Java的Condition接口最佳理解方式
這篇文章主要介紹了關(guān)于Java的Condition接口最佳理解方式,Condition就是實(shí)現(xiàn)了管程里面的條件變量,Java?語(yǔ)言內(nèi)置的管程里只有一個(gè)條件變量,而Lock&Condition實(shí)現(xiàn)的管程支持多個(gè)條件變量,需要的朋友可以參考下2023-05-05Java執(zhí)行cmd命令的舉例與注意事項(xiàng)
Java應(yīng)用程序主要是通過Runtime和Process兩個(gè)類來(lái)執(zhí)行cmd命令,下面這篇文章主要給大家介紹了關(guān)于Java執(zhí)行cmd命令的方法與注意事項(xiàng),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02高并發(fā)系統(tǒng)的限流詳解及實(shí)現(xiàn)
這篇文章主要介紹了高并發(fā)系統(tǒng)的限流詳解及實(shí)現(xiàn),內(nèi)容詳細(xì),小編覺得很不錯(cuò),這里分享給大家,供需要的朋友參考。隨小編一起看看吧。2017-11-11intellij idea如何配置網(wǎng)絡(luò)代理
intellij idea所在的這臺(tái)電腦本身上不了網(wǎng),要通過代理上網(wǎng),那么intellij idea怎么設(shè)置代理上網(wǎng)呢?今天通過本文給大家分享intellij idea如何配置網(wǎng)絡(luò)代理,感興趣的朋友一起看看吧2023-10-10SpringBoot使用Minio進(jìn)行文件存儲(chǔ)的實(shí)現(xiàn)
本文主要介紹了SpringBoot使用Minio進(jìn)行文件存儲(chǔ)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07