private string RODecryptString(string inStr, string inKey) { try { var hasher = new ROHasher(DesMD5); byte[] encryptedData = Convert.FromBase64String(inStr); byte ver = encryptedData[0]; int ivSize = 0; if (ver == 1) { ivSize = 8; } else if (ver == 2) { ivSize = 16; } else { throw new Exception("unsupported encryption version"); } SymmetricAlgorithm cipher = ver == 1 ? (SymmetricAlgorithm) new TripleDESCryptoServiceProvider() : (SymmetricAlgorithm) new AesCryptoServiceProvider(); foreach (string key in (inKey ?? "") .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Where(s => !string.IsNullOrEmpty(s.Trim())) .Select(s => s.Trim()) ) { try { cipher.IV = encryptedData.Skip(1).Take(ivSize).ToArray(); cipher.Mode = CipherMode.CBC; cipher.Key = hasher.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)).Take(DesMD5 ? 16 : 32).ToArray(); string outStr = UTF8Encoding.UTF8.GetString(cipher.CreateDecryptor().TransformFinalBlock(encryptedData.Skip(1 + ivSize).ToArray(), 0, encryptedData.Length - (1 + ivSize))); return(outStr); } catch { } } throw new Exception("no suitable secret column keys"); } catch { throw; } }
private string RODecryptString(string inStr, string inKey) { try { var hasher = new ROHasher(DesMD5); byte[] encryptedData = Convert.FromBase64String(inStr); byte ver = encryptedData[0]; int ivSize = 0; if (ver == 1) { ivSize = 8; } else if (ver == 2) { ivSize = 16; } else { throw new Exception("unsupported encryption version"); } SymmetricAlgorithm cipher = ver == 1 ? (SymmetricAlgorithm) new TripleDESCryptoServiceProvider() : (SymmetricAlgorithm) new AesCryptoServiceProvider(); try { cipher.IV = encryptedData.Skip(1).Take(ivSize).ToArray(); cipher.Mode = CipherMode.CBC; cipher.Key = hasher.ComputeHash(UTF8Encoding.UTF8.GetBytes(inKey)).Take(DesMD5 ? 16 : 32).ToArray(); string outStr = UTF8Encoding.UTF8.GetString(cipher.CreateDecryptor().TransformFinalBlock(encryptedData.Skip(1 + ivSize).ToArray(), 0, encryptedData.Length - (1 + ivSize))); return(outStr); } catch { return(null); } } catch { return(null); } }
private string ROEncryptString(string inStr, string inKey) { string outStr = string.Empty; RandomNumberGenerator rng = new RNGCryptoServiceProvider(); // general format // base64(version byte + byte[] of IV + encrypted content) + '-' + visible tail portion // version 1 3DES CBC with 8 byte IV // version 2 AES256 CBC with 16 byte IV byte[] ver = new byte[] { (byte)(DesMD5 ? 1 : 2) }; byte[] iv = new byte[DesMD5 ? 8 : 16]; rng.GetBytes(iv); var hasher = new ROHasher(DesMD5); SymmetricAlgorithm cipher = DesMD5 ? (SymmetricAlgorithm) new TripleDESCryptoServiceProvider() : (SymmetricAlgorithm) new AesCryptoServiceProvider(); cipher.Mode = CipherMode.CBC; cipher.IV = iv; cipher.Key = hasher.ComputeHash(UTF8Encoding.UTF8.GetBytes(inKey)).Take(DesMD5 ? 16 : 32).ToArray(); byte[] encryptedBlock = cipher.CreateEncryptor().TransformFinalBlock(UTF8Encoding.UTF8.GetBytes(inStr), 0, UTF8Encoding.UTF8.GetBytes(inStr).Length); outStr = Convert.ToBase64String(ver.Concat(iv).Concat(encryptedBlock).ToArray()); return(outStr); }