Esempio n. 1
0
        public static string RSAEncryptLong(string XmlPublicKey, string PlainText)
        {
            byte[] publicInfoByte            = Convert.FromBase64String(XmlPublicKey);
            AsymmetricKeyParameter publicKey = PublicKeyFactory.CreateKey(publicInfoByte);
            var decryptEngine = new OaepEncoding(new RsaEngine());

            decryptEngine.Init(true, publicKey);

            byte[] bytesToDecrypt = Encoding.Default.GetBytes(PlainText);

            int MaxPlainTextLength = Common.GetOEAPFixedMaxPlainTextLength(decryptEngine.GetOutputBlockSize());

            byte[] decrypted = null;

            int Times = (bytesToDecrypt.Length - 1) / MaxPlainTextLength + 1;

            List <byte> encryptedCache = new List <byte>();
            int         Counter        = 0;

            while (Counter < Times)
            {
                int    RemainLength   = bytesToDecrypt.Length - MaxPlainTextLength * Counter;
                int    Length         = MaxPlainTextLength > RemainLength ? RemainLength : MaxPlainTextLength;
                byte[] decryptedPatch = decryptEngine.ProcessBlock(bytesToDecrypt, MaxPlainTextLength * Counter, Length);
                encryptedCache.AddRange(decryptedPatch);

                Counter++;
            }

            decrypted = encryptedCache.ToArray();

            return(Convert.ToBase64String(decrypted));
        }