求助一个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
...全文
201 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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();
	}
}
  • 打赏
  • 举报
回复
还没玩pyhon……
江南小鱼 2015-05-08
  • 打赏
  • 举报
回复
没玩过pyhon,帮顶吧~

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧