using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace PasswordManager
{
public partial class Main : Form
{
public static byte[] initialVector = Encoding.ASCII.GetBytes( "s4V90gI2i4eNLvF3" );
public static byte[] salt = Encoding.ASCII.GetBytes( "p7Ps986x2h08kzOlBq03zAC3Y8pfRwGQ9Wu3LoZg97OXiDHEDlY7g5FmK09z37T1" );
public static byte[] encryptedPasswords = {
22, 89, 217, 116, 132, 30, 247, 115, 130, 56, 176, 19, 125, 34, 167, 1, 200, 254, 216, 5, 14, 1, 128, 29, 102, 185, 173, 110, 1, 174, 18, 13, 115, 215, 48, 73,
35, 181, 202, 173, 36, 89, 227, 6, 60, 7, 216, 35, 45, 15, 209, 194, 27, 11, 200, 175, 99, 249, 116, 119, 104, 20, 8, 46, 111, 50, 51, 112, 212, 88, 136, 62, 153,
21, 46, 233, 9, 247, 208, 241, 202, 160, 237, 166, 12, 85, 21, 91, 123, 246, 79, 252, 28, 253, 141, 35, 168, 26, 50, 93, 206, 39, 76, 89, 74, 159, 48, 149, 224,
172, 32, 196, 165, 115, 151, 23, 62, 218, 211, 182, 11, 191, 148, 67, 82, 148, 92, 231, 30, 75, 28, 59, 163, 209, 104, 36, 102, 117, 56, 141, 74, 164, 173, 142,
23, 187, 196, 133, 160, 210, 251, 238, 231, 124, 186, 129, 206, 151, 253, 202, 21, 229, 47, 94, 202, 68, 145, 179, 70, 137, 218, 107, 73, 159, 136, 39, 49, 253,
206, 87, 136, 109, 210, 203, 254, 46, 151, 22, 235, 167, 113, 215, 61, 174, 241, 109, 209, 87, 15, 121, 39, 217, 178, 229, 180, 128, 24, 61, 78, 204, 198, 149,
125, 49, 11, 217, 78, 25, 194, 29, 188, 63, 29, 175, 74, 82, 168, 185, 247, 143, 199, 151, 122, 222, 45, 77, 128, 109, 35, 67, 94, 34, 99, 19, 235, 252, 46, 224,
132, 217, 60, 80, 53, 187, 119, 7, 190, 73, 32, 208, 146, 22, 75, 15, 144, 190, 140, 132, 145, 184, 162, 207, 77, 50, 78, 203, 131, 29, 64, 0, 41, 147, 240, 159,
27, 17, 83, 24, 227, 112, 1, 171, 252, 83, 8, 193, 98, 175, 255, 70, 86, 22, 245, 238, 63, 47, 111, 81, 44, 251, 213, 73, 8, 113, 58, 188, 124, 238, 238, 169
};
public class PasswordEntry
{
public PasswordEntry( String identifier, String password )
{
this.identifier = identifier;
this.password = password;
}
public String identifier;
public String password;
}
public static List<String> passwordEntries;
public Main()
{
InitializeComponent();
}
private void wrongPasswordDelay()
{
passwordBox.Enabled = false;
passwordBox.Refresh();
System.Threading.Thread.Sleep( 1000 );
passwordBox.Enabled = true;
MessageBox.Show( "The passwords could not be decrypted, as you specified an incorrect password.", "Decryption Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
private void passwordBox_KeyPress( object sender, KeyPressEventArgs e )
{
if ( e.KeyChar != 13 ) return;
if ( passwordBox.Text.Trim().Length == 0 ) return;
String key = passwordBox.Text.Trim();
String decryptedPasswordData = decrypt( encryptedPasswords, key );
if ( decryptedPasswordData.IndexOf( '\n' ) == -1 ) { wrongPasswordDelay(); return; }
string[] lines = decryptedPasswordData.Split( '\n' );
if ( !lines[0].Equals( key ) ) { wrongPasswordDelay(); return; }
lockStatusIcon.Image = lockOpenImage.Image;
passwordBox.Visible = false;
passwordList.Visible = true;
copyButton.Visible = true;
passwordEntries = new List<String>();
for ( int i = 1; i < lines.Length; i += 2 )
{
passwordEntries.Add( lines[i + 1] );
passwordList.Items.Add( lines[i] );
}
passwordList.SelectedIndex = 0;
}
private static byte[] encrypt( String plainText, String key )
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes( plainText );
PasswordDeriveBytes derivedKey = new PasswordDeriveBytes( key, salt, "SHA1", 2 );
byte[] keyBytes = derivedKey.GetBytes( 256 / 8 );
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
byte[] cipherTextBytes = null;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor( keyBytes, initialVector );
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream( memoryStream, encryptor, CryptoStreamMode.Write );
cryptoStream.Write( plainTextBytes, 0, plainTextBytes.Length );
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
symmetricKey.Clear();
return cipherTextBytes;
}
private static String decrypt( byte[] cipherText, String key )
{
byte[] cipherTextBytes = cipherText;
PasswordDeriveBytes derivedKey = new PasswordDeriveBytes( key, salt, "SHA1", 2 );
byte[] keyBytes = derivedKey.GetBytes( 256 / 8 );
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int byteCount = 0;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor( keyBytes, initialVector );
MemoryStream memoryStream = new MemoryStream( cipherTextBytes );
CryptoStream cryptoStream = new CryptoStream( memoryStream, decryptor, CryptoStreamMode.Read );
try
{
byteCount = cryptoStream.Read( plainTextBytes, 0, plainTextBytes.Length );
}
catch ( CryptographicException e ) { return ""; }
memoryStream.Close();
cryptoStream.Close();
symmetricKey.Clear();
return Encoding.UTF8.GetString( plainTextBytes, 0, byteCount );
}
private void copyButton_Click( object sender, EventArgs e )
{
Clipboard.SetText( passwordEntries[passwordList.SelectedIndex] );
}
}
}