private static void TestEncryption()
 {
     byte[] salt = new byte[] { 65, 61, 53, 222, 105, 5, 199, 241, 213, 56, 19, 120, 251, 37, 66, 185 };
     byte[] data = new byte[255];
     for (int i = 0; i < data.Length; i++)
     {
         data[i] = (byte)i;
     }
     byte[] password = new byte[16];
     for (int i = password.Length - 1; i >= 0; i--)
     {
         password[i] = (byte)i;
     }
     byte[] encrypted = CryptoUtility.AesEncryption(data, password, salt);
     byte[] decrypted = CryptoUtility.AesDecryption(encrypted, password, salt);
     if (!decrypted.SequenceEqual(data))
     {
         throw new ApplicationException("AES encryption test fail");
     }
 }
Пример #2
0
        /// <summary>
        /// Store an encrypted key file for user or machine level usage
        /// </summary>
        /// <param name="scope">Scope</param>
        /// <returns>RSA key</returns>
        public static RSA RSAFromFile(DataProtectionScope scope)
        {
            byte[] esp     = new byte[] { 69, 155, 31, 254, 7, 18, 99, 187 };
            byte[] esl     = new byte[] { 101, 5, 79, 221, 48, 42, 26, 123 };
            string xmlFile = (scope == DataProtectionScope.CurrentUser ? Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "esku_123_abc.bin") :
                              Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "eskm_123_abc.bin"));
            RSACryptoServiceProvider rsa;

            if (File.Exists(xmlFile))
            {
                byte[] xmlBytes = File.ReadAllBytes(xmlFile);
                xmlBytes = CryptoUtility.AesDecryption(xmlBytes, esp, esl);
                rsa      = new RSACryptoServiceProvider();
                RSAKeyExtensions.FromXmlString(rsa, System.Text.Encoding.ASCII.GetString(xmlBytes));
            }
            else
            {
                rsa = new RSACryptoServiceProvider(4096);
                byte[] xmlBytes = System.Text.Encoding.ASCII.GetBytes(RSAKeyExtensions.ToXmlString(rsa, true));
                xmlBytes = CryptoUtility.AesEncryption(xmlBytes, esp, esl);
                File.WriteAllBytes(xmlFile, xmlBytes);
            }
            return(rsa);
        }