Example #1
0
        /// <summary>
        /// Encrypt the specified Password with the supplied Key
        /// </summary>
        /// <param name='Password'>
        /// Password to encrypt
        /// </param>
        /// <param name='Key'>
        /// Key for encryption
        /// </param>
        public static EncryptedPassword Encrypt(string Password, EncryptKey Key)
        {
            //Setup contracts for arguments
            Contract.Requires(!string.IsNullOrWhiteSpace(Password), "No password supplied!");
            Contract.Requires(Key != null, "No key supplied!");

            //Create instance and assign key
            EncryptedPassword EncryptedPassword = new EncryptedPassword ();
            EncryptedPassword.key = Key;

            //Create encryption algoritm
            using (RijndaelManaged AES = new RijndaelManaged())
            {
                //Assign aes keys from the supplied key
                AES.Key = Key.Key;
                AES.IV = Key.IV;

                //Create encryption streams
                var encryptor = AES.CreateEncryptor();
                using (MemoryStream ms = new MemoryStream()) {
                    using (CryptoStream cryptStream = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {
                        using (StreamWriter cryptWriter = new StreamWriter(cryptStream)) {
                            //Write password for encryption
                            cryptWriter.Write(Password);
                        }
                        //Store encrypted password
                        EncryptedPassword.passwordData = ms.ToArray();
                    }
                }
            }

            //Return instance
            return EncryptedPassword;
        }
Example #2
0
 public static LoginInfo Load(byte[] data, EncryptKey Key)
 {
     using (MemoryStream ms = new MemoryStream(data))
     using (BinaryReader br = new BinaryReader(ms)) {
         LoginInfo li = new LoginInfo();
         li.name = br.ReadString();
         int length = br.ReadInt32();
         li.password = new EncryptedPassword(br.ReadBytes(length),Key);
         return li;
     }
 }
Example #3
0
        /// <summary>
        /// Generates a new EncryptionKey
        /// </summary>
        /// <returns>
        /// The key.
        /// </returns>
        public static EncryptKey GenerateKey()
        {
            //Create key
            EncryptKey key = new EncryptKey();

            //create encryption algoritm to make the keys
            using(RijndaelManaged rj = new RijndaelManaged())
            {
                //Generate key data
                rj.GenerateKey();
                rj.GenerateIV();

                key.data = rj.Key;
                key.dataIV = rj.IV;
                //dispose encryptor
            }
            //Return key
            return key;
        }
Example #4
0
        /// <summary>
        /// Load a serialized key
        /// </summary>
        /// <param name='Data'>
        /// The serialized key
        /// </param>
        public static EncryptKey Load(byte[] Data)
        {
            //Setup var contracts
            Contract.Requires(Data != null && Data.Length > 0, "Data array null or zero");

            //Init stream and main vars
            MemoryStream ms = new MemoryStream(Data);
            BinaryReader br = new BinaryReader(ms);
            byte[] data;
            byte[] dataIV;

            //read data
            int dl = br.ReadInt32();
            data = br.ReadBytes(dl);
            dl = br.ReadInt32();
            dataIV = br.ReadBytes(dl);

            //close stream
            br.Close();

            //Create instance
            EncryptKey key = new EncryptKey();
            key.data = data;
            key.dataIV = dataIV;

            //return key
            return key;
        }
Example #5
0
 /// <summary>
 /// Initializes the variables in the class.
 /// </summary>
 static LoginManager()
 {
     logins = new List<LoginInfo>();
     key = EncryptKey.GenerateKey();
 }
Example #6
0
        /// <summary>
        /// Loads login info objects from a folder.
        /// </summary>
        /// <param name='Path'>
        /// Path.
        /// </param>
        public static void LoadFromFolder(string Path)
        {
            Contract.Requires(Directory.Exists(Path));
            //Load key
            string[] KeyFiles = Directory.GetFiles (Path,"*.key");
            if (KeyFiles.Length == 1) {
                key = EncryptKey.Load(File.ReadAllBytes(KeyFiles[0]));
            }

            //List files
            var LoginFiles = Directory.GetFiles(Path,"*.login");

            //Clear list
            logins.Clear();

            //Load each file
            foreach (var file in LoginFiles) {
                logins.Add(LoginInfo.Load(File.ReadAllBytes(file),key));
            }
        }
Example #7
0
 public EncryptedPassword(byte[] data, EncryptKey Key)
 {
     passwordData = data;
     key = Key;
 }