Exemplo n.º 1
0
        public string encrypt(string text, string password)
        {
            core   c = new core();
            random r = new random();

            byte[] baPwd = Encoding.UTF8.GetBytes(password);

            // Hash the password with SHA256
            byte[] baPwdHash = SHA256Managed.Create().ComputeHash(baPwd);

            byte[] baText = Encoding.UTF8.GetBytes(text);

            byte[] baSalt      = r.GetRandomBytes();
            byte[] baEncrypted = new byte[baSalt.Length + baText.Length];

            // Combine Salt + Text
            for (int i = 0; i < baSalt.Length; i++)
            {
                baEncrypted[i] = baSalt[i];
            }
            for (int i = 0; i < baText.Length; i++)
            {
                baEncrypted[i + baSalt.Length] = baText[i];
            }

            baEncrypted = c.encrypt(baEncrypted, baPwdHash);

            string result = Convert.ToBase64String(baEncrypted);

            return(result);
        }
Exemplo n.º 2
0
        public string decrypt(string text, string password)
        {
            core   c = new core();
            random r = new random();

            byte[] baPwd = Encoding.UTF8.GetBytes(password);

            // Hash the password with SHA256
            byte[] baPwdHash = SHA256Managed.Create().ComputeHash(baPwd);

            byte[] baText = Convert.FromBase64String(text);

            byte[] baDecrypted = c.decrypt(baText, baPwdHash);

            // Remove salt
            int saltLength = r.GetSaltLength();

            byte[] baResult = new byte[baDecrypted.Length - saltLength];
            for (int i = 0; i < baResult.Length; i++)
            {
                baResult[i] = baDecrypted[i + saltLength];
            }

            string result = Encoding.UTF8.GetString(baResult);

            return(result);
        }
Exemplo n.º 3
0
        public void EncryptFile(string inputFile, string encryptedOutputFile, string password)
        {
            core c = new core();

            byte[] bytesToBeEncrypted = File.ReadAllBytes(inputFile);
            byte[] passwordBytes      = Encoding.UTF8.GetBytes(password);

            // Hash the password with SHA256
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            byte[] bytesEncrypted = c.encrypt(bytesToBeEncrypted, passwordBytes);

            File.WriteAllBytes(encryptedOutputFile, bytesEncrypted);
        }
Exemplo n.º 4
0
        public string decrypt(string input, string password)
        {
            core c = new core();

            // Get the bytes of the string
            byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
            byte[] passwordBytes      = Encoding.UTF8.GetBytes(password);
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            byte[] bytesDecrypted = c.decrypt(bytesToBeDecrypted, passwordBytes);

            string result = Encoding.UTF8.GetString(bytesDecrypted);

            return(result);
        }
Exemplo n.º 5
0
        public string encrypt(string input, string password)
        {
            core c = new core();

            // Get the bytes of the string
            byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
            byte[] passwordBytes      = Encoding.UTF8.GetBytes(password);

            // Hash the password with SHA256
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            byte[] bytesEncrypted = c.encrypt(bytesToBeEncrypted, passwordBytes);

            string result = Convert.ToBase64String(bytesEncrypted);

            return(result);
        }