求助一个AES算法问题;急

程序漫步 2015-05-08 11:08:26
用C# 翻译 以下 pyhon代码的 AES算法逻辑;


采用 ECB/NoPadding





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
...全文
73 点赞 收藏 3
写回复
3 条回复
Poopaye 2015年05月08日
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();
	}
}
回复 点赞
娃都会打酱油了 2015年05月08日
还没玩pyhon……
回复 点赞
江南小鱼 2015年05月08日
没玩过pyhon,帮顶吧~
回复 点赞
发动态
发帖子
C#
创建于2007-09-28

8.5w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告