public void With_EncryptDecrypt()
            {
                CryptoAlgorithm algorithm = context.Build();

                functionToInvoke = (data) =>
                {
                    var encrypted = algorithm.Encrypt(data);
                    return(algorithm.Decrypt(encrypted));
                };
            }
Exemple #2
0
        public HmacDigest()
        {
            var             Keys            = new AppKeys();
            CryptoAlgorithm secretDecryptor = new CryptoAlgorithm("ENCRYPT", "FSCS", new AesManaged());

            PublicKey = Keys.Key;
            SecretKey = secretDecryptor.Decrypt(Keys.Secret);
            EpochTime = Convert.ToInt64((DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds);
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[]     keyByte = encoding.GetBytes(SecretKey);
            HMACSHA256 sha     = new HMACSHA256(keyByte);

            byte[] byteData         = encoding.GetBytes(EpochTime.ToString());
            byte[] hashedDataOutput = sha.ComputeHash(byteData);

            HashedString       = ByteToString(hashedDataOutput);
            HashedStringBase64 = Convert.ToBase64String(hashedDataOutput);
        }
        private void DecryptButton_Click(object sender, RoutedEventArgs e)
        {
            byte[] textToDecodeBytes;

            if (filePath == string.Empty)
            {
                textToDecodeBytes = Encoding.ASCII.GetBytes(TextToDecryptTextBox.Text);
            }
            else
            {
                textToDecodeBytes = File.ReadAllBytes(filePath);
            }

            if (DecryptionKeyTextBox.Text.Length != 8)
            {
                MessageBox.Show("Key must be an 8byte string", "Error!");
                return;
            }
            BitArray encryptionKey = new BitArray(Encoding.ASCII.GetBytes(DecryptionKeyTextBox.Text));

            if (textToDecodeBytes.Length == 0)
            {
                MessageBox.Show("There is nothing to decode", "Error!");
            }
            BitArray bitArrayToDecode = new BitArray(textToDecodeBytes).RevertEveryByte();

            bool[] bitsToDecode = new bool[bitArrayToDecode.Count];
            bitArrayToDecode.CopyTo(bitsToDecode, 0);

            DESBuilder builder = new DESBuilder();

            builder.AddWholeDES(encryptionKey);
            CryptoAlgorithm des = builder.Build();

            bool[] decrypted = des.Decrypt(bitsToDecode);
            decryptedBytes = decrypted.ToByteArray();

            DecryptedTextBox.Text = Encoding.ASCII.GetString(decryptedBytes);
            filePath = string.Empty;
        }