110,538
社区成员
发帖
与我相关
我的任务
分享
class X5AES():
# the block size for the cipher object; must be 16, 24, or 32 for AES
BLOCK_SIZE = 16
# the character used for padding--with a block cipher such as AES, the value
# you encrypt must be a multiple of BLOCK_SIZE in length. This character is
# used to ensure that your value is always a multiple of BLOCK_SIZE
PADDING = '\x00'
# one-liner to sufficiently pad the text to be encrypted
@staticmethod
def pad(s):
return s + (X5AES.BLOCK_SIZE - len(s) % X5AES.BLOCK_SIZE) * X5AES.PADDING
# create a cipher object using x5 appkey
cipher = AES.new(X5_TRANSFER_KEY)
# one-liners to encrypt/encode and decrypt/decode a string
# encrypt with AES, encode with base64
@staticmethod
def encode(s):
r = X5AES.cipher.encrypt(X5AES.pad(s))
return r
using System;
using System.IO;
using System.Security.Cryptography;
static class X5AES
{
static byte[] X5_TRANSFER_KEY;
static RijndaelManaged cipher;
static X5AES()
{
cipher = new RijndaelManaged();
cipher.Padding = PaddingMode.Zeros;
cipher.Mode = CipherMode.ECB;
cipher.Key = X5_TRANSFER_KEY;
}
public static byte[] encode(byte[] s)
{
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, cipher.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(s, 0, s.Length);
cs.Close();
return ms.ToArray();
}
}