1. RSA key pair generation algorithm
/ ** * Generate a random RSA key pair (public and private) * @return * / public static the Map <String, String> createKeyPair () throws Exception { the Map <String, String> = keyPairMap new new the HashMap <> () ; // key generator, based on the RSA algorithm the KeyPairGenerator KeyPairGenerator = KeyPairGenerator.getInstance ( "RSA" ); // initialize the key generator, a key size of 96-1024 bit keyPairGenerator.initialize (1024, SecureRandom.getInstance ( " SHA1PRNG " )); // generates a key pair, stored in the KeyPair KeyPair KeyPair = keyPairGenerator.generateKeyPair (); //Obtain the public key and the public key byte array PublicKey publicKey = keyPair.getPublic (); byte [] = publicKeyBytes publicKey.getEncoded (); // Get the private key and the private key byte array the PrivateKey privateKey = keyPair.getPrivate (); byte [] = privateKeyBytes privateKey.getEncoded (); // converted to hexadecimal characters stored in the map keyPairMap.put (PUBLIC_KEY, byte2Hex (publicKeyBytes)); keyPairMap.put (PRIVATE_KEY,, byte2Hex (privateKeyBytes)); return keyPairMap; }
2. Use the private key signature
/ ** * using a private key (RSA algorithm generates) data (typically digital abstract) sign * @param Contents * @param privateKeyString * @return * / public static String Sign (Contents String, String privateKeyString) throws Exception { IF (Contents! = null && privateKeyString! = null &&! "." the equals (Contents) &&! "" .equals (privateKeyString)) { // get the private key PrivateKey privateKey = KeyFactory.getInstance ( "RSA"). generatePrivate ( new new PKCS8EncodedKeySpec (hex2Byte (privateKeyString))) ;// instantiate performed using a hashing algorithm SHA, encrypted using RSA Signature Signature Signature = Signature.getInstance ( "SHA1withRSA" ); // load private key encryption hash code used signature.initSign (privateKey); / / hashed, the hash code generation is encrypted and returned Signature.update (contents.getBytes ( "UTF-. 8" )); // signed byte [] = signBytes signature.sign (); return byte2Hex (signBytes ); } return null ; }
3. Confirm signatures using public key
/ ** * using the public key to verify the signature data * @param Contents * @param signString * @param publicKeyString * @return * / public static Boolean checkSign (String Contents, signString String, String publicKeyString) throws Exception { IF (= Contents = null || signString == null || publicKeyString == null ) { return to false ; } // Get public Key . PublicKey publicKey = KeyFactory.getInstance ( "RSA") generatePublic ( new new (hex2Byte (publicKeyString)) X509EncodedKeySpec); // instantiate a hashed with SHA algorithm is encrypted using RSA Signature Signature signature = Signature.getInstance ( "SHA1withRSA" ); // load the public signature.initVerify (publicKey); // update the original data Signature.update (contents.getBytes ( "UTF-8" )); // returns the signature is correct return signature.verify (hex2Byte (signString)); }
--------------------------------------
Methods Tools
/ ** * the byte [] array is converted into a hexadecimal character. Generating a two byte character length corresponds. 1: 2 * @param bytes, the input byte [] array * @return 16 hexadecimal characters * / public static String byte2Hex ( byte [] bytes) { IF (bytes == null ) { return null ; } the StringBuilder Builder = new new the StringBuilder (); // iterate byte [] array, converting each byte into a number hexadecimal characters, then spliced together into a string for ( int I = 0; I <bytes.length ; I ++ ) { //When converting each byte into a 16 hexadecimal characters, bytes [i] & 0xff if the high bits are 0, the output will be removed, so + 0x100 (plus 1 bit higher), the two characters then taken builder.append (Integer. toString ((bytes [I] & 0xFF) + 0x100, 16) .substring (. 1 )); } return builder.toString (); } / ** * converted into a hexadecimal character byte [] array. Contrary to byte2Hex function. * @Param String 16 hexadecimal string * @return byte [] array * / public static byte [] hex2Byte (String String) { IF (String == null || String.length () <. 1 ) { return null ; } //Because generating a two byte character, a length corresponding to 1: 2, the byte [] array length is half the length of the string byte [] bytes = new new byte [String.length () / 2 ]; // traverse byte [] array, half the length of the string is the number of traverse for ( int I = 0; I <String.length () / 2; I ++ ) { // taken not before a two-character, which is converted int, int High = the Integer.parseInt (String.substring (I * 2, * 2 + I. 1), 16 ); // after a two characters not taken, which is converted int, int Low = the Integer.parseInt (String.substring (I * 2 . 1 +, 2 + I 2 *), 16 ); // high values corresponding to the character * 16 + int int value low, byte values can be transformed into strong @As dd, low high 13 * 16 + 13 = 221 (strong byte binary 11011101 converted into corresponding decimal -35) bytes [I] = ( byte ) (* 16 + High Low); } return bytes; }