Last active
August 1, 2016 16:12
-
-
Save aarondandy/1539d75621b128fa8d58d420cf1fe93b to your computer and use it in GitHub Desktop.
Bad encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace EncryptPassword | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var random = new Random((int)DateTime.Now.Ticks); | |
Console.Write("Enter the password: "); | |
var rawPassword = Console.ReadLine(); | |
ulong key = (ulong)random.Next(0, 65536); | |
Console.WriteLine($"Secret key: {key}"); | |
string encryptedText = Encrypt(rawPassword, key); | |
Console.WriteLine($"Encrypted:"); | |
Console.WriteLine(encryptedText); | |
Console.ReadKey(); | |
} | |
static string Encrypt(string rawText, ulong key) | |
{ | |
string base64EncryptedText; | |
// create a provider using the encryption key | |
using (var provider = new DESCryptoServiceProvider | |
{ | |
Key = BitConverter.GetBytes(key), | |
IV = BitConverter.GetBytes(key) | |
}) | |
{ | |
// get an encryptor from the provider | |
var encryptor = provider.CreateEncryptor(); | |
// Convert the text from a string to bytes because the encryption algorithm works on byte arrays. | |
var passwordBytes = Encoding.ASCII.GetBytes(rawText); | |
// Use the encryptor to transform the block of bytes into | |
var encryptedBytes = encryptor.TransformFinalBlock(passwordBytes, 0, passwordBytes.Length); | |
// convert the bytes to base64 text so we can store it in a text file | |
base64EncryptedText = Convert.ToBase64String(encryptedBytes); | |
} | |
// return the generated encrypted password | |
return base64EncryptedText; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given that my encrypted password generated from the program above is:
Find what my actual plain text password is. Note that the key I have selected is between 0 and 65535 inclusive to keep run-times short.