Beispiel #1
0
        public static async Task <CNG> DecryptAsync(Stream encryptedStream, Byte[] iv, Byte[] bkey)
        {
            CNG cng = new CNG();

            cng.Key    = bkey;
            cng.Iv     = iv;
            cng.Stream = encryptedStream;
            return(await Task.Factory.StartNew(() =>
            {
                using (Aes aes = new AesCryptoServiceProvider())
                {
                    aes.Key = cng.Key;
                    aes.IV = cng.Iv;
                    aes.Padding = PaddingMode.PKCS7;
                    // Decrypt the message
                    using (MemoryStream plaintext = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            encryptedStream.CopyTo(cs);
                            cs.Close();
                            cng.PlaintextBytes = plaintext.ToArray();
                        }
                    }
                }
                return cng;
            }));
        }
Beispiel #2
0
        public AES()
        {
            CNG c = new CNG();

            c.Key = Key = RijndaelManaged.Create().Key;
            Cng   = c;
        }
Beispiel #3
0
 public static CNG Decrypt(CNG c)
 {
     Decrypt(out c.PlaintextBytes, c.EncryptedBytes, c.Iv, c.Key);
     c.EncryptedBytes = null;
     c.Iv             = null;
     return(c);
 }
Beispiel #4
0
        public static async Task <CNG> EncryptAsync(Byte[] key, Stream plaintextMessage)
        {
            CNG cng = new CNG();

            cng.Key    = key;
            cng.Stream = plaintextMessage;
            return(await Task.Factory.StartNew(() =>
            {
                using (Aes aes = new AesCryptoServiceProvider())
                {
                    aes.Key = key;
                    cng.Iv = aes.IV;
                    aes.Padding = PaddingMode.PKCS7;
                    using (MemoryStream ciphertext = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            plaintextMessage.CopyTo(cs);
                            cs.Close();
                            cng.EncryptedBytes = ciphertext.ToArray();
                        }
                    }
                    return cng;
                }
            }));
        }
Beispiel #5
0
        public static CNG B(CNG c)
        {
            if (c.Bob == null)
            {
                c.Bob = new ECDiffieHellmanCng();

                c.Bob.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                c.Bob.HashAlgorithm         = CngAlgorithm.Sha256;
                c.PublicKey = c.Bob.PublicKey.ToByteArray();
                //c.cngkey = CngKey.Import(c.bpublicKey, CngKeyBlobFormat.EccPublicBlob);
                c.Key        = c.Bob.DeriveKeyMaterial(CngKey.Import(c.bPublicKey, CngKeyBlobFormat.EccPublicBlob));
                c.Iv         = null;
                c.bPublicKey = null;
                //c.bob = null;
                c.EncryptedBytes = null;
                c.PlaintextBytes = null;
                // c.alice = null;
                return(c);
            }
            if (c.Bob != null)
            {
                c.CngKey = CngKey.Import(c.bPublicKey, CngKeyBlobFormat.EccPublicBlob);
                //c.bcngkey = c.cngkey;
                c.Key = c.Bob.DeriveKeyMaterial(CngKey.Import(c.bPublicKey, CngKeyBlobFormat.EccPublicBlob));


                c.EncryptedBytes = null;
                c.Iv             = null;
                //c.publicKey = null;
                c.bPublicKey = null;
                //c.bob = null;
                c.PlaintextBytes = null;
                //c.alice = null;
                return(c);
            }
            c.EncryptedBytes = null;
            c.Iv             = null;
            //c.publicKey = null;
            c.bPublicKey = null;
            //c.bob = null;
            c.EncryptedBytes = null;
            c.PlaintextBytes = null;
            //c.alice = null;
            return(c);
        }
Beispiel #6
0
        public static CNG A(CNG c)
        {
            if (c.Alice == null)
            {
                c.Alice = new ECDiffieHellmanCng();

                c.Alice.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                c.Alice.HashAlgorithm         = CngAlgorithm.Sha256;
                c.PublicKey      = c.Alice.PublicKey.ToByteArray();
                c.EncryptedBytes = null;
                c.Iv             = null;
                c.PlaintextBytes = null;
                return(c);
            }
            if (c.Alice != null)
            {
                try
                {
                    c.CngKey = CngKey.Import(c.bPublicKey, CngKeyBlobFormat.EccPublicBlob);
                    c.Key    = c.Alice.DeriveKeyMaterial(CngKey.Import(c.bPublicKey, CngKeyBlobFormat.EccPublicBlob));
                    c.Iv     = null;
                    //c.publicKey = null;
                    c.bPublicKey = null;
                    //c.bob = null;
                    c.EncryptedBytes = null;
                    c.PlaintextBytes = null;
                    //c.alice = null;
                    return(c);
                }
                catch (Exception) { return(c); }
            }

            c.Iv = null;
            //c.publicKey = null;
            c.bPublicKey = null;
            //c.bob = null;
            c.EncryptedBytes = null;
            c.PlaintextBytes = null;
            //c.alice = null;
            return(c);
        }
Beispiel #7
0
 public static CNG Encrypt(CNG c)
 {
     Encrypt(c.Key, c.PlaintextBytes, out c.EncryptedBytes, out c.Iv);
     c.PlaintextBytes = null;
     return(c);
 }