Let's go

uniapp-Java使用AES加密解密

操作前

uniapp 需要安装  npm install crypto-js

 在需要使用的界面加入 import CryptoJS from 'crypto-js'

 

java端

        <!-- spring security 安全认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- 添加 commons-net 依赖 -->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.8.0</version> <!-- 请检查是否有更新的版本 -->
        </dependency>

 

 

一、ECB 模式

uniapp端

    function decryptData(encryptedData) {
      const SECRET_KEY = CryptoJS.enc.Utf8.parse('mysecretkey12345');
      const decryptedBytes = CryptoJS.AES.decrypt(encryptedData, SECRET_KEY, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
      });
      const decryptedData = decryptedBytes.toString(CryptoJS.enc.Utf8);
      return decryptedData;
    };
    function  encryptData(strData) {
      const SECRET_KEY = CryptoJS.enc.Utf8.parse('mysecretkey12345');
      // 加密
      const encryptedData = CryptoJS.AES.encrypt(strData, SECRET_KEY, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
      });
     return encryptedData;
    };

uniapp端调用

    function jiami(){
        const obj = {
          name: "Alice",
          age: 25,
          hobby: "Reading books"
        };
        var str=JSON.stringify(obj);
        var ss=this.encryptData(str);
        console.log("加密==>"+ss);
    };
    function jiemi(){
        var mv="QmEJivSxPT/qMEfflQ+E4f0MZUKrxnqtcSU6xEcE1EQfBhC+8RLHQoZh33jugqzzMRRLyh5/HiXvboBObVP5P9SCBy/w4iGs0OEKMtGU7lSEXZ3RvzZhG3IV/3l13Eiw4vXio/JlASg7HyNxH04IYv6EQvFzYTSsVgu91VnK1+jPzgrBv/EuGr1AqBR9CFKNY=";
        var ss =this.decryptData(mv);
        console.log("解密==>"+ss);
    };

 

JAVA端

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
import java.util.Base64;

public class AsymmetricEncryption {
    public static KeyPair generateKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        return keyPairGenerator.generateKeyPair();
    }

    public static String encrypt(String text, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encrypted = cipher.doFinal(text.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }
}

Java端调用

    public static void main(String[] args) throws Exception {
        
        //加密
        String key = "mysecretkey12345";
        String json="123456";
        String a= SymmetricEncryption.encrypt(json,key);
        System.out.println("a=>"+a);

        //解密
        String b =SymmetricEncryption.decrypt(a,key);
        System.out.println("b=>"+b);

    }

 

二、CBC模式

uniapp端

    function encryptByAES(data, key, iv) {
        return CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
            iv: CryptoJS.enc.Utf8.parse(iv),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        }).toString();
    };
    function  decryptByAES(data, key, iv) {
        var decrypted = CryptoJS.AES.decrypt(data, CryptoJS.enc.Utf8.parse(key), {
            iv: CryptoJS.enc.Utf8.parse(iv),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return decrypted.toString(CryptoJS.enc.Utf8);
    };

uniapp端调用

   function jiami(){
        const obj = {
          name: "Alice",
          age: 25,
          hobby: "Reading books"
        };
        var str=JSON.stringify(obj);
        var key="1234567890123456";
        var iv="1234567890123456";
        var ss=this.encryptByAES(str,key,iv);
        console.log("加密==>"+ss);
    };
    function jiemi(){
        var key="1234567890123456";
        var iv="1234567890123456";
        var mv="QmEJWUGl9Qwy7P7rlY1YAybFyIbMC585Ow50Pb2wONHmvaTnLC4dz4pm0JNpdnVScKBA4TZYXio/JlASg7HyNxH04IYv6EQvFzYTSsVgu91VnK1+jPzgrBv/EuGr1AqBR9CFKNY=";
        var ss=this.decryptByAES(mv,key,iv);
        console.log("解密==>"+ss);
    };

Java端

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

public class AESCBCExample {
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String SECRET_KEY = "1234567890123456"; // 16 bytes key
    private static final String IV = "1234567890123456"; // 16 bytes IV sail

    public static String encrypt(String data) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encrypted = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String encryptedData) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(original);
    }
}

Java端调用

    public static void main(String[] args) throws Exception {

        //加密
        String json="123456";
        String a=AESCBCExample.encrypt(json);
        System.out.println("a=>"+a);
        
        //解密
        String b=AESCBCExample.decrypt(a);
        System.out.println("b=>"+b);

    }

其他

 

posted @ 2024-07-10 19:11  chenze  阅读(386)  评论(0编辑  收藏  举报
有事您Q我