Untitled

 avatar
unknown
plain_text
17 days ago
2.3 kB
3
Indexable
       
 public static string DecryptWithKey(byte[] encryptedData, byte[] MasterKey)
        {

            if (encryptedData == null || encryptedData.Length < 31)
            {
               // MessageBox.Show("\"Encrypted data is too short\"");
                return "";
            }
              
            if (MasterKey == null || (MasterKey.Length != 16 && MasterKey.Length != 24 && MasterKey.Length != 32))
            {
               // MessageBox.Show("Invalid master key length");
                return "";
            }

            byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // IV 12 bytes

            //trim first 3 bytes(signature "v10") and take 12 bytes after signature.
            Array.Copy(encryptedData, 3, iv, 0, 12);

            try
            {
                //encryptedData without IV
                byte[] Buffer = new byte[encryptedData.Length - 15];
                Array.Copy(encryptedData, 15, Buffer, 0, encryptedData.Length - 15);

                byte[] tag = new byte[16]; //AuthTag
                byte[] data = new byte[Buffer.Length - tag.Length]; //Encrypted Data

                //Last 16 bytes for tag
                Array.Copy(Buffer, Buffer.Length - 16, tag, 0, 16);

                //encrypted password
                Array.Copy(Buffer, 0, data, 0, Buffer.Length - tag.Length);

                AesGcm aesDecryptor = new AesGcm();
                var result = Encoding.UTF8.GetString(aesDecryptor.Decrypt(MasterKey, iv, null, data, tag));

                return result;
            }
            catch (Exception ex)
            {
               // MessageBox.Show(ex.Message);
                return null;
            }
        }

   var pw_data = db.GetValue(2) as byte[];
                            if (pw_data.Length != 0)
                            {
                                password = Encoding.UTF8.GetString(pw_data);
                                if ((password.StartsWith("v10") || password.StartsWith("v11")) && master_key != null)
                                {
                                    password = DecryptWithKey(pw_data, master_key);
                                }
                            }
Leave a Comment