Beispiel #1
0
        public void PublicKeyRoundTrip()
        {
            var rsa = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha1);
            var key = rsa.CreateKeyPair(512);

            int supportedFormats = 0;

            foreach (CryptographicPublicKeyBlobType format in Enum.GetValues(typeof(CryptographicPublicKeyBlobType)))
            {
                try
                {
                    byte[] keyBlob  = key.ExportPublicKey(format);
                    var    key2     = rsa.ImportPublicKey(keyBlob, format);
                    byte[] key2Blob = key2.ExportPublicKey(format);

                    CollectionAssertEx.AreEqual(keyBlob, key2Blob);

                    WinRTCrypto.CryptographicEngine.Encrypt(key2, new byte[0]);

                    Debug.WriteLine("Format {0} supported.", format);
                    Debug.WriteLine(Convert.ToBase64String(keyBlob));
                    supportedFormats++;
                }
                catch (NotSupportedException)
                {
                    Debug.WriteLine("Format {0} NOT supported.", format);
                }
            }

            Assert.IsTrue(supportedFormats > 0, "No supported formats.");
        }
Beispiel #2
0
        public void CreateKey()
        {
            foreach (var algorithmAndExpectedResult in this.stretchedKeyBase64)
            {
                Debug.WriteLine("Testing algorithm: {0}", algorithmAndExpectedResult.Key);
                var algorithm         = WinRTCrypto.KeyDerivationAlgorithmProvider.OpenAlgorithm(algorithmAndExpectedResult.Key);
                ICryptographicKey key = algorithm.CreateKey(this.originalKey);
                Assert.IsNotNull(key);
                Assert.AreEqual(this.originalKey.Length * 8, key.KeySize);

                IKeyDerivationParameters parameters = WinRTCrypto.KeyDerivationParameters.BuildForPbkdf2(this.salt, this.iterations);
                Assert.AreEqual(this.iterations, parameters.IterationCount);
                CollectionAssertEx.AreEqual(this.salt, parameters.KdfGenericBinary);

                try
                {
                    byte[] keyMaterial = WinRTCrypto.CryptographicEngine.DeriveKeyMaterial(key, parameters, 20);
                    Assert.AreEqual(algorithmAndExpectedResult.Value, Convert.ToBase64String(keyMaterial));
                }
                catch (NotSupportedException)
                {
                    Debug.WriteLine(" - Not supported on this platform");
                }
            }
        }
        public void CreateDecryptor()
        {
            byte[] cipherText = Convert.FromBase64String(DataAesCiphertextBase64);
            var    decryptor  = WinRTCrypto.CryptographicEngine.CreateDecryptor(this.aesKey, this.iv);

            byte[] plaintext = decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
            CollectionAssertEx.AreEqual(this.data, plaintext);
        }
 public void EncryptAndDecrypt_AES_IV()
 {
     byte[] cipherText = WinRTCrypto.CryptographicEngine.Encrypt(this.aesKey, this.data, this.iv);
     CollectionAssertEx.AreNotEqual(this.data, cipherText);
     Assert.AreEqual(DataAesCiphertextBase64, Convert.ToBase64String(cipherText));
     byte[] plainText = WinRTCrypto.CryptographicEngine.Decrypt(this.aesKey, cipherText, this.iv);
     CollectionAssertEx.AreEqual(this.data, plainText);
 }
 public void EncryptAndDecrypt_AES_NoIV()
 {
     byte[] cipherText = WinRTCrypto.CryptographicEngine.Encrypt(this.aesKey, this.data, null);
     CollectionAssertEx.AreNotEqual(this.data, cipherText);
     Assert.AreEqual("oCSAA4sUCGa5ukwSJdeKWw==", Convert.ToBase64String(cipherText));
     byte[] plainText = WinRTCrypto.CryptographicEngine.Decrypt(this.aesKey, cipherText, null);
     CollectionAssertEx.AreEqual(this.data, plainText);
 }
Beispiel #6
0
        public void GetBytes()
        {
            byte[] keyFromPassword = NetFxCrypto.DeriveBytes.GetBytes(Password1, Salt1, 5, 10);
            byte[] keyFromBytes    = NetFxCrypto.DeriveBytes.GetBytes(Encoding.UTF8.GetBytes(Password1), Salt1, 5, 10);
            CollectionAssertEx.AreEqual(keyFromPassword, keyFromBytes);
            Assert.AreEqual(DerivedKey, Convert.ToBase64String(keyFromPassword));

            byte[] keyWithOtherSalt = NetFxCrypto.DeriveBytes.GetBytes(Password1, Salt2, 5, 10);
            CollectionAssertEx.AreNotEqual(keyFromPassword, keyWithOtherSalt);
        }
        public void GenerateRandom()
        {
            byte[] buffer1 = WinRTCrypto.CryptographicBuffer.GenerateRandom(15);
            Assert.AreEqual(15, buffer1.Length);

            byte[] buffer2 = WinRTCrypto.CryptographicBuffer.GenerateRandom(15);
            Assert.AreEqual(15, buffer2.Length);

            CollectionAssertEx.AreNotEqual(buffer1, buffer2);
        }
 public void EncryptAndDecrypt_RSA()
 {
     byte[] keyMaterialBytes = Convert.FromBase64String(AesKeyMaterial);
     byte[] cipherText       = WinRTCrypto.CryptographicEngine.Encrypt(
         this.rsaEncryptingKey,
         keyMaterialBytes,
         null);
     CollectionAssertEx.AreNotEqual(keyMaterialBytes, cipherText);
     byte[] plainText = WinRTCrypto.CryptographicEngine.Decrypt(this.rsaEncryptingKey, cipherText, null);
     CollectionAssertEx.AreEqual(keyMaterialBytes, plainText);
 }
Beispiel #9
0
        public void RSAParametersPrivateKeyRoundtrip()
        {
            var               rsa        = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha1);
            var               keyPair    = rsa.CreateKeyPair(512);
            RSAParameters     parameters = keyPair.ExportParameters(includePrivateParameters: true);
            ICryptographicKey keyPair2   = rsa.ImportParameters(parameters);

            var blob1 = keyPair.Export();
            var blob2 = keyPair2.Export();

            CollectionAssertEx.AreEqual(blob1, blob2);
        }
        public void CreateKey()
        {
            var algorithm         = WinRTCrypto.KeyDerivationAlgorithmProvider.OpenAlgorithm(KeyDerivationAlgorithm.Pbkdf2Sha1);
            ICryptographicKey key = algorithm.CreateKey(this.originalKey);

            Assert.IsNotNull(key);
            Assert.AreEqual(this.originalKey.Length * 8, key.KeySize);

            IKeyDerivationParameters parameters = WinRTCrypto.KeyDerivationParameters.BuildForPbkdf2(this.salt, this.iterations);

            Assert.AreEqual(this.iterations, parameters.IterationCount);
            CollectionAssertEx.AreEqual(this.salt, parameters.KdfGenericBinary);

            byte[] keyMaterial = WinRTCrypto.CryptographicEngine.DeriveKeyMaterial(key, parameters, 20);
            Assert.AreEqual(this.stretchedKeyBase64, Convert.ToBase64String(keyMaterial));
        }
        private void EncryptDecryptStreamChain(byte[] data)
        {
            var encryptor = WinRTCrypto.CryptographicEngine.CreateEncryptor(this.aesKey);
            var decryptor = WinRTCrypto.CryptographicEngine.CreateDecryptor(this.aesKey);

            var decryptedStream = new MemoryStream();

            using (var decryptingStream = new CryptoStream(decryptedStream, decryptor, CryptoStreamMode.Write))
            {
                using (var encryptingStream = new CryptoStream(decryptingStream, encryptor, CryptoStreamMode.Write))
                {
                    encryptingStream.Write(data, 0, data.Length);
                }
            }

            CollectionAssertEx.AreEqual(data, decryptedStream.ToArray());
        }
Beispiel #12
0
        public void RSAParametersPublicKeyRoundtrip()
        {
            var           rsa        = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithm.RsaOaepSha1);
            var           keyPair    = rsa.CreateKeyPair(512);
            RSAParameters parameters = keyPair.ExportParameters(includePrivateParameters: false);

            Assert.IsNull(parameters.P, "Private key should have been omitted.");
            Assert.IsNull(parameters.InverseQ, "Private key should have been omitted.");
            Assert.IsNull(parameters.D, "Private key should have been omitted.");
            Assert.IsNull(parameters.Q, "Private key should have been omitted.");
            Assert.IsNull(parameters.DP, "Private key should have been omitted.");
            Assert.IsNull(parameters.DQ, "Private key should have been omitted.");
            ICryptographicKey publicKey = rsa.ImportParameters(parameters);

            var blob1 = keyPair.ExportPublicKey();
            var blob2 = publicKey.ExportPublicKey();

            CollectionAssertEx.AreEqual(blob1, blob2);
        }
        public void PublicKeyRoundTrip()
        {
            foreach (var algorithm in KeyAlgorithmsToTest)
            {
                Debug.WriteLine("** Algorithm: {0} **", algorithm.Key);
                var keyAlgorithm = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(algorithm.Key);
                var key          = keyAlgorithm.CreateKeyPair(algorithm.Value);

                int supportedFormats = 0;
                foreach (CryptographicPublicKeyBlobType format in Enum.GetValues(typeof(CryptographicPublicKeyBlobType)))
                {
                    try
                    {
                        byte[] keyBlob  = key.ExportPublicKey(format);
                        var    key2     = keyAlgorithm.ImportPublicKey(keyBlob, format);
                        byte[] key2Blob = key2.ExportPublicKey(format);

                        CollectionAssertEx.AreEqual(keyBlob, key2Blob);

                        try
                        {
                            WinRTCrypto.CryptographicEngine.Encrypt(key2, new byte[0]);
                        }
                        catch (NotSupportedException)
                        {
                            // Some algorithms, such as ECDSA, only support signing/verifying.
                        }

                        Debug.WriteLine("Format {0} supported.", format);
                        Debug.WriteLine("    " + Convert.ToBase64String(keyBlob));
                        supportedFormats++;
                    }
                    catch (NotSupportedException)
                    {
                        Debug.WriteLine("Format {0} NOT supported.", format);
                    }
                }

                Assert.IsTrue(supportedFormats > 0, "No supported formats.");
            }
        }
 public void DecodeFromHexString()
 {
     CollectionAssertEx.AreEqual(new byte[] { 0x00, 0x1, 0xf, 0xae, 0xff, 0xf0 }, WinRTCrypto.CryptographicBuffer.DecodeFromHexString("00010faefff0"));
 }
 public void DecodeFromHexString_EmptyString()
 {
     CollectionAssertEx.AreEqual(new byte[0], WinRTCrypto.CryptographicBuffer.DecodeFromHexString(string.Empty));
 }