V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
Janxun
V2EX  ›  JavaScript

有无大佬帮忙把这段 Python 代码转换成 JavaScript

  •  
  •   Janxun · 2021-03-04 18:31:22 +08:00 · 2204 次点击
    这是一个创建于 1141 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下,非常感觉!

    import base64
    from Crypto.Cipher import AES
    
    class Encrypt(object):
        def __init__(self):
            self.key = "ABCDEXGH01234567".encode("utf8")
            self.length = AES.block_size
            self.aes = AES.new(self.key, AES.MODE_ECB)
    
        def pad(self, text):
            count = len(text.encode('utf-8'))
            add = self.length - (count % self.length)
            return text + (chr(add) * add)
    
        def encrypt(self, encrData):
            res = self.aes.encrypt(self.pad(encrData).encode("utf8"))
            msg = str(base64.b64encode(res), encoding="utf8")
            return msg
    
    if __name__ == '__main__':
        e = Encrypt()
        a = e.encrypt("12345678")
        print(a)
    
    第 1 条附言  ·  2021-03-04 19:29:19 +08:00

    已完成,大家费心了!

    const CryptoJS = require('crypto-js');
    
    function cryptoEncrypt(word) {
        const key = CryptoJS.enc.Utf8.parse("ABCDEXGH01234567");
        const encrypted = CryptoJS.AES.encrypt(word, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
    }
    
    10 条回复    2021-03-05 10:44:02 +08:00
    tulongtou
        1
    tulongtou  
       2021-03-04 18:38:57 +08:00 via iPhone
    你的工作为啥想让别人免费给你做
    kaikai5601
        2
    kaikai5601  
       2021-03-04 18:42:20 +08:00
    你的工作为啥想让别人免费给你做
    FaiChou
        3
    FaiChou  
       2021-03-04 18:42:53 +08:00
    代码如下,非常感觉!
    kingfalse
        4
    kingfalse  
       2021-03-04 18:45:34 +08:00
    一个简单的加密.自己百度一下就能搞定
    SilverLink
        5
    SilverLink  
       2021-03-04 18:49:01 +08:00
    解一个 AES 加密,还原 base64 编码
    Janxun
        6
    Janxun  
    OP
       2021-03-04 18:52:37 +08:00
    @kingfalse 好吧,我再试试
    darksword21
        7
    darksword21  
       2021-03-04 19:11:11 +08:00 via iPhone
    console.log(“import base64
    from Crypto.Cipher import AES

    class Encrypt(object):
    def __init__(self):
    self.key = "ABCDEXGH01234567".encode("utf8")
    self.length = AES.block_size
    self.aes = AES.new(self.key, AES.MODE_ECB)

    def pad(self, text):
    count = len(text.encode('utf-8'))
    add = self.length - (count % self.length)
    return text + (chr(add) * add)

    def encrypt(self, encrData):
    res = self.aes.encrypt(self.pad(encrData).encode("utf8"))
    msg = str(base64.b64encode(res), encoding="utf8")
    return msg

    if __name__ == '__main__':
    e = Encrypt()
    a = e.encrypt("12345678")
    print(a)”)
    Janxun
        8
    Janxun  
    OP
       2021-03-04 19:27:47 +08:00
    ```javascript
    const CryptoJS = require('crypto-js');

    function cryptoEncrypt(word) {
    const key = CryptoJS.enc.Utf8.parse("ABCDEXGH01234567");
    const encrypted = CryptoJS.AES.encrypt(word, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
    })
    return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
    }
    ```
    solopython
        9
    solopython  
       2021-03-05 10:05:40 +08:00
    人家就是问个技术问题,#1#2 你们可以选择不回答,没必要还挖苦人家,我看国外的技术论坛从来就没有像你们这样的人,悲哀
    acmore
        10
    acmore  
       2021-03-05 10:44:02 +08:00
    @solopython 问技术问题的方法是:用 JS 的什么库可以完成 AES 加密,或者顶多再问下 Padding 的细节,而不是 “我有段代码不会翻译,师爷你给翻译翻译什么叫 AES”。后者在国外也会被群嘲的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5147 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:14 · PVG 16:14 · LAX 01:14 · JFK 04:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.