您的位置:首页 > 编程语言 > C#

[winphone][C#] RSA加密与解密

2013-11-14 14:28 513 查看
RSA加密(分片加密,每片最大长度117)

const int RSA_PIECE_LENGTH = 117;//xml格式的密钥
public static byte[] RSAEncypt(byte[] contentBytes, string key)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(key);
byte[] oText = contentBytes;
List<byte[]> pieceList = new List<byte[]>();
List<byte> oTextList = new List<byte>();
oTextList.AddRange(oText);
int pieces = oText.Length / RSA_PIECE_LENGTH + 1;
//分片
for (int i = 0; i < pieces; i++)
{
int index = i * RSA_PIECE_LENGTH;
byte[] bytes = oTextList.GetRange(index, index + RSA_PIECE_LENGTH > oTextList.Count ? oTextList.Count - index : RSA_PIECE_LENGTH).ToArray();
pieceList.Add(bytes);
}
List<byte> cipherList = new List<byte>();
for (int i = 0; i < pieceList.Count; i++)
{
byte[] cbs = rsa.Encrypt(pieceList[i], false);
cipherList.AddRange(cbs);
}
rsa.Dispose();
return cipherList.ToArray();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  winphone C# RSA 分片