用了你的ase加密解密,其他语言怎么配合加解密?
hut140 opened this issue · comments
huteng commented
请问,如何用java的方法来实现AESEncrypt里的编解码方法呢?
Bruce commented
你在这里测试online cryptaes可以配置如下加密参数:
- AES加密模式: ECB
- 填充: PKCS5Padding
- 数据块:128位
- 密码: 1234567890abcdef
- 偏移量: 空
- 字符集: utf8
测试加解密结果应该跟我的一致。其他语言请自行配置如上的加密参数。
不过,我希望的是每个平台都使用这么一套加密代码而不是使用同一套加密算法。
我推荐你参考: python配合这套AES
ChenLi commented
提供一份python版的配套作者的加解密方法,如下
#! /usr/bin/env python
# -*- coding: utf-8 -*
import base64
import re
from Crypto.Cipher import AES
# pip install pycryptodome 注pycrypto经久不休 pycryptodome可能在linux下不能使用
class Ice:
def __init__(self, key='1234567890abcdef'):
self.key = key.encode('utf-8')
self.mode = AES.MODE_ECB # ECB模式
self.bs = 16 # block size
self.HEX = ['\x10', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0a', '\x0b',
'\x0c', '\x0d', '\x0e', '\x0f'] # 缺几就填充几个几
def add_padding(self, text):
if len(text.encode('utf-8')) % self.bs:
add = self.bs - (len(text.encode('utf-8')) % self.bs)
else:
add = 0
text = text + (self.HEX[add] * add)
return text.encode('utf-8')
def encrypt(self, text):
generator = AES.new(self.key, self.mode) # ECB模式无需向量iv
try:
crypt = generator.encrypt(self.add_padding(text))
crypted_str = base64.b64encode(crypt)
result = crypted_str.decode()
except Exception:
result = '加密失败,请确认输入是否有误!'
return result
def decrypt(self, text):
generator = AES.new(self.key, self.mode) # ECB模式无需向量iv
text += (len(text) % 4) * '='
try:
decrpyt_bytes = base64.b64decode(text)
meg = generator.decrypt(decrpyt_bytes)
# 去除解码后的非法字符
result = re.compile('[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f\n\r\t]').sub('', meg.decode())
except Exception:
result = 'null'
return result
def create(self):
encode = base64.b64encode(self.key)
return 'N' + encode.decode()
if __name__ == '__main__':
aes = Ice()
print(aes.create())
print(aes.encrypt('123abcABC*%!~#+_/中文测试'))
print(aes.decrypt('SkiDk/JC5F/BXKf/np7rWNub7ibxzYMjKwkQ7A6AqPw='))
Bruce commented
@SmileChen007 thank a lot ! i will add your github page to contributor in README.md.
ChenLi commented
@SmileChen007 thank a lot ! i will add your github page to contributor in README.md.
不用谢,毕竟我也在Android用了你的aes项目