Exemple #1
0
        public static byte[] Decrypt(SecureString privateKey, byte[] data)
        {
            if (privateKey == null)
            {
                throw new ArgumentNullException(nameof(privateKey));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var rsaOnly = data[data.Length - 1] == 0;

            if (rsaOnly)
            {
                byte[] d = new byte[data.Length - 1];

                Array.Copy(data, 0, d, 0, d.Length);
                return(Rsa.Decrypt(privateKey, d));
            }
            else
            {
                byte[] keyLength = new byte[4];

                Array.Copy(data, data.Length - 5, keyLength, 0, 4);

                byte[] k = new byte[keyLength.ToInteger()];
                byte[] d = new byte[data.Length - k.Length - 5];

                Array.Copy(data, k, k.Length);
                Array.Copy(data, k.Length, d, 0, d.Length);

                var    key           = Rsa.Decrypt(privateKey, k);
                byte[] decryptedData = Aes.Decrypt(key.ToSecureString(), d);

                return(decryptedData);
            }
        }
Exemple #2
0
 /// <summary>
 /// Descrypts encrypted data
 /// </summary>
 /// <param name="password">The password used to decrypt data</param>
 /// <param name="data">The data to decrypt</param>
 /// <returns>The decrypted data</returns>
 /// <exception cref="ArgumentNullException"><paramref name="password"/> is null</exception>
 /// <exception cref="ArgumentNullException"><paramref name="data"/> is null</exception>
 public static byte[] Decrypt(SecureString password, byte[] data) =>
 Aes.Decrypt(password, KeyMaterial.DefaultIterations, data);