budd/src/main/java/io/github/ehlxr/DemoECDSA.java

122 lines
4.4 KiB
Java
Raw Normal View History

2020-12-10 03:05:29 +00:00
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2020-12-09 09:55:24 +00:00
package io.github.ehlxr;
2017-08-03 05:18:52 +00:00
import org.apache.commons.codec.binary.Base64;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
2020-12-09 09:27:50 +00:00
* Created by ehlxr on 2017/8/1.
2017-08-03 05:18:52 +00:00
*/
public class DemoECDSA {
//摘要
private static final String strMsg = "hold on";
public static void main(String[] args) throws Exception {
jdkECDSA();
}
/**
* ECDSA 线 jdk1.7
*
* @throws Exception
*/
public static void jdkECDSA() throws Exception {
//1.初始化密钥
KeyPair keyPair = initKey();
//2.执行签名(用私钥签名)
ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate();
byte[] sign = privateKeySign(strMsg, ecPrivateKey);
String signStr = Base64.encodeBase64String(sign);
System.out.println("sign String :" + signStr);//数字签名格式转换,以便报文传输用
ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
String publicKeyStr = Base64.encodeBase64String(ecPublicKey.getEncoded());
System.out.println("publicKeyStr String :" + publicKeyStr);//提供给对端,以便于对端使用公钥验证签名
//3.验证签名(公钥验证签名)
boolean result = publicKeyVerify(Base64.decodeBase64(signStr), Base64.decodeBase64(publicKeyStr));
System.out.println("JDK DSA verify:" + result);
}
/**
* 1.ECDSA
*
* @return
* @throws Exception
*/
public static KeyPair initKey() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
keyPairGenerator.initialize(256); //key长度设置
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
/**
* 2.
*
* @return
* @throws Exception
*/
public static byte[] privateKeySign(String data, ECPrivateKey ecPrivateKey) throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("EC");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Signature signature = Signature.getInstance("SHA1withECDSA");
signature.initSign(privateKey);
signature.update(strMsg.getBytes());
byte[] sign = signature.sign();
return sign;
}
/**
* 3.++
*
* @throws Exception
*/
public static boolean publicKeyVerify(byte[] sign, byte[] dsaPublicKey) throws Exception {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(dsaPublicKey);
KeyFactory keyFactory = KeyFactory.getInstance("EC");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Signature signature = Signature.getInstance("SHA1withECDSA");
signature.initVerify(publicKey);
signature.update(strMsg.getBytes());
boolean result = signature.verify(sign);
return result;
}
}