【记录】CryptoJS 使用Base64、AES、RSA

Windows Base64 编码

window.btoa() 方法对源数据进行编码;

window.atob() 方法对其进行解码, 从而得到原数据;

btoa和atob是window对象的两个函数,其中btoa是binary to ASCII,用于将binary的数据用ASCII码表示,即Base64的编码过程,
而atob则是ASCII to binary,用于将ASCII码解析成binary数据,即Base64的解码过程
binary 是JS字符集的另外一个子集,它类似于 ASCII 字符集,但是字符的码点(charCode)不再限制到 127,它包含了255 以内的字符。
binary string设计的目的不是用于代表字符, 而是代表二进制数据。由 binary string 代表的二进制数据大小是原始数据的两倍,
然而这对于最终用户是不可见的, 因为JavaScript strings 的长度是以2字节为单位进行计算的。
比如, "Hello world" 这个字符串属于 ASCII 子集, 而 ÀÈÌÒÙ 不属于ASCII码,但属于binary。
所以btoa和atob其实还涉及了编码问题,我们只需要找出相同编码进行替换即可。
在node.js环境中,提供了一个 Buffer 类,用于操作二进制及Base64转码。
// atob
var s = new Buffer.from("待解码的字符", "base64").toString("binary")
// btoa
var s = new Buffer.from("待编码的字符", "binary").toString("base64")

比如:

// 编码
function utf8_to_b64(str) {
    return window.btoa(unescape(encodeURIComponent(str)));
}

// 解码
function b64_to_utf8(str) {
    return decodeURIComponent(escape(window.atob(str)));
}

// Usage:
utf8_to_b64('✓ à la mode'); // 4pyTIMOgIGxhIG1vZGU=
b64_to_utf8('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"

utf8_to_b64('I \u2661 Unicode!'); // SSDimaEgVW5pY29kZSE=
b64_to_utf8('SSDimaEgVW5pY29kZSE='); // "I ♡ Unicode!"

utf8_to_b64('我爱中国'); // 5oiR54ix5Lit5Zu9
b64_to_utf8('SSDimaEgVW5pY29kZSE='); // "我爱中国"

utf8_to_b64(123456); // MTIzNDU2
b64_to_utf8("MTIzNDU2"); // 123456

CryptoJS 使用Base64加解密:

//base64加密
function base64_encode(code){
    var str = CryptoJS.enc.Utf8.parse(code);
    return CryptoJS.enc.Base64.stringify(str);
}
//base64解密
function base64_decode(code){
    var words = CryptoJS.enc.Base64.parse(code);
    return words.toString(CryptoJS.enc.Utf8)
}

CryptoJS 使用 AES 加解密:

AES加密:

import CryptoJS from "crypto-js";

const key = CryptoJS.enc.Utf8.parse("123456789xxxxxxx"); //16位 密钥
const iv = CryptoJS.enc.Utf8.parse("987654321123456789"); // 初始向量

export default {
  //aes加密
  encrypt(word) {
    let encrypted = "";
    if (typeof word == "string") {
      const srcs = CryptoJS.enc.Utf8.parse(word);
      encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      });
    } else if (typeof word == "object") {
      //对象格式的转成json字符串
      const data = JSON.stringify(word);
      const srcs = CryptoJS.enc.Utf8.parse(data);
      encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,  //加密方式
        padding: CryptoJS.pad.Pkcs7  // 填充方式
      });
    }
    return encrypted.ciphertext.toString();
  },
};

AES解密:

decrypt(word) {
    //word 密文
    const encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    const decrypt = CryptoJS.AES.decrypt(srcs, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    });
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
  }

RSA:

// pip install pycryptodome

import base64

from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.PublicKey import RSA


public_key = """
-----BEGIN PUBLIC KEY-----
Your PUBLIC KEY
-----END PUBLIC KEY-----
"""


def make_message(pwd):
    rsakey = RSA.importKey(public_key)
    cipher = Cipher_pkcs1_v1_5.new(rsakey)
    cipher_text = base64.b64encode(cipher.encrypt(pwd.encode(encoding="utf-8")))
    return cipher_text.decode('utf8')


if __name__ == '__main__':
    print(make_message('hello'))

➣➣欢迎关注本人微信公众号Anonymous NoteBook➣➣

分享好玩有趣的网络资源&软件工具~

分享各种技术性PDF资源/付费查询PDF资源~

分享各种编程语言、开发技术、分布式事务、大数据与云计算技术~

分享有关网络爬虫(Web爬虫逆向 | 安卓逆向)案例、数据挖掘、技术心得等~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值