JAVA decryption RSA algorithm JS encryption example details

  • 2020-06-23 00:22:14
  • OfStack

JAVA decryption RSA algorithm JS encryption example details

There is such a demand, the front-end login user name password, password must be encrypted, but can not use MD5, because the background to detect the complexity of the password, so under the premise of security to send the password to the background, the answer is to use RSA asymmetric encryption algorithm to solve.

java code

You need to rely on the ES13en-ES14en package

RSACoder.Java


import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lake on 17-4-12.
 */
public class RSACoder {
  public static final String KEY_ALGORITHM = "RSA";
  public static final String SIGNATURE_ALGORITHM = "MD5withRSA";

  private static final String PUBLIC_KEY = "RSAPublicKey";
  private static final String PRIVATE_KEY = "RSAPrivateKey";

  public static byte[] decryptBASE64(String key) {
    return Base64.decodeBase64(key);
  }

  public static String encryptBASE64(byte[] bytes) {
    return Base64.encodeBase64String(bytes);
  }

  /**
   *  Generates a digital signature with the private key pair information 
   *
   * @param data     Encrypt the data 
   * @param privateKey  The private key 
   * @return
   * @throws Exception
   */
  public static String sign(byte[] data, String privateKey) throws Exception {
    //  Decryption by base64 The private key of the encoding 
    byte[] keyBytes = decryptBASE64(privateKey);
    //  structure PKCS8EncodedKeySpec object 
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    // KEY_ALGORITHM  The specified encryption algorithm 
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    //  Retrieve private key object 
    PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
    //  Generates a digital signature with the private key pair information 
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initSign(priKey);
    signature.update(data);
    return encryptBASE64(signature.sign());
  }

  /**
   *  Verify digital signature 
   *
   * @param data    Encrypt the data 
   * @param publicKey  The public key 
   * @param sign    A digital signature 
   * @return  Check successful return true  Failure to return false
   * @throws Exception
   */
  public static boolean verify(byte[] data, String publicKey, String sign)
      throws Exception {
    //  Decryption by base64 Encoded public key 
    byte[] keyBytes = decryptBASE64(publicKey);
    //  structure X509EncodedKeySpec object 
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
    // KEY_ALGORITHM  The specified encryption algorithm 
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    //  Retrieve the public key object 
    PublicKey pubKey = keyFactory.generatePublic(keySpec);
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initVerify(pubKey);
    signature.update(data);
    //  Verify that the signature is healthy 
    return signature.verify(decryptBASE64(sign));
  }

  public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception{
    //  Decrypt the key 
    byte[] keyBytes = decryptBASE64(key);
    //  Get the private key 
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
    //  Decryption of data 
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    return cipher.doFinal(data);
  }

  /**
   *  decryption <br>
   *  Decrypt with private key 
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] decryptByPrivateKey(String data, String key)
      throws Exception {
    return decryptByPrivateKey(decryptBASE64(data),key);
  }

  /**
   *  decryption <br>
   *  Decryption with public key 
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] decryptByPublicKey(byte[] data, String key)
      throws Exception {
    //  Decrypt the key 
    byte[] keyBytes = decryptBASE64(key);
    //  In public key 
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key publicKey = keyFactory.generatePublic(x509KeySpec);
    //  Decryption of data 
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
    return cipher.doFinal(data);
  }

  /**
   *  encryption <br>
   *  Encrypt with public key 
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] encryptByPublicKey(String data, String key)
      throws Exception {
    //  Decryption of public key 
    byte[] keyBytes = decryptBASE64(key);
    //  In public key 
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key publicKey = keyFactory.generatePublic(x509KeySpec);
    //  Encrypt data 
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    return cipher.doFinal(data.getBytes());
  }

  /**
   *  encryption <br>
   *  Encrypt with private key 
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] encryptByPrivateKey(byte[] data, String key)
      throws Exception {
    //  Decrypt the key 
    byte[] keyBytes = decryptBASE64(key);
    //  Get the private key 
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
    //  Encrypt data 
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    return cipher.doFinal(data);
  }

  /**
   *  Get the private key 
   *
   * @param keyMap
   * @return
   * @throws Exception
   */
  public static String getPrivateKey(Map<String, Key> keyMap)
      throws Exception {
    Key key = (Key) keyMap.get(PRIVATE_KEY);
    return encryptBASE64(key.getEncoded());
  }

  /**
   *  In public key 
   *
   * @param keyMap
   * @return
   * @throws Exception
   */
  public static String getPublicKey(Map<String, Key> keyMap)
      throws Exception {
    Key key = keyMap.get(PUBLIC_KEY);
    return encryptBASE64(key.getEncoded());
  }

  /**
   *  Initializing key 
   *
   * @return
   * @throws Exception
   */
  public static Map<String, Key> initKey() throws Exception {
    KeyPairGenerator keyPairGen = KeyPairGenerator
        .getInstance(KEY_ALGORITHM);
    keyPairGen.initialize(1024);
    KeyPair keyPair = keyPairGen.generateKeyPair();
    Map<String, Key> keyMap = new HashMap(2);
    keyMap.put(PUBLIC_KEY, keyPair.getPublic());//  The public key 
    keyMap.put(PRIVATE_KEY, keyPair.getPrivate());//  The private key 
    return keyMap;
  }
}

The test class

RSACoderTest.java


import org.junit.Before;
import org.junit.Test;

import java.security.Key;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by lake on 17-4-12.
 */
public class RSACoderTest {
  private String publicKey;
  private String privateKey;

  @Before
  public void setUp() throws Exception {
    Map<String, Key> keyMap = RSACoder.initKey();
    publicKey = RSACoder.getPublicKey(keyMap);
    privateKey = RSACoder.getPrivateKey(keyMap);
    System.err.println(" The public key : \n\r" + publicKey);
    System.err.println(" The private key:  \n\r" + privateKey);
  }

  @Test
  public void test() throws Exception {
    System.err.println(" Public key encryption - Private key decryption ");
    String inputStr = "abc";
    byte[] encodedData = RSACoder.encryptByPublicKey(inputStr, publicKey);
    byte[] decodedData = RSACoder.decryptByPrivateKey(encodedData,
        privateKey);
    String outputStr = new String(decodedData);
    System.err.println(" Before the encryption : " + inputStr + "\n\r" + " decrypted : " + outputStr);
    assertEquals(inputStr, outputStr);
  }

  @Test
  public void testSign() throws Exception {
    System.err.println(" Private key encryption -- public key decryption ");
    String inputStr = "sign";
    byte[] data = inputStr.getBytes();
    byte[] encodedData = RSACoder.encryptByPrivateKey(data, privateKey);
    byte[] decodedData = RSACoder.decryptByPublicKey(encodedData, publicKey);
    String outputStr = new String(decodedData);
    System.err.println(" Before the encryption : " + inputStr + "\n\r" + " decrypted : " + outputStr);
    assertEquals(inputStr, outputStr);
    System.err.println(" Private key signature - The public key validates the signature ");
    //  Generate the signature 
    String sign = RSACoder.sign(encodedData, privateKey);
    System.err.println(" The signature :" + sign);
    //  Verify the signature 
    boolean status = RSACoder.verify(encodedData, publicKey, sign);
    System.err.println(" state :" + status);
    assertTrue(status);
  }
}

The front-end code

Rely on the jsencrypt project


<script src="bin/jsencrypt.min.js"></script>
<script type="text/javascript">
  var encrypt = new JSEncrypt();
  encrypt.setPublicKey('java Generated public key ');
  var encrypted = encrypt.encrypt(' Encrypted string ');
</script>

instructions

The encrypted string encrypted is generated in the front end and sent to the background. java can be decrypted using the private key.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: