/// <summary> /// Encrypt/Decypt the given cryptData with the given password /// </summary> /// <param name="algoType">Algorithm type</param> /// <param name="cryptData">data to crypt</param> /// <param name="offset">offset of cryptData for crypt</param> /// <param name="count">length for crypt</param> /// <param name="cryptPwd">password string</param> /// <param name="keySalt">salt string</param> /// <param name="cryptType">crypt type</param> /// <returns>encrypted/decrypted data</returns> /// <remarks>if keySalt is null, then default keySalt is used</remarks> public static byte[] GetCrypt(CryptAlgo algoType, byte[] cryptData, int offset, int count, string cryptPwd, byte[] keySalt, CryptType cryptType) { byte[] retBytes = null; switch (algoType) { case CryptAlgo.Aes: retBytes = AesCrypt.GetCrypt(cryptData, offset, count, cryptPwd, keySalt, cryptType); break; case CryptAlgo.Rijndael: retBytes = RijndaelCrypt.GetCrypt(cryptData, offset, count, cryptPwd, keySalt, cryptType); break; } return(retBytes); }
/// <summary> /// Encrypt/Decypt the given cryptData with the given password /// </summary> /// <param name="cryptData">data to crypt</param> /// <param name="cryptPwd">password string</param> /// <param name="cryptType">crypt type</param> /// <returns>encrypted/decrypted data</returns> public static byte[] GetCrypt(CryptAlgo algoType, byte[] cryptData, string cryptPwd, CryptType cryptType) { byte[] retBytes = null; switch (algoType) { case CryptAlgo.Aes: retBytes = AesCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType); break; case CryptAlgo.Rijndael: retBytes = RijndaelCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType); break; } return(retBytes); }
/// <summary> /// Encrypt/Decypt the given cryptData string with the given password /// </summary> /// <param name="cryptData">string data to encrypt</param> /// <param name="cryptPwd">password string</param> /// <param name="cryptType">crypt type</param> /// <returns>encrypted/decrypted data</returns> public static string GetCrypt(CryptAlgo algoType, string cryptData, string cryptPwd, CryptType cryptType) { string retString = null; switch (algoType) { case CryptAlgo.Aes: retString = AesCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType); break; case CryptAlgo.Rijndael: retString = RijndaelCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType); break; } return(retString); }
/// <summary> /// Encrypt/Decypt the given cryptData string with the given password /// </summary> /// <param name="cryptData">string data to encrypt</param> /// <param name="cryptPwd">password string</param> /// <param name="cryptType">crypt type</param> /// <returns>encrypted/decrypted data</returns> public static string GetCrypt(CryptAlgo algoType,string cryptData, string cryptPwd, CryptType cryptType) { string retString = null; switch (algoType) { case CryptAlgo.Aes: retString = AesCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType); break; case CryptAlgo.Rijndael: retString = RijndaelCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType); break; } return retString; }
/// <summary> /// Encrypt/Decypt the given cryptData with the given password /// </summary> /// <param name="algoType">Algorithm type</param> /// <param name="cryptData">data to crypt</param> /// <param name="offset">offset of cryptData for crypt</param> /// <param name="count">length for crypt</param> /// <param name="cryptPwd">password string</param> /// <param name="keySalt">salt string</param> /// <param name="cryptType">crypt type</param> /// <returns>encrypted/decrypted data</returns> /// <remarks>if keySalt is null, then default keySalt is used</remarks> public static byte[] GetCrypt(CryptAlgo algoType, byte[] cryptData,int offset, int count, string cryptPwd, byte[] keySalt, CryptType cryptType) { byte[] retBytes = null; switch (algoType) { case CryptAlgo.Aes: retBytes = AesCrypt.GetCrypt(cryptData,offset,count, cryptPwd, keySalt, cryptType); break; case CryptAlgo.Rijndael: retBytes = RijndaelCrypt.GetCrypt(cryptData,offset,count, cryptPwd, keySalt, cryptType); break; } return retBytes; }
/// <summary> /// Encrypt/Decypt the given cryptData with the given password /// </summary> /// <param name="cryptData">data to crypt</param> /// <param name="cryptPwd">password string</param> /// <param name="cryptType">crypt type</param> /// <returns>encrypted/decrypted data</returns> public static byte[] GetCrypt(CryptAlgo algoType, byte[] cryptData, string cryptPwd, CryptType cryptType) { byte[] retBytes = null; switch (algoType) { case CryptAlgo.Aes: retBytes = AesCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType); break; case CryptAlgo.Rijndael: retBytes = RijndaelCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType); break; } return retBytes; }
public object Crypt(CryptType whichCrypt, CryptAlgo whichAlgo, string input, string password) { if (string.IsNullOrEmpty(input)) { throw new ArgumentNullException("input"); } if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException("password"); } SymmetricAlgorithm cipher; try { switch (whichAlgo) { case CryptAlgo.DES: cipher = new DESCryptoServiceProvider(); break; case CryptAlgo.TDES: cipher = new TripleDESCryptoServiceProvider(); break; case CryptAlgo.AES128: cipher = new AesManaged(); cipher.KeySize = 128; // 16 bit key if (IV == null) { cipher.GenerateIV(); IV = cipher.IV; } else { cipher.IV = IV; } break; case CryptAlgo.AES192: cipher = new AesManaged(); cipher.KeySize = 192; if (IV == null) { cipher.GenerateIV(); IV = cipher.IV; } else { cipher.IV = IV; } break; case CryptAlgo.AES256: cipher = new AesManaged(); cipher.KeySize = 256; // 32 bit key if (IV == null) { cipher.GenerateIV(); IV = cipher.IV; } else { cipher.IV = IV; } break; default: return(false); // switch shouldn't return } //cipher.Key = UTF8Encoding.UTF8.GetBytes(key); //cipher.Key = Encoding.Unicode.GetBytes(password); if (Salt == null) { Salt = deriveSalt(cipher.KeySize, password); } cipher.Key = deriveKeyFromPass(cipher.KeySize, password, Salt); cipher.Padding = PaddingMode.PKCS7; // change to random data or ISO standard cipher.Mode = CipherMode.CBC; // More research into proper modes needed byte[] _input; // encrypt needs a string, decrypt needs a byte[] byte[] _output; // will contain either ciphertxt or plaintxt ICryptoTransform transformer = null; // object that does actual crypto computation if (whichCrypt == CryptType.Encrypt) { transformer = cipher.CreateEncryptor(cipher.Key, cipher.IV); } else if (whichCrypt == CryptType.Decrypt) { transformer = cipher.CreateDecryptor(cipher.Key, cipher.IV); } if (input is string) { //_input = UTF8Encoding.UTF8.GetBytes(input as string); if (whichCrypt == CryptType.Encrypt) { //_input = UnicodeEncoding.Unicode.GetBytes(input as string); _input = Encoding.UTF8.GetBytes(input as string); //_input = Convert.FromBase64String(input as string); _output = transformer.TransformFinalBlock(_input, 0, _input.Length); transformer.Dispose(); cipher.Clear(); // kill resource //return Convert.ToBase64String(_output); return(Convert.ToBase64String(_output, 0, _output.Length)); } else if (whichCrypt == CryptType.Decrypt) { _input = Convert.FromBase64String(input as string); _output = transformer.TransformFinalBlock(_input, 0, _input.Length); //return Convert.ToBase64String(_input, 0, _input.Length); //return Convert.ToBase64String(_output); //return Encoding.UTF8.GetString(_output); transformer.Dispose(); cipher.Clear(); // kill resource return(Encoding.UTF8.GetString(_output)); } } //else if (input is byte[]) //{ // _output = transformer.TransformFinalBlock(input as byte[], 0, (input as byte[]).Length); // return _output; //} return(false); // failed } catch (Exception ex) { //return ex.Message; return("Invalid key! " + ex.Message); } }