Esempio n. 1
0
        public byte[] Encrypt(byte[] M, List <int> S)
        {
            RSAParameters SK, VK;
            Cryptotext    cryptotext = new Cryptotext();
            List <byte[]> cry        = new List <byte[]>();

            using (var RSA = new RSACryptoServiceProvider())
            {
                VK = RSA.ExportParameters(false);
                SK = RSA.ExportParameters(true);
            }
            for (int i = 1; i < S.Count; i++)
            {
                BinaryFormatter bf = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream())
                {
                    bf.Serialize(ms, new TextPerUser(M, VK));
                    using (var RSA = new RSACryptoServiceProvider())
                    {
                        RSA.ImportParameters(MPK[i]);
                        cry[i] = RSA.Encrypt(ms.ToArray(), false);
                    }
                }
            }
            cryptotext.VK = VK;
            cryptotext.Ci = cry;
            using (var RSA = new RSACryptoServiceProvider())
            {
                BinaryFormatter bf = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream())
                {
                    RSA.ImportParameters(SK);
                    bf.Serialize(ms, cry);
                    cryptotext.signedData = RSA.SignData(ms.ToArray(), new SHA1CryptoServiceProvider());
                }
            }

            byte[]          rezult = null;
            BinaryFormatter bf1    = new BinaryFormatter();

            using (MemoryStream ms = new MemoryStream())
            {
                bf1.Serialize(ms, cryptotext);
                rezult = ms.ToArray();
            }
            return(rezult);
        }
Esempio n. 2
0
        public byte[] Decrypt(byte[] C, RSAParameters ski)
        {
            byte[] rezult = null;

            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    ms        = new MemoryStream(C);
            Cryptotext      deserializedCryptotext = (Cryptotext)formatter.Deserialize(ms);

            for (int i = 0; i < deserializedCryptotext.Ci.Count; i++)
            {
                using (var RSA = new RSACryptoServiceProvider())
                {
                    RSA.ImportParameters(ski);
                    byte[] M = RSA.Decrypt(deserializedCryptotext.Ci[i], false);
                }
            }

            return(rezult);
        }