コード例 #1
0
        public DataInstance decryptData(DataInstance dataInst)
        {
            int index = dataInst.encryptRounds.Count - 1;

            while (index >= 0 && checkIfCanDecrypt(dataInst.encryptRounds[index]))
            {
                EncryptRound round        = dataInst.encryptRounds[index];
                byte[]       decryptedKey = RSAProvider.Decrypt(round.key, USE_AOEP);

                byte[] plain;
                int    count;
                using (MemoryStream mStream = new MemoryStream(dataInst.data))
                {
                    using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(mStream,
                                                                            aesProvider.CreateDecryptor(decryptedKey, round.IV), CryptoStreamMode.Read))
                        {
                            plain = new byte[dataInst.data.Length];
                            count = cryptoStream.Read(plain, 0, plain.Length);
                        }
                    }
                }
                dataInst.data = new byte[count];
                Array.Copy(plain, dataInst.data, count);

                dataInst.encryptRounds.RemoveAt(index);
                index--;
            }

            return(dataInst);
        }
コード例 #2
0
        public DataInstance?chunkToInstance(DataChunk chunk)
        {
            byte[]       hash      = HashProvider.ComputeHash(chunk.data);
            EncryptRound lastRound = chunk.encryptRounds.Last();

            if (
                !lastRound.hash.SequenceEqual(hash) ||
                !RSAProvider.VerifyHash(hash, HASH_ALGORITHM_NAME, lastRound.sign))
            {
                return(null);
            }

            return(new DataInstance(chunk.data, chunk.hashOrigin, chunk.signOrigin, chunk.encryptRounds));
        }
コード例 #3
0
        public DataInstance encryptData(DataInstance dataInst, int roundsCount = 1)
        {
            DataInstance instance = dataInst;

            instance.encryptRounds = new List <EncryptRound>(dataInst.encryptRounds);
            for (int i = 0; i < roundsCount; i++)
            {
                AESProvider.GenerateKey();
                AESProvider.GenerateIV();
                byte[] key = AESProvider.Key;
                byte[] IV  = AESProvider.IV;

                //let's encrypt data
                byte[] encrypted;
                using (MemoryStream mstream = new MemoryStream())
                    using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(mstream,
                                                                            aesProvider.CreateEncryptor(key, IV), CryptoStreamMode.Write))
                            cryptoStream.Write(instance.data, 0, instance.data.Length);
                        encrypted = mstream.ToArray();
                    }

                instance.data = encrypted;

                //now encrypt AES key
                byte[] enKey = RSAProvider.Encrypt(key, USE_AOEP);

                //and hash&sign
                byte[] hash = HashProvider.ComputeHash(instance.data);
                byte[] sign = RSAProvider.SignHash(hash, HASH_ALGORITHM_NAME);

                //now create Encrypt Round
                EncryptRound round = new EncryptRound(enKey, IV, hash, sign, instance.encryptRounds.Count);

                //and add to rounds list
                instance.encryptRounds.Add(round);
            }

            return(instance);
        }
コード例 #4
0
 private bool checkIfCanDecrypt(EncryptRound round)
 {
     return(RSAProvider.VerifyHash(round.hash, HASH_ALGORITHM_NAME, round.sign));
 }