前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >node启动js-3DES-ECB加密,python-3DES-ECB加密

node启动js-3DES-ECB加密,python-3DES-ECB加密

作者头像
小小咸鱼YwY
发布2020-07-02 15:52:43
1.1K0
发布2020-07-02 15:52:43
举报
文章被收录于专栏:python-爬虫

一.node启动js-3DES-ECB加密

代码语言:javascript
复制
var arguments = process.argv.splice(2);
// console.log('所传递的参数是:', arguments);

var password = arguments[0];   //加密的password
var t=  arguments[1];   //加密的txt
var forge = require('node-forge');
// var fs = require('fs'); 写文件
// console.log(t);
var n = forge.cipher.createCipher("3DES-ECB",password );
n.start();
n.update(forge.util.createBuffer(forge.util.encodeUtf8(t)));
n.finish();
var data = forge.util.encode64(n.output.getBytes()).toString();
console.log(data);
// fs.writeFile(name, data, function (error) {});

二.python-3DES-ECB加密

代码语言:javascript
复制
from Crypto.Cipher import DES3
import base64
import json
BS = DES3.block_size


def pad(s):
    return s + (BS - len(s) % BS) * chr(BS - len(s) % BS)


def unpad(s):
    return s[0:-ord(s[-1])]


class prpcrypt():
    def __init__(self, key):
        self.key = key
        self.mode = DES3.MODE_ECB

    def encrypt(self, text):
        text = pad(text)
        cryptor = DES3.new(self.key, self.mode)
        x = len(text) % 8
        if x != 0:
            text = text + '\0' * (8 - x)
        text=text.encode("utf-8")
        self.ciphertext = cryptor.encrypt(text)
        return base64.standard_b64encode(self.ciphertext).decode("utf-8")

    def decrypt(self, text):
        cryptor = DES3.new(self.key, self.mode)
        de_text = base64.standard_b64decode(text)
        plain_text = cryptor.decrypt(de_text)
        st = str(plain_text.decode("utf-8")).rstrip('\0')
        out = unpad(st)
        return out

code = json.dumps(text)  //加密的内容
key = 'Q5yuGAZ2hHg1jhuRWjhc39oM'  //盐
print(prpcrypt(key).encrypt(code))
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-07-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.node启动js-3DES-ECB加密
  • 二.python-3DES-ECB加密
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档