**

首先是koa2上

**

使用的是crypto-js

const CryptoJS = require("crypto-js");
function AESEncrypt(value,key,slot) {
    if(isEmpty(key)){
        key = AESKey
    }
    if(!isEmpty(slot)){
        key = key + slot
    }
    key = CryptoJS.enc.Utf8.parse(key)
    let iv = key
    return CryptoJS.AES.encrypt(value, key,{
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    }).toString();
}

function AESDecrypt(value,key,slot) {
    if(isEmpty(key)){
        key = AESKey
    }
    if(!isEmpty(slot)){
        key = key + slot
    }
    key = CryptoJS.enc.Utf8.parse(key)
    let iv = key
    return CryptoJS.AES.decrypt(value, key,{
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    }).toString(CryptoJS.enc.Utf8);
}

然后android上
只写了 解密的 加密的

 public static String decryptAES(String data, String pass) throws Exception {
     Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
     SecretKeySpec keyspec = new SecretKeySpec(pass.getBytes(), "AES");
     IvParameterSpec ivspec = new IvParameterSpec(pass.getBytes());
     cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
     byte[] result=cipher.doFinal(Base64.decode(data.getBytes(),Base64.DEFAULT));
     return new String(result, "utf-8");
 }

 private static String encryptAES(String data,String pass) throws Exception {
     byte[] raw = pass.getBytes("utf-8");
     SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
     Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
     IvParameterSpec iv = new IvParameterSpec(pass.getBytes());
     cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
     byte[] encrypted = cipher.doFinal(data.getBytes());
     return Base64.encodeToString(encrypted,Base64.DEFAULT);
 }

以上方法均已验证,请放心使用

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐