Skip to content

Instantly share code, notes, and snippets.

@aarondandy
Last active August 1, 2016 16:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aarondandy/1539d75621b128fa8d58d420cf1fe93b to your computer and use it in GitHub Desktop.
Bad encryption
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;
}
}
}
@aarondandy
Copy link
Author

Given that my encrypted password generated from the program above is:

FtAMuJQMsSDRBI+BEPEyow==

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment