コード例 #1
0
        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");
            }

            byte[] protectedData   = DataProtector.Protect(salt);
            byte[] unprotectedData = DataProtector.Unprotect(protectedData);
            if (!unprotectedData.SequenceEqual(salt))
            {
                throw new ApplicationException("Protected data API fail");
            }
        }
コード例 #2
0
        public void AESEncryption()
        {
            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);
            Assert.IsTrue(decrypted.SequenceEqual(data));

            byte[] protectedData   = DataProtector.Protect(salt);
            byte[] unprotectedData = DataProtector.Unprotect(protectedData);
            Assert.IsTrue(unprotectedData.SequenceEqual(salt));
        }
コード例 #3
0
ファイル: DataProtector.cs プロジェクト: qinxg/ExchangeSharp
        /// <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, CryptoUtility.UTF8EncodingNoPrefix.GetString(xmlBytes));
            }
            else
            {
                rsa = new RSACryptoServiceProvider(4096);
                byte[] xmlBytes = RSAKeyExtensions.ToXmlString(rsa, true).ToBytesUTF8();
                xmlBytes = CryptoUtility.AesEncryption(xmlBytes, esp, esl);
                File.WriteAllBytes(xmlFile, xmlBytes);
            }
            return(rsa);
        }