Ejemplo 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);
        }
Ejemplo 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);
        }