Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 9th, 2011  |  syntax: C#  |  size: 6.43 KB  |  views: 91  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Security.Cryptography;
  10.  
  11. namespace PasswordManager
  12. {
  13.     public partial class Main : Form
  14.     {
  15.         public static byte[] initialVector = Encoding.ASCII.GetBytes( "s4V90gI2i4eNLvF3" );
  16.         public static byte[] salt = Encoding.ASCII.GetBytes( "p7Ps986x2h08kzOlBq03zAC3Y8pfRwGQ9Wu3LoZg97OXiDHEDlY7g5FmK09z37T1" );
  17.         public static byte[] encryptedPasswords = {
  18.             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,
  19.             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,
  20.             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,
  21.             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,
  22.             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,
  23.             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,
  24.             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,
  25.             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,
  26.             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
  27.         };
  28.  
  29.         public class PasswordEntry
  30.         {
  31.             public PasswordEntry( String identifier, String password )
  32.             {
  33.                 this.identifier = identifier;
  34.                 this.password = password;
  35.             }
  36.  
  37.             public String identifier;
  38.             public String password;
  39.         }
  40.         public static List<String> passwordEntries;
  41.  
  42.         public Main()
  43.         {
  44.             InitializeComponent();
  45.         }
  46.  
  47.         private void wrongPasswordDelay()
  48.         {
  49.             passwordBox.Enabled = false;
  50.             passwordBox.Refresh();
  51.             System.Threading.Thread.Sleep( 1000 );
  52.             passwordBox.Enabled = true;
  53.  
  54.             MessageBox.Show( "The passwords could not be decrypted, as you specified an incorrect password.", "Decryption Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
  55.         }
  56.  
  57.         private void passwordBox_KeyPress( object sender, KeyPressEventArgs e )
  58.         {
  59.             if ( e.KeyChar != 13 ) return;
  60.             if ( passwordBox.Text.Trim().Length == 0 ) return;
  61.             String key = passwordBox.Text.Trim();
  62.  
  63.             String decryptedPasswordData = decrypt( encryptedPasswords, key );
  64.             if ( decryptedPasswordData.IndexOf( '\n' ) == -1 ) { wrongPasswordDelay(); return; }
  65.  
  66.             string[] lines = decryptedPasswordData.Split( '\n' );
  67.             if ( !lines[0].Equals( key ) ) { wrongPasswordDelay(); return; }
  68.  
  69.             lockStatusIcon.Image = lockOpenImage.Image;
  70.             passwordBox.Visible = false;
  71.             passwordList.Visible = true;
  72.             copyButton.Visible = true;
  73.  
  74.             passwordEntries = new List<String>();
  75.             for ( int i = 1; i < lines.Length; i += 2 )
  76.             {
  77.                 passwordEntries.Add( lines[i + 1] );
  78.                 passwordList.Items.Add( lines[i] );
  79.             }
  80.             passwordList.SelectedIndex = 0;
  81.         }
  82.  
  83.         private static byte[] encrypt( String plainText, String key )
  84.         {
  85.             byte[] plainTextBytes = Encoding.UTF8.GetBytes( plainText );
  86.  
  87.             PasswordDeriveBytes derivedKey = new PasswordDeriveBytes( key, salt, "SHA1", 2 );
  88.             byte[] keyBytes = derivedKey.GetBytes( 256 / 8 );
  89.  
  90.             RijndaelManaged symmetricKey = new RijndaelManaged();
  91.             symmetricKey.Mode = CipherMode.CBC;
  92.             byte[] cipherTextBytes = null;
  93.  
  94.             ICryptoTransform encryptor = symmetricKey.CreateEncryptor( keyBytes, initialVector );
  95.             MemoryStream memoryStream = new MemoryStream();
  96.             CryptoStream cryptoStream = new CryptoStream( memoryStream, encryptor, CryptoStreamMode.Write );
  97.  
  98.             cryptoStream.Write( plainTextBytes, 0, plainTextBytes.Length );
  99.             cryptoStream.FlushFinalBlock();
  100.             cipherTextBytes = memoryStream.ToArray();
  101.             memoryStream.Close();
  102.             cryptoStream.Close();
  103.  
  104.             symmetricKey.Clear();
  105.  
  106.             return cipherTextBytes;
  107.         }
  108.  
  109.         private static String decrypt( byte[] cipherText, String key )
  110.         {
  111.             byte[] cipherTextBytes = cipherText;
  112.  
  113.             PasswordDeriveBytes derivedKey = new PasswordDeriveBytes( key, salt, "SHA1", 2 );
  114.             byte[] keyBytes = derivedKey.GetBytes( 256 / 8 );
  115.  
  116.             RijndaelManaged symmetricKey = new RijndaelManaged();
  117.             symmetricKey.Mode = CipherMode.CBC;
  118.             byte[] plainTextBytes = new byte[cipherTextBytes.Length];
  119.  
  120.             int byteCount = 0;
  121.             ICryptoTransform decryptor = symmetricKey.CreateDecryptor( keyBytes, initialVector );
  122.             MemoryStream memoryStream = new MemoryStream( cipherTextBytes );
  123.             CryptoStream cryptoStream = new CryptoStream( memoryStream, decryptor, CryptoStreamMode.Read );
  124.  
  125.             try
  126.             {
  127.                 byteCount = cryptoStream.Read( plainTextBytes, 0, plainTextBytes.Length );
  128.             }
  129.             catch ( CryptographicException e ) { return ""; }
  130.  
  131.             memoryStream.Close();
  132.             cryptoStream.Close();
  133.  
  134.             symmetricKey.Clear();
  135.  
  136.             return Encoding.UTF8.GetString( plainTextBytes, 0, byteCount );
  137.         }
  138.  
  139.         private void copyButton_Click( object sender, EventArgs e )
  140.         {
  141.             Clipboard.SetText( passwordEntries[passwordList.SelectedIndex] );
  142.         }
  143.     }
  144. }
clone this paste RAW Paste Data