java使用Cipher进行签名和验签

public static void main(String[] args) {
        try {
            String plainText = "duwenlei";
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("rsa");
            keyPairGenerator.initialize(2048);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            
            //签名
            MessageDigest md = MessageDigest.getInstance("sha1");
            byte[] encHash = md.digest(plainText.getBytes());
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyPair.getPrivate().getEncoded());
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            Key privateKey =keyFactory.generatePrivate(keySpec);
            Cipher cipherSign = Cipher.getInstance(keyFactory.getAlgorithm());
            cipherSign.init(Cipher.ENCRYPT_MODE,privateKey);
//            cipherSign.init(Cipher.ENCRYPT_MODE,keyPair.getPrivate());
            byte[] encHashSign = cipherSign.doFinal(encHash);
            
            //验签
            X509EncodedKeySpec pkcs8EncodedKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded());
            KeyFactory keyFactoryPub = KeyFactory.getInstance("RSA");
            Key publicKey = keyFactoryPub.generatePublic(pkcs8EncodedKeySpec);
            Cipher cipherVer = Cipher.getInstance(keyFactoryPub.getAlgorithm());
            cipherVer.init(Cipher.DECRYPT_MODE, publicKey);
//            cipherVer.init(Cipher.DECRYPT_MODE, keyPair.getPublic());
            byte[] decHashVer = cipherVer.doFinal(encHashSign);
            MessageDigest md2 = MessageDigest.getInstance("sha1");
            byte[] decHash = md2.digest(plainText.getBytes());
            
            
//            System.out.println("encHash = " + byte2hex(encHash));
//            System.out.println("decHashVer = " + byte2hex(decHashVer));
//            System.out.println("decHash = " + byte2hex(decHash));
            System.out.println("签名是否正确:" + byte2hex(decHashVer).equals(byte2hex(decHash)));
            
            PEMWriter privateKeyWriter = new PEMWriter(new FileWriter(new File("D://privateKey.key")));
            privateKeyWriter.writeObject(keyPair.getPrivate());
            privateKeyWriter.flush();
            privateKeyWriter.close();
            PEMWriter publicKeyWriter = new PEMWriter(new FileWriter(new File("D://publicKey.key")));
            publicKeyWriter.writeObject(keyPair.getPublic());
            publicKeyWriter.flush();
            publicKeyWriter.close();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static byte[] hex2byte(String strhex) {
        if (strhex == null) {
            return null;
        }
        int l = strhex.length();
        if (l % 2 == 1) {
            return null;
        }
        byte[] b = new byte[l / 2];
        for (int i = 0; i != l / 2; i++) {
            b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),16);
        }
        return b;
    }

    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp ;
            }
        }
        return hs.toUpperCase();
    }

 

转载于:https://www.cnblogs.com/duwenlei/p/4301108.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值