/// <summary> /// ファイルを暗号化する /// </summary> /// <param name="ifs"></param> /// <param name="ofs"></param> /// <param name="password"></param> public static void Encrypt(Stream ifs, Stream ofs, string password) { var keyBytes = FileEncryptor.GetKeyBytes(password); using var aes = new AesManaged() { BlockSize = 128, KeySize = 128, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7, Key = keyBytes, }; aes.GenerateIV(); ofs.Write(aes.IV, 0, aes.IV.Length); using var enc = aes.CreateEncryptor(); using var cs = new CryptoStream(ofs, enc, CryptoStreamMode.Write, true); ifs.CopyTo(cs, 32 * 1024); }
/// <summary> /// ファイルを復号化する /// </summary> /// <param name="ifs"></param> /// <param name="ofs"></param> /// <param name="password"></param> public static void Decrypt(Stream ifs, Stream ofs, string password) { var keyBytes = FileEncryptor.GetKeyBytes(password); using var aes = new AesManaged() { BlockSize = 128, KeySize = 128, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7, Key = keyBytes, }; var ivBytes = new byte[aes.IV.Length]; ifs.Read(ivBytes, 0, ivBytes.Length); aes.IV = ivBytes; /* Create AES Decryptor */ using var encrypt = aes.CreateDecryptor(); using var cs = new CryptoStream(ofs, encrypt, CryptoStreamMode.Write, true); ifs.CopyTo(cs, 32 * 1024); }
/// <summary> /// APIキーのロード /// </summary> /// <returns></returns> private void LoadApiKey(Stream outStream) { using var apiKeySt = new MemoryStream(Properties.Resources.api); FileEncryptor.Decrypt(apiKeySt, outStream, "n1xDVuFqSN"); outStream.Seek(0, SeekOrigin.Begin); }