如何使用crypto.js解密图像

时间:2019-06-17 02:18:49

标签: javascript node.js electron cryptojs

我很难用crypto.js AES解密图像。

我需要读取从服务本地下载的加密图像,然后需要对其解密。现在我无法解密图片。但是,如果我只解密加密图像的base64,就可以成功解密。

这是我在电子战中做的一个演示

<template>
    <div>
        <el-button @click="encrypt">加密</el-button>
        <el-button @click="decrypt">解密</el-button>
    </div>
</template>
<script>
import fs from 'fs';
import CryptoJS from 'crypto-js';

export default {
    data() {
        return {
            file: {},
            fileName: '',
            mydata: '',
            beforeBase64: '',
            afterBase64: ''
        };
    },
    methods: {
        async encrypt() {
            const eKey = '123';
            const filedir = '/Users/haoxiaoli/Documents/data/old.jpg';
            const c = fs.readFileSync(filedir);
            const data = new Buffer(c).toString('base64');
            let encrypted = CryptoJS.AES.encrypt(data, eKey).toString();
            this.mydata = encrypted;
            fs.writeFile('haoxl.jpg', encrypted, function (err) {
                if (err) {
                    console.log(err);
                }
                else {
                    console.log('save success');
                }
            });
        },
        decrypt() {
            const eKey = '123';
            const filedir = '/Users/haoxiaoli/Documents/data/haoxl.jpg';
            const c = fs.readFileSync(filedir);
            const data = new Buffer(c).toString('base64');

            // It was successful, but I need to decrypt a picture
            // let base64 = CryptoJS.AES.decrypt(this.mydata, eKey).toString(CryptoJS.enc.Utf8);

            let decrypted = CryptoJS.AES.decrypt(data, eKey);
            let base64 = new Buffer(decrypted.words).toString('base64');

            let img = new Image();
            img.src = 'data:image/jpeg;base64,' + base64;
            img.style.height = '600px';
            img.style.width = '600px';
            document.body.append(img);
        }
    }

};
</script>

我希望能够成功解密加密的图像,请帮助我,谢谢!

0 个答案:

没有答案