Java实现ECDSA签名算法
ECDSA签名算法
packagecom.albedo.security; /** *DSA加解密实现 */ publicclassECDSAUtilsextendsBase{ //字符编码 publicstaticfinalStringALGORITHM="EC"; publicstaticfinalStringSIGN_ALGORITHM="SHA1withECDSA"; /** *ECDSA验签 * *@paramsign加密签名 *@paramstr加密字符串 *@parampublicKey公钥 *@return密文 *@throwsException加密过程中的异常信息 */ publicstaticbooleanverify(Stringsign,Stringstr,StringpublicKey)throwsException{ returnverify(sign,str,publicKey,ALGORITHM,SIGN_ALGORITHM); } /** *ECDSA签名 * *@paramstr加密字符串 *@paramprivateKey私钥 *@return铭文 *@throwsException解密过程中的异常信息 */ publicstaticStringsign(Stringstr,StringprivateKey)throwsException{ returnsign(str,privateKey,ALGORITHM,SIGN_ALGORITHM); } publicstaticvoidmain(String[]args)throwsException{ StringpublicKey=getPublicKey(ALGORITHM,512); StringprivateKey=getPrivateKey(ALGORITHM,512); Stringmessage="我要测试DSA"; Stringsign=sign(message,privateKey); System.out.println(verify(sign,message,publicKey)); } }
基础代码
packagecom.albedo.security; importcom.albedo.num.ByteUtils; importjava.security.KeyFactory; importjava.security.KeyPair; importjava.security.KeyPairGenerator; importjava.security.PrivateKey; importjava.security.PublicKey; importjava.security.Signature; importjava.security.spec.PKCS8EncodedKeySpec; importjava.security.spec.X509EncodedKeySpec; importjava.util.Objects; classBase{ staticKeyPairkeyPair; /** *生成密钥实际方法,可以使用多种方式 *一篇文档提供一下多种方式 *{"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"}, * *@paramalgorithm *@parambit *@return *@throwsException */ protectedstaticKeyPaircreateKey(Stringalgorithm,intbit)throwsException{ KeyPairGeneratorkeyPairGenerator=KeyPairGenerator.getInstance(algorithm); keyPairGenerator.initialize(bit); KeyPairkeyPair=keyPairGenerator.generateKeyPair(); returnkeyPair; } /** *获取公钥 * *@return *@throwsException */ publicstaticStringgetPublicKey(Stringalgorithm,intbit)throwsException{ if(Objects.isNull(keyPair)){ keyPair=createKey(algorithm,bit); } returnByteUtils.byteArr2HexStr(keyPair.getPublic().getEncoded()); } /** *获取私钥 * *@return *@throwsException */ publicstaticStringgetPrivateKey(Stringalgorithm,intbit)throwsException{ if(Objects.isNull(keyPair)){ keyPair=createKey(algorithm,bit); } returnByteUtils.byteArr2HexStr(keyPair.getPrivate().getEncoded()); } /** *非对称加密签名 *@paramstr *@paramprivateKey *@paramalgorithm *@paramsignAlgorithm *@return *@throwsException */ publicstaticStringsign(Stringstr,StringprivateKey,Stringalgorithm,StringsignAlgorithm)throwsException{ PKCS8EncodedKeySpecpkcs8EncodedKeySpec=newPKCS8EncodedKeySpec(ByteUtils.hexstr2ByteArr(privateKey)); KeyFactorykeyFactory=KeyFactory.getInstance(algorithm); PrivateKeydsaPrivateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec); Signaturesignature=Signature.getInstance(signAlgorithm); signature.initSign(dsaPrivateKey); signature.update(str.getBytes()); returnByteUtils.byteArr2HexStr(signature.sign()); } /** *非对称加密验证 *@paramsign *@paramstr *@parampublicKey *@paramalgorithm *@paramsignAlgorithm *@return *@throwsException */ publicstaticbooleanverify(Stringsign,Stringstr,StringpublicKey,Stringalgorithm,StringsignAlgorithm)throwsException{ //base64编码的公钥 X509EncodedKeySpecx509EncodedKeySpec=newX509EncodedKeySpec(ByteUtils.hexstr2ByteArr(publicKey)); KeyFactorykeyFactory=KeyFactory.getInstance(algorithm); PublicKeydsaPublicKey=keyFactory.generatePublic(x509EncodedKeySpec); Signaturesignature=Signature.getInstance(signAlgorithm); signature.initVerify(dsaPublicKey); signature.update(str.getBytes()); returnsignature.verify(ByteUtils.hexstr2ByteArr(sign)); } }
以上就是Java实现ECDSA签名算法的详细内容,更多关于JavaECDSA签名算法的资料请关注毛票票其它相关文章!