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(); }
如果有使用请标明来源:http://www.cnblogs.com/duwenlei/