public void Decrypt_Plaintext() { // Get the bytes of the string "Key". This will be the decryption key // that we use. byte[] key = Encoding.ASCII.GetBytes("Key"); // Decrypt the cipher using the key byte[] decrypted = RC4.Apply(CIPHER_PLAINTEXT, key); // Decode the decrypted array string decoded = Encoding.ASCII.GetString(decrypted); // Validate the decoded string Assert.AreEqual(decoded, "Plaintext"); }
public void Decrypt_AttackAtDawn() { // Get the bytes of the string "Key". This will be the decryption key // that we use. byte[] key = Encoding.ASCII.GetBytes("Secret"); // Decrypt the cipher using the key byte[] decrypted = RC4.Apply(CIPHER_ATTACKATDAWN, key); // Decode the decrypted array string decoded = Encoding.ASCII.GetString(decrypted); // Validate the decoded string Assert.AreEqual(decoded, "Attack at dawn"); }
public void Encrypt_Plaintext() { // Get the bytes of the string "Plaintext". This will be the data // that we are encoding. byte[] data = Encoding.ASCII.GetBytes("Plaintext"); // Get the bytes of the string "Key". This will be the encryption key // that we use. byte[] key = Encoding.ASCII.GetBytes("Key"); // Encrypt the data using the key byte[] encrypted = RC4.Apply(data, key); // Validate that the encrypted data is the same as the cipher data Assert.IsTrue(encrypted.SequenceEqual(CIPHER_PLAINTEXT)); }
public void Encrypt_AttactAtDawn() { // Get the bytes of the string "Attack at dawn". This will be the data // that we are encoding. byte[] data = Encoding.ASCII.GetBytes("Attack at dawn"); // Get the bytes of the string "Secret". This will be the encryption key // that we use. byte[] key = Encoding.ASCII.GetBytes("Secret"); // Encrypt the data using the key byte[] encrypted = RC4.Apply(data, key); // Validate that the encrypted data is the same as the cipher data Assert.IsTrue(encrypted.SequenceEqual(CIPHER_ATTACKATDAWN)); }
public void Encrypt_Pedia() { // Get the bytes of the string "pedia". This will be the data // that we are encoding. byte[] data = Encoding.ASCII.GetBytes("pedia"); // Get the bytes of the string "Wiki". This will be the encryption key // that we use. byte[] key = Encoding.ASCII.GetBytes("Wiki"); // Encrypt the data using the key byte[] encrypted = RC4.Apply(data, key); // Validate that the encrypted data is the same as the cipher data Assert.IsTrue(encrypted.SequenceEqual(CIPHER_PEDIA)); }
static void Main(string[] args) { // Let's say we had the phrase "The one ring" string phrase = "The one ring"; // And we wanted to encrypt it, using the phrase "Keep it secret. Keep it safe." string key_phrase = "Keep it secret. Keep it safe."; // First, let's get the byte data of the phrase byte[] data = Encoding.UTF8.GetBytes(phrase); // Next, let's get the byte data of the key phrase byte[] key = Encoding.UTF8.GetBytes(key_phrase); // We can encrypt it like so byte[] encrypted_data = RC4.Apply(data, key); // Now, RC4 is a symetric algorithm, meaning, if we encrypt something // with a given key, we can run the encrypted data through the same // method with the same key to decrypt it. // Let's do that byte[] decrypted_data = RC4.Apply(encrypted_data, key); // Decode the decrypted data string decrypted_phrase = Encoding.UTF8.GetString(decrypted_data); // Let's output the data created above to the console so we can see the results Console.WriteLine("Phrase:\t\t\t{0}", phrase); Console.WriteLine("Phrase Bytes:\t\t{0}", BitConverter.ToString(data)); Console.WriteLine("Key Phrase:\t\t{0}", key_phrase); Console.WriteLine("Key Bytes:\t\t{0}", BitConverter.ToString(key)); Console.WriteLine("Encryption Result:\t{0}", BitConverter.ToString(encrypted_data)); Console.WriteLine("Decryption Result:\t{0}", BitConverter.ToString(decrypted_data)); Console.WriteLine("Decrypted Phrase:\t{0}", decrypted_phrase); Console.WriteLine(Environment.NewLine + "Press enter to close"); Console.ReadLine(); }