public void ConfigurationProtectorTestEncryptedButNoProvider() { RijndaelManaged myRijndael = new RijndaelManaged(); myRijndael.GenerateKey(); KeyAlgorithmPair pair = new KeyAlgorithmPair(myRijndael.Key, myRijndael.GetType().AssemblyQualifiedName); SaveKeyPair(pair, xmlStringWithDpapi); using (ConfigurationContext context = CreateContext(xmlStringNoStorageProvider)) { using (ConfigurationProtector protector = new ConfigurationProtector()) { protector.Load(context, sectionName); } } }
public void ConfigurationProtectorTestWithDpapi() { string mySecret = "mary had a little lamb"; RijndaelManaged myRijndael = new RijndaelManaged(); myRijndael.GenerateKey(); KeyAlgorithmPair pair = new KeyAlgorithmPair(myRijndael.Key, myRijndael.GetType().AssemblyQualifiedName); SaveKeyPair(pair, xmlStringWithDpapi); ConfigurationContext context = CreateContext(xmlStringWithDpapi); using (ConfigurationProtector protector = new ConfigurationProtector()) { protector.Load(context, sectionName); byte[] inBytes = UnicodeEncoding.Unicode.GetBytes(mySecret); byte[] encryptedBytes = protector.Encrypt(inBytes); byte[] decryptedBytes = protector.Decrypt(encryptedBytes); Assert.AreEqual(mySecret, UnicodeEncoding.Unicode.GetString(decryptedBytes)); } }
/// <summary> /// Initializes a new instance of the <see cref="RijndaelEncryptor"/> class using the user supplied key and initial vector arrays. /// NOTE: these arrays will be validated for use with the <see cref="RijndaelManaged"/> cypher. /// </summary> /// <param name="encryptedKey"></param> /// <param name="encryptedIV"></param> public RijndaelEncryptor(byte[] encryptedKey, byte[] encryptedIV) { if (encryptedKey == null) { throw new ArgumentNullException("encryptedKey"); } if (encryptedIV == null) { throw new ArgumentNullException("encryptedIV"); } //Verify encrypted key length is valid for this cryptor algo. int keylen = encryptedKey.Length << 3; if (!_crypt.ValidKeySize(keylen)) { string errmsg = "Encryption key length(" + keylen.ToString() + ") is not for this algorithm:" + _crypt.GetType().Name; throw new ApplicationException(errmsg); } //Verify encrypted iv length is valid for this cryptor algo. int len = encryptedIV.Length << 3; if (len != _crypt.BlockSize) { string errmsg = "Encryption key length(" + len.ToString() + ") is not for this algorithm:" + _crypt.GetType().Name; throw new ApplicationException(errmsg); } EncryptKey = encryptedKey; EncryptIV = encryptedIV; }