public static void InvalidCFBFeedbackSizes(int feedbackSize, bool discoverableInSetter)
        {
            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.GenerateKey();
                tdes.Mode = CipherMode.CFB;

                if (discoverableInSetter)
                {
                    // there are some key sizes that are invalid for any of the modes,
                    // so the exception is thrown in the setter
                    Assert.Throws <CryptographicException>(() =>
                    {
                        tdes.FeedbackSize = feedbackSize;
                    });
                }
                else
                {
                    tdes.FeedbackSize = feedbackSize;

                    // however, for CFB only few sizes are valid. Those should throw in the
                    // actual AES instantiation.

                    Assert.Throws <CryptographicException>(() => tdes.CreateDecryptor());
                    Assert.Throws <CryptographicException>(() => tdes.CreateEncryptor());
                }
            }
        }
예제 #2
0
        public void Process()
        {
            // Display the help
            if (_arguments.Any(a => a.Equals("?") || a.Equals("-help")))
            {
                DisplayHelp();
            }

            else if (!ConsoleHelpers.AllCommandExist(_arguments, typeof(Arguments)))
            {
                return;
            }

            // Proceed a cleanup of the folder .smk-meta
            else if (ConsoleHelpers.CommandArgExists(_arguments, Arguments.RemoveArtifacts))
            {
                CleanArtefacts();
            }

            // Generate a new key
            else if (ConsoleHelpers.CommandArgExists(_arguments, Arguments.NewKey))
            {
                DisplayHeader();
                string value = ConsoleHelpers.GetCommandArgValue(_arguments, Arguments.NewKey);
                TripleDES.GenerateKey(value, _logger);
                _logger.WriteLine(string.Format(ErrorResources.Engine_KeyFileGenerated, value));
                DisplayFooter();
            }
            // Run a backup
            else
            {
                Backup();
            }
        }
예제 #3
0
        /// <summary>
        /// 加密字符串返回加密后的字节
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public static byte[] EncryptPlainText(string plainText)
        {
            TripleDES ta = TripleDES.Create();

            ta.KeySize = 128;
            ta.GenerateKey();
            ta.GenerateIV();
            ta.Mode    = CipherMode.ECB;
            ta.Padding = PaddingMode.PKCS7;
            byte[] head = new byte[4];
            new Random().NextBytes(head);
            byte[] key         = ta.Key;
            byte[] cipherbytes = null;
            try
            {
                MemoryStream ms         = new MemoryStream();
                CryptoStream cs         = new CryptoStream(ms, ta.CreateEncryptor(), CryptoStreamMode.Write);
                byte[]       plainbytes = Encoding.Default.GetBytes(plainText);
                cs.Write(plainbytes, 0, plainbytes.Length);
                cs.Close();
                byte[] cipherText = ms.ToArray();
                ms.Close();
                int len = cipherText.Length + 20;
                cipherbytes = new byte[len];
                head.CopyTo(cipherbytes, 0);
                key.CopyTo(cipherbytes, 4);
                cipherText.CopyTo(cipherbytes, 20);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(cipherbytes);
        }
예제 #4
0
        public void Key()
        {
            TripleDES algo = TripleDES.Create();

            algo.GenerateKey();
            algo.GenerateIV();
            Assert.AreEqual(192, algo.KeySize, "Key Size");
            Assert.AreEqual(24, algo.Key.Length, "Key Length");
            Assert.AreEqual(8, algo.IV.Length, "IV Length");
        }
예제 #5
0
 public static string GenerateKey()
 {
     using (TripleDES tripleDES = TripleDES.Create())
     {
         tripleDES.Mode    = CipherMode.ECB;
         tripleDES.Padding = PaddingMode.PKCS7;
         tripleDES.GenerateKey();
         return(Convert.ToBase64String(tripleDES.Key));
     }
 }
        public static void Windows7DoesNotSupportCFB64()
        {
            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.GenerateKey();
                tdes.Mode         = CipherMode.CFB;
                tdes.FeedbackSize = 64;

                Assert.ThrowsAny <CryptographicException>(() => tdes.CreateDecryptor());
                Assert.ThrowsAny <CryptographicException>(() => tdes.CreateEncryptor());
            }
        }
        public static void ValidCFBFeedbackSizes(int feedbackSize)
        {
            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.GenerateKey();
                tdes.Mode = CipherMode.CFB;

                tdes.FeedbackSize = feedbackSize;

                using var decryptor = tdes.CreateDecryptor();
                using var encryptor = tdes.CreateEncryptor();
                Assert.NotNull(decryptor);
                Assert.NotNull(encryptor);
            }
        }
예제 #8
0
        /// <summary>
        /// 获得密钥.
        /// </summary>
        /// <returns>密钥.</returns>
        private byte[] GetLegalKey()
        {
            string sTemp = Key;

            mydes.GenerateKey();
            byte[] bytTemp   = mydes.Key;
            int    KeyLength = bytTemp.Length;

            if (sTemp.Length > KeyLength)
            {
                sTemp = sTemp.Substring(0, KeyLength);
            }
            else if (sTemp.Length < KeyLength)
            {
                sTemp = sTemp.PadRight(KeyLength, ' ');
            }
            return(ASCIIEncoding.ASCII.GetBytes(sTemp));
        }
        public static void ValidCFBFeedbackSizes(int feedbackSize)
        {
            // Windows 7 only supports CFB8.
            if (feedbackSize != 8 && PlatformDetection.IsWindows7)
            {
                return;
            }

            using (TripleDES tdes = TripleDESFactory.Create())
            {
                tdes.GenerateKey();
                tdes.Mode = CipherMode.CFB;

                tdes.FeedbackSize = feedbackSize;

                using var decryptor = tdes.CreateDecryptor();
                using var encryptor = tdes.CreateEncryptor();
                Assert.NotNull(decryptor);
                Assert.NotNull(encryptor);
            }
        }
예제 #10
0
        /// <summary>
        /// Take an action in according to the arguments
        /// </summary>
        public void Process()
        {
            // Display the help
            if (_arguments.Any(a => a.Equals("?") || a.Equals("-help")))
            {
                DisplayHelp();
                return;
            }

            else if (!ConsoleHelpers.AllCommandExist(_arguments, typeof(Arguments)))
            {
                return;
            }

            // Generate a new key
            else
            {
                DisplayHeader();
                string value = ConsoleHelpers.GetCommandArgValue(_arguments, Arguments.File);
                TripleDES.GenerateKey(value, _logger);
                _logger.WriteLine(string.Format(ErrorResources.Engine_KeyFileGenerated, value));
                DisplayFooter();
            }
        }
예제 #11
0
        public bool ValidateCertificateAndCreateKey(ProtoLogIn login, out byte[] key)
        {
            if (login == null || login.certificate == null)
            {
                key = null;
                return(false);
            }
            else
            {
                try
                {
                    // Get certificate
                    X509Certificate2         cert = new X509Certificate2(login.certificate);
                    RSACryptoServiceProvider rsa  = (RSACryptoServiceProvider)cert.PublicKey.Key;
#if DEBUG
                    if (this.key != null)
                    {
                        if (this.key.Length == 0)
                        {
                            alg = new NetAESEncryption(client);
                        }
                        else
                        {
                            alg = new NetAESEncryption(client,
                                                       this.key, 0, this.key.Length);
                        }
                        key = rsa.Encrypt(this.key, false);
                    }
                    else
                    {
                        // If no key, do not use an encryption algorithm
                        alg = null;
                        key = null;
                    }
#else
                    // Create a new symmetric key
                    TripleDES des = TripleDESCryptoServiceProvider.Create();
                    des.GenerateKey();
                    // Encrypt key with server's public key
                    this.key = des.Key;
                    key      = rsa.Encrypt(des.Key, false);
                    // Initialise the algoitm
                    alg = new NetAESEncryption(client, des.Key, 0, des.Key.Length);
                    Console.WriteLine("CLIENT: my unencrypted key:");
                    foreach (var bite in des.Key)
                    {
                        Console.Write(bite.ToString());
                    }
#endif
                    // Validate certificate
                    if (!cert.Verify())
                    {
                        X509Chain CertificateChain = new X509Chain();
                        //If you do not provide revokation information, use the following line.
                        CertificateChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
                        bool IsCertificateChainValid = CertificateChain.Build(cert);
                        if (!IsCertificateChainValid)
                        {
                            for (int i = 0; i < CertificateChain.ChainStatus.Length; i++)
                            {
                            }
                            // TODO change to false after testing
                            return(true);
                        }
                    }
                    // temporary certificate validation fix
                    return(true);
                    //return cert.Verify();
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("A problem occurred when parsing certificate from bytes: \n" + "type: " + e.GetType().FullName + "\n " + ", source: " + e.Source + "\n message: " + e.Message);
                    key = null;
                    return(false);
                }
            }
        }
예제 #12
0
 public string CreateTripleDESKey()
 {
     tripleDES.GenerateKey();
     return(ByteToString(tripleDES.Key));
 }
예제 #13
0
 public EncryptedString(string String)
 {
     des.GenerateIV();
     des.GenerateKey();
     Set(String);
 }
 private void genKey_Click(object sender, EventArgs e)
 {
     des.GenerateKey();
     textKey.Text = Convert.ToBase64String(des.Key);
 }