Java JavaScript AES/CBC/PKCS5Padding 加密解密

Java后端代码

 

package org.example;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws Exception {
        String key = "1234567890123456";
        String iv = "1234567890123456";

        //加密
        String encrypt1 = encrypt1(key, "ABCDEFG");
        System.out.println(encrypt1);

        //解密
        String decrypt1 = decrypt1(key, encrypt1);
        System.out.println(decrypt1);

        //加密
        String encrypt2 = encrypt2(key, iv, "ABCDEFG");
        System.out.println(encrypt2);

        //解密
        String decrypt2 = decrypt2(key, iv, encrypt2);
        System.out.println(decrypt2);
    }

    /**
     * aes+base64 加密
     */
    public static String encrypt1(String keyPara, String plaintext) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec key = new SecretKeySpec(keyPara.getBytes(), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypted = cipher.doFinal(plaintext.getBytes("utf-8"));
        return Base64.getEncoder().encodeToString(encrypted);
    }

    /**
     * aes+base64 解密
     */
    public static String decrypt1(String keyPara, String encryptedData) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec key = new SecretKeySpec(keyPara.getBytes(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(original, "utf-8");
    }

    /**
     * aes+base64 加密
     */
    public static String encrypt2(String keyPara, String ivPara, String plaintext) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec key = new SecretKeySpec(keyPara.getBytes(), "AES");
        IvParameterSpec iv = new IvParameterSpec(ivPara.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
        byte[] encrypted = cipher.doFinal(plaintext.getBytes("utf-8"));
        return Base64.getEncoder().encodeToString(encrypted);
    }

    /**
     * aes+base64 解密
     */
    public static String decrypt2(String keyPara, String ivPara, String encryptedData) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec key = new SecretKeySpec(keyPara.getBytes(), "AES");
        IvParameterSpec iv = new IvParameterSpec(ivPara.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
        byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(original, "utf-8");
    }
}
输出:
McO75Zw0xlgE5HgeNDljtA== ABCDEFG +BTLEaD7AUwCO07+PE0mMw== ABCDEFG

 

前端代码

{
  "name": "testcrypto",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "crypto-js": "^4.1.1"
  },
  "description": ""
}

 

<script type="text/javascript" src="/node_modules/crypto-js/crypto-js.js"></script>

<script>
  var plaintext = "ABCDEFG";

  var key = CryptoJS.enc.Utf8.parse("1234567890123456");
  var iv = CryptoJS.enc.Utf8.parse("1234567890123456");

  document.write("plaintext:");
  document.write("<br>" + plaintext);

  var encryptedData = CryptoJS.AES.encrypt(plaintext, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });

  document.write("<br><br>encrypted:");
  document.write("<br>" + encryptedData);

  var decryptedData = CryptoJS.AES.decrypt(encryptedData, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  }).toString(CryptoJS.enc.Utf8);

  document.write("<br><br>decrypted:");
  document.write("<br>" + decryptedData);

</script>

 

 
posted @ 2022-01-22 10:02  Herenwei_Wayne  阅读(2385)  评论(0编辑  收藏  举报