javaDSA实现加密和解密(签名和验证)

创建主函数测试

  public static void main(String[] args) throws Exception {
        
        String data = "跳梁小豆tlxd666";
        
        //创建秘钥生成器
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
        kpg.initialize(512);
        KeyPair keypair = kpg.generateKeyPair();//生成秘钥对
        DSAPublicKey publickey = (DSAPublicKey)keypair.getPublic();
        DSAPrivateKey privatekey = (DSAPrivateKey)keypair.getPrivate();
        
        //签名和验证
        //签名
        Signature sign = Signature.getInstance("SHA1withDSA");
        sign.initSign(privatekey);//初始化私钥,签名只能是私钥
        sign.update(data.getBytes());//更新签名数据
        byte[] b = sign.sign();//签名,返回签名后的字节数组
        
        
        //验证
        sign.initVerify(publickey);//初始化公钥,验证只能是公钥
        sign.update(data.getBytes());//更新验证的数据
        boolean result = sign.verify(b);//签名和验证一致返回true  不一致返回false
        System.out.println(result);
        
        
    }

 

posted @ 2018-09-26 19:36  跳梁小豆  阅读(2214)  评论(0编辑  收藏  举报