16: vue + crypto-js + python前后端加密解密

1.1 vue中使用crypto-js进行AES加密解密

     参考博客:https://www.cnblogs.com/qixidi/p/10137935.html

  1、初始化vue项目

      vue init webpack itany

      cd itany

      cnpm install

      npm install crypto-js    # 安装rypto-js

  2、使用

<template>
  <div id="app">
    <p @click="handleLogin">点击发送axiso请求</p>


  </div>
</template>

<script>
  import { mapActions } from 'vuex'
  import CryptoJS from "crypto-js";
  export default {
    name: 'App',
    methods: {
      handleLogin () {

        // 1、Encrypt 加密
        var cipherText = CryptoJS.AES.encrypt(
          "my message",
          "secretkey123"
        ).toString();
        console.log(cipherText)  // U2FsdGVkX1/Hjuv9Mt10c9Q/98PjhFSNXkmtz+sVaPU=

        // 2、Decrypt 解密
        var bytes = CryptoJS.AES.decrypt(cipherText, "secretkey123");
        var originalText = bytes.toString(CryptoJS.enc.Utf8);
        console.log(originalText, typeof originalText); // 'my message'

        // 3、解析json格式
        // var pjson =  JSON.parse(originalText)
      }
    },
  }
</script>

<style>

</style>
App.vue

      

1.2 python中进行AES加密解密模块

  1、安装与使用

      pip uninstall crypto pycryptodome

      pip install pycryptodome
# -*- coding:UTF-8 -*-
from Crypto import Random
from Crypto.Cipher import AES
import base64
from hashlib import md5


class EncDecAES(object):
    def pad(self, data):
        length = 16 - (len(data) % 16)
        return data + (chr(length)*length).encode()

    def unpad(self, data):
        return data[:-(data[-1] if type(data[-1]) == int else ord(data[-1]))]

    def bytes_to_key(self, data, salt, output=48):
        assert len(salt) == 8, len(salt)
        data += salt
        key = md5(data).digest()
        final_key = key
        while len(final_key) < output:
            key = md5(key + data).digest()
            final_key += key
        return final_key[:output]

    def encrypt(self, message, passphrase):
        salt = Random.new().read(8)
        key_iv = self.bytes_to_key(passphrase, salt, 32 + 16)
        key = key_iv[:32]
        iv = key_iv[32:]
        aes = AES.new(key, AES.MODE_CBC, iv)
        return base64.b64encode(b"Salted__" + salt + aes.encrypt(self.pad(message)))


    def decrypt(self, encrypted, passphrase):
        encrypted = base64.b64decode(encrypted)
        assert encrypted[0:8] == b"Salted__"
        salt = encrypted[8:16]
        key_iv = self.bytes_to_key(passphrase, salt, 32 + 16)
        key = key_iv[:32]
        iv = key_iv[32:]
        aes = AES.new(key, AES.MODE_CBC, iv)
        return self.unpad(aes.decrypt(encrypted[16:]))



if '__main__' == __name__:
    data = 'Data that needs to be encrypted!'    # 要加密的数据
    passphrase = 'secretkey123'                  # 加密解密用的秘钥
    
    #1、加密
    encMsg = EncDecAES().encrypt(data, passphrase)
    print encMsg   # U2FsdGVkX1/GaKnTiu4lQ6zuCwHB+SyN9ARgSZXLpJznjY38+cApNYL0qTdgQ3Iv
    
    #2、解密
    decMsg = EncDecAES().decrypt(encMsg, passphrase)
    print decMsg  # Data that needs to be encrypted
crypto_aes.py

 

posted @ 2019-04-26 12:45  不做大哥好多年  阅读(1025)  评论(0编辑  收藏  举报