Implementation of RSA Digital signature with JDK's own method
1 package jdbc.pro.lin; 2 3 import java.security.InvalidKeyException; 4 import java.security.NoSuchAlgorithmException; 5 import java.security.PrivateKey; 6 import java.security.PublicKey; 7 import java.security.Signature; 8 import java.security.SignatureException; 9 import java.util.Map; 10 11 import jdbc.pro.lin.MyRSA 12 13 / * 14 * RSA digital signature, borrow the algorithm in MyRSA, do not repeat 15 * digital signature follows the principle of "private key signature, public key verification", because the private key is personal identity authentication 16 * @ author Kinsley 17 * 18 * / 19 public class MySignature {20 21 / * * digital signature algorithm. JDK only provides MD2withRSA, MD5withRSA and SHA1withRSA. Other algorithms need third-party packages to support * / 22 public static final String SIGNATURE_ALGORITHM = "SHA1withRSA"; 23 24 public static final String PLAIN_TEXT = "MANUTD is the greatest club in the world"; 25 public static void main (String [] args) 26 {27 / / establish two public and private key pairs 28 Map keyMap1 = MyRSA.generateKeyBytes () 29 PublicKey publicKey1 = MyRSA.restorePublicKey (keyMap1.get (MyRSA.PUBLIC_KEY)); 30 PrivateKey privateKey1 = MyRSA.restorePrivateKey (keyMap1.get (MyRSA.PRIVATE_KEY)); 31 32 Map keyMap2 = MyRSA.generateKeyBytes (); 33 PublicKey publicKey2 = MyRSA.restorePublicKey (keyMap2.get (MyRSA.PUBLIC_KEY)); 34 PrivateKey privateKey2 = MyRSA.restorePrivateKey (keyMap2.get (MyRSA.PRIVATE_KEY)) 35 36 / * * suppose that now A signs and sends a message to B 37 * An encrypts with B's public key 38 * sign 39 * / 40 byte [] encodedText = MyRSA.RSAEncode (publicKey2, PLAIN_TEXT.getBytes ()); 41 byte [] signature = sign (privateKey1, PLAIN_TEXT.getBytes ()) 42 43 / * * 44 * now B received the message from An and performed two operations 45 * decrypting the private key of B to get the plaintext 46 * verifying the plaintext and the public key of A 47 * / 48 49 byte [] decodedText = MyRSA.RSADecode (privateKey2, encodedText). GetBytes () 50 System.out.println ("Decoded Text:" + new String (decodedText)); 51 52 System.out.println ("Signature is" + verify (publicKey1, signature, decodedText)); 53} 54 55 / * 56 * signature, 57 * 1 in three steps. Instantiate, pass in the algorithm 58 * 2. Initialize, pass in the private key 59 * 3. Signature 60 * @ param key 61 * @ param plainText 62 * @ return 63 * / 64 public static byte [] sign (PrivateKey privateKey, byte [] plainText) 65 {66 try {67 / / instantiate 68 Signature signature = Signature.getInstance (SIGNATURE_ALGORITHM); 69 70 / / initialize, pass in the private key 71 signature.initSign (privateKey) 72 73 / Update 74 signature.update (plainText); 75 76 / / signature 77 return signature.sign (); 78 79} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {80 / / TODO Auto-generated catch block 81 e.printStackTrace () 82} 83 84 return null; 85} 86 87 / * 88 * check, take 89 * 1 in three steps. Instantiate, pass in algorithm 90 * 2. Initialize, pass in the public key 91 * 3. Check signature 92 * @ param publicKey 93 * @ param signatureVerify 94 * @ param plainText 95 * @ return 96 * / 97 public static boolean verify (PublicKey publicKey, byte [] signatureVerify, byte [] plainText) 98 {99 try {100 / / instantiate 101 Signature signature = Signature.getInstance (SIGNATURE_ALGORITHM) 102 103 / / initialize 104 signature.initVerify (publicKey); 105 106 / update 107 signature.update (plainText); 108 109 / / check 110 return signature.verify (signatureVerify) Catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {112 / / TODO Auto-generated catch block113 e.printStackTrace (); 114} 115116 return false;117} 118}