亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JAVA 中解密RSA算法JS加密實(shí)例詳解

 更新時(shí)間:2017年04月14日 08:39:10   投稿:lqh  
這篇文章主要介紹了JAVA 中解密RSA算法JS加密 的相關(guān)資料,需要的朋友可以參考下

JAVA 中解密RSA算法JS加密實(shí)例詳解

有這樣一個(gè)需求,前端登錄的用戶(hù)名密碼,密碼必需加密,但不可使用MD5,因?yàn)楹笈_(tái)要檢測(cè)密碼的復(fù)雜度,那么在保證安全的前提下將密碼傳到后臺(tái)呢,答案就是使用RSA非對(duì)稱(chēng)加密算法解決 。

java代碼

需要依賴(lài) commons-codec 包

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);
  }

  /**
   * 用私鑰對(duì)信息生成數(shù)字簽名
   *
   * @param data    加密數(shù)據(jù)
   * @param privateKey 私鑰
   * @return
   * @throws Exception
   */
  public static String sign(byte[] data, String privateKey) throws Exception {
    // 解密由base64編碼的私鑰
    byte[] keyBytes = decryptBASE64(privateKey);
    // 構(gòu)造PKCS8EncodedKeySpec對(duì)象
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    // KEY_ALGORITHM 指定的加密算法
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    // 取私鑰匙對(duì)象
    PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
    // 用私鑰對(duì)信息生成數(shù)字簽名
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initSign(priKey);
    signature.update(data);
    return encryptBASE64(signature.sign());
  }

  /**
   * 校驗(yàn)數(shù)字簽名
   *
   * @param data   加密數(shù)據(jù)
   * @param publicKey 公鑰
   * @param sign   數(shù)字簽名
   * @return 校驗(yàn)成功返回true 失敗返回false
   * @throws Exception
   */
  public static boolean verify(byte[] data, String publicKey, String sign)
      throws Exception {
    // 解密由base64編碼的公鑰
    byte[] keyBytes = decryptBASE64(publicKey);
    // 構(gòu)造X509EncodedKeySpec對(duì)象
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
    // KEY_ALGORITHM 指定的加密算法
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    // 取公鑰匙對(duì)象
    PublicKey pubKey = keyFactory.generatePublic(keySpec);
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initVerify(pubKey);
    signature.update(data);
    // 驗(yàn)證簽名是否正常
    return signature.verify(decryptBASE64(sign));
  }

  public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception{
    // 對(duì)密鑰解密
    byte[] keyBytes = decryptBASE64(key);
    // 取得私鑰
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
    // 對(duì)數(shù)據(jù)解密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    return cipher.doFinal(data);
  }

  /**
   * 解密<br>
   * 用私鑰解密
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] decryptByPrivateKey(String data, String key)
      throws Exception {
    return decryptByPrivateKey(decryptBASE64(data),key);
  }

  /**
   * 解密<br>
   * 用公鑰解密
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] decryptByPublicKey(byte[] data, String key)
      throws Exception {
    // 對(duì)密鑰解密
    byte[] keyBytes = decryptBASE64(key);
    // 取得公鑰
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key publicKey = keyFactory.generatePublic(x509KeySpec);
    // 對(duì)數(shù)據(jù)解密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
    return cipher.doFinal(data);
  }

  /**
   * 加密<br>
   * 用公鑰加密
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] encryptByPublicKey(String data, String key)
      throws Exception {
    // 對(duì)公鑰解密
    byte[] keyBytes = decryptBASE64(key);
    // 取得公鑰
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key publicKey = keyFactory.generatePublic(x509KeySpec);
    // 對(duì)數(shù)據(jù)加密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    return cipher.doFinal(data.getBytes());
  }

  /**
   * 加密<br>
   * 用私鑰加密
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   */
  public static byte[] encryptByPrivateKey(byte[] data, String key)
      throws Exception {
    // 對(duì)密鑰解密
    byte[] keyBytes = decryptBASE64(key);
    // 取得私鑰
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
    // 對(duì)數(shù)據(jù)加密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    return cipher.doFinal(data);
  }

  /**
   * 取得私鑰
   *
   * @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());
  }

  /**
   * 取得公鑰
   *
   * @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());
  }

  /**
   * 初始化密鑰
   *
   * @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());// 公鑰
    keyMap.put(PRIVATE_KEY, keyPair.getPrivate());// 私鑰
    return keyMap;
  }
}

測(cè)試類(lèi)

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("公鑰: \n\r" + publicKey);
    System.err.println("私鑰: \n\r" + privateKey);
  }

  @Test
  public void test() throws Exception {
    System.err.println("公鑰加密——私鑰解密");
    String inputStr = "abc";
    byte[] encodedData = RSACoder.encryptByPublicKey(inputStr, publicKey);
    byte[] decodedData = RSACoder.decryptByPrivateKey(encodedData,
        privateKey);
    String outputStr = new String(decodedData);
    System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
    assertEquals(inputStr, outputStr);
  }

  @Test
  public void testSign() throws Exception {
    System.err.println("私鑰加密——公鑰解密");
    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("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
    assertEquals(inputStr, outputStr);
    System.err.println("私鑰簽名——公鑰驗(yàn)證簽名");
    // 產(chǎn)生簽名
    String sign = RSACoder.sign(encodedData, privateKey);
    System.err.println("簽名:" + sign);
    // 驗(yàn)證簽名
    boolean status = RSACoder.verify(encodedData, publicKey, sign);
    System.err.println("狀態(tài):" + status);
    assertTrue(status);
  }
}

前端代碼

依賴(lài) jsencrypt 項(xiàng)目

<script src="bin/jsencrypt.min.js"></script>
<script type="text/javascript">
  var encrypt = new JSEncrypt();
  encrypt.setPublicKey('java生成的公鑰');
  var encrypted = encrypt.encrypt('加密的字符串');
</script>

說(shuō)明

前端生成加密的字符串encrypted,傳到后臺(tái),java使用私鑰進(jìn)行解密即可。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 半小時(shí)通透Java的泛型

    半小時(shí)通透Java的泛型

    這篇文章主要給大家介紹了關(guān)于Java中泛型使用的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-09-09
  • 學(xué)習(xí)Java設(shè)計(jì)模式之觀察者模式

    學(xué)習(xí)Java設(shè)計(jì)模式之觀察者模式

    這篇文章主要為大家介紹了Java設(shè)計(jì)模式中的觀察者模式,對(duì)Java設(shè)計(jì)模式感興趣的小伙伴們可以參考一下
    2016-01-01
  • SpringMVC+EasyUI實(shí)現(xiàn)頁(yè)面左側(cè)導(dǎo)航菜單功能

    SpringMVC+EasyUI實(shí)現(xiàn)頁(yè)面左側(cè)導(dǎo)航菜單功能

    這篇文章主要介紹了SpringMVC+EasyUI實(shí)現(xiàn)頁(yè)面左側(cè)導(dǎo)航菜單功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Springboot Druid 自定義加密數(shù)據(jù)庫(kù)密碼的幾種方案

    Springboot Druid 自定義加密數(shù)據(jù)庫(kù)密碼的幾種方案

    這篇文章主要介紹了Springboot Druid 自定義加密數(shù)據(jù)庫(kù)密碼的步驟,幫助大家更好的理解和使用springboot,感興趣的朋友可以了解下
    2020-12-12
  • Java文件管理操作的知識(shí)點(diǎn)整理

    Java文件管理操作的知識(shí)點(diǎn)整理

    這篇文章主要為大家詳細(xì)介紹了Java中文件管理操作的一些知識(shí)點(diǎn)和實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-09-09
  • SpringCloud?客戶(hù)端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法

    SpringCloud?客戶(hù)端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法

    Ribbon 是 Netflix 提供的一個(gè)基于 Http 和 TCP 的客戶(hù)端負(fù)載均衡工具,且已集成在 Eureka 依賴(lài)中,這篇文章主要介紹了SpringCloud?客戶(hù)端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2022-06-06
  • java數(shù)據(jù)結(jié)構(gòu)算法稀疏數(shù)組示例詳解

    java數(shù)據(jù)結(jié)構(gòu)算法稀疏數(shù)組示例詳解

    這篇文章主要為大家介紹了java數(shù)據(jù)結(jié)構(gòu)算法稀疏數(shù)組示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Springboot通用mapper和mybatis-generator代碼示例

    Springboot通用mapper和mybatis-generator代碼示例

    這篇文章主要介紹了Springboot通用mapper和mybatis-generator代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 一文解讀java.nio.ByteBuffer

    一文解讀java.nio.ByteBuffer

    這篇文章主要介紹了java.nio.ByteBuffer的用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • SpringDataMongoDB多文檔事務(wù)的實(shí)現(xiàn)

    SpringDataMongoDB多文檔事務(wù)的實(shí)現(xiàn)

    mongodb4.0也出來(lái)一段時(shí)間了,這個(gè)版本最為大眾期待的特性就是支持了多文檔事務(wù)。這篇文章主要介紹了SpringDataMongoDB多文檔事務(wù)的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2018-11-11

最新評(píng)論