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

来源:互联网 发布:怎样设计淘宝店铺 编辑:程序博客网 时间:2024/06/05 04:32

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();        }


原创粉丝点击