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

2021-03-04 18:31:22 +08:00
 Janxun

代码如下,非常感觉!

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)
2220 次点击
所在节点    JavaScript
10 条回复
tulongtou
2021-03-04 18:38:57 +08:00
你的工作为啥想让别人免费给你做
kaikai5601
2021-03-04 18:42:20 +08:00
你的工作为啥想让别人免费给你做
FaiChou
2021-03-04 18:42:53 +08:00
代码如下,非常感觉!
kingfalse
2021-03-04 18:45:34 +08:00
一个简单的加密.自己百度一下就能搞定
SilverLink
2021-03-04 18:49:01 +08:00
解一个 AES 加密,还原 base64 编码
Janxun
2021-03-04 18:52:37 +08:00
@kingfalse 好吧,我再试试
darksword21
2021-03-04 19:11:11 +08:00
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
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
2021-03-05 10:05:40 +08:00
人家就是问个技术问题,#1#2 你们可以选择不回答,没必要还挖苦人家,我看国外的技术论坛从来就没有像你们这样的人,悲哀
acmore
2021-03-05 10:44:02 +08:00
@solopython 问技术问题的方法是:用 JS 的什么库可以完成 AES 加密,或者顶多再问下 Padding 的细节,而不是 “我有段代码不会翻译,师爷你给翻译翻译什么叫 AES”。后者在国外也会被群嘲的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/758543

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX