论坛首页 综合技术论坛

数字签名算法:DSA

浏览 3434 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2019-01-18  

 

private static PrivateKey privateKey = null;
private static PublicKey publicKey = null;

@BeforeClass
public static void init() {
    KeyPairGenerator keyPairGenerator = null;
    try {
        keyPairGenerator = KeyPairGenerator.getInstance("DSA");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    keyPairGenerator.initialize(1024, new SecureRandom());
    KeyPair keyPair = keyPairGenerator.generateKeyPair();

    privateKey = keyPair.getPrivate(); // sun.security.ec.ECPrivateKeyImpl
    publicKey = keyPair.getPublic(); // sun.security.ec.ECPublicKeyImpl

    byte[] privateKeyEncoded = ((Key) privateKey).getEncoded();
    byte[] publicKeyEncoded = ((Key) publicKey).getEncoded();
    System.out.println("private key: " + Base64.byteArrayToBase64(privateKeyEncoded));
    System.out.println("public key: " + Base64.byteArrayToBase64(publicKeyEncoded));
}

 

@org.junit.Test
public void test() {
    String message = "13120983870";
//        String message = "13120983870222222222";
    System.out.println(message);

    KeyFactory keyFactory = null;
    try {
        keyFactory = KeyFactory.getInstance("DSA");
    } catch (NoSuchAlgorithmException e) {
        Assert.fail("no such algorithm: " + e.getMessage());
    }

    // 签名
    Signature signature = null;
    try {
        signature = Signature.getInstance("SHA256withDSA"); // NONEwithDSA, SHA256withDSA
    } catch (NoSuchAlgorithmException e) {
        Assert.fail("no such algorithm: " + e.getMessage());
    }
    try {
        signature.initSign(privateKey);
    } catch (InvalidKeyException e) {
        Assert.fail("invalid key: " + e.getMessage());
    }

    byte[] sign = null;
    try {
        signature.update(message.getBytes());
        sign = signature.sign();
    } catch (SignatureException e) {
        e.printStackTrace();
        Assert.fail("signature: " + e.getMessage());
    }
    System.out.println("signature: " + Base64.byteArrayToBase64(sign));

    // 验证
    try {
        signature.initVerify(publicKey);
    } catch (InvalidKeyException e) {
        Assert.fail("invalid key: " + e.getMessage());
    }

    try {
        signature.update(message.getBytes());
        boolean result = signature.verify(sign);
        Assert.assertTrue(result);
    } catch (SignatureException e) {
        Assert.fail("signature: " + e.getMessage());
    }
}

 

private key: MIIBSwIBADCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoEFgIUWljb9g4BGWyIIwY9IbL2GbrrSeY=
public key: MIIBtzCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYQAAoGAGWINaQe4sr9eIFMZk6TMkWxwufiClPTerjSH/KBpU86RDrzOALu+graaXFJdhM1G03CSnM7JItMzIzzLuosVMNj/FVxUSC/utLlAajdlQ3TNP2ZEtLFvBw8YKewUbfXoGCfGcbnPRcPBYpkoj3/lhIhg0u06na1ZFD/soH8JWEo=
13120983870
signature: MCwCFHSq4vK8GPOt+vyChr48ubF3YSCmAhRrIUKJHhpYhqKSJq81OV52g1wqvw==

 

 

论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics