public void CryptoStream_should_show_unencrypted_length_properly(int expectedSizeInBytes) { var data = new byte[expectedSizeInBytes]; new System.Random().NextBytes(data); const string encryptionKey = "Byax1jveejqio9Urcdjw8431iQYKkPg6Ig4OxHdxSAU="; var encryptionKeyBytes = Convert.FromBase64String(encryptionKey); var encryptionSettings = new EncryptionSettings(encryptionKeyBytes, typeof(RijndaelManaged), true, 128); var filename = Guid.NewGuid() + ".txt"; try { using (var stream = new FileStream(filename, FileMode.CreateNew)) using (var cryptoStream = new SeekableCryptoStream(encryptionSettings, encryptionKey, stream)) { cryptoStream.Write(data, 0, data.Length); cryptoStream.Flush(); } using (var stream = new FileStream(filename, FileMode.Open)) using (var cryptoStream = new SeekableCryptoStream(encryptionSettings, encryptionKey, stream)) { Assert.Equal(data.Length, cryptoStream.Length); } } finally { File.Delete(filename); } }
private IEnumerator LoadAssetBundleFromCache(IObserver <SeekableAssetBundle> observer, AssetInfo assetInfo) { var filePath = GetFilePath(assetInfo); var assetBundleInfo = assetInfo.AssetBundle; var assetBundleName = assetBundleInfo.AssetBundleName; var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); var cryptoStream = new SeekableCryptoStream(fileStream, cryptoKey); var bundleLoadRequest = AssetBundle.LoadFromStreamAsync(cryptoStream); while (!bundleLoadRequest.isDone) { yield return(null); } var assetBundle = bundleLoadRequest.assetBundle; // 読み込めなかった時はファイルを削除して次回読み込み時にダウンロードし直す. if (assetBundle == null) { UnloadAsset(assetBundleName); if (cryptoStream != null) { cryptoStream.Dispose(); } if (fileStream != null) { fileStream.Dispose(); } if (File.Exists(filePath)) { File.Delete(filePath); } var builder = new StringBuilder(); builder.Append("Failed to load AssetBundle!").AppendLine(); builder.AppendLine(); builder.AppendFormat("File : {0}", filePath).AppendLine(); builder.AppendFormat("AssetBundleName : {0}", assetBundleName).AppendLine(); builder.AppendFormat("CRC : {0}", assetBundleInfo.CRC).AppendLine(); observer.OnError(new Exception(builder.ToString())); } else { var seekableAssetBundle = new SeekableAssetBundle(assetBundle, fileStream, cryptoStream); observer.OnNext(seekableAssetBundle); } observer.OnCompleted(); }
/// <summary> AssetInfoManifest読み込み </summary> private async Task LoadAssetInfoManifest() { Debug.Log("Load AssetInfoManifest.package."); var projectFolders = ProjectFolders.Instance; assetInfoManifestFilePath = GetAssetInfoManifestFilePath(files); var manifestAssetInfo = AssetInfoManifest.GetManifestAssetInfo(); var assetPath = PathUtility.Combine(projectFolders.ExternalResourcesPath, manifestAssetInfo.ResourcePath); var cryptoKey = GetCryptoKey(); using (var fileStream = new FileStream(assetInfoManifestFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var aesStream = new SeekableCryptoStream(fileStream, cryptoKey)) { var bundleLoadRequest = AssetBundle.LoadFromStreamAsync(aesStream); while (!bundleLoadRequest.isDone) { await Task.Delay(25); } var assetBundle = bundleLoadRequest.assetBundle; var loadAssetAsync = assetBundle.LoadAssetAsync(assetPath, typeof(AssetInfoManifest)); while (!loadAssetAsync.isDone) { await Task.Delay(25); } assetInfoManifest = loadAssetAsync.asset as AssetInfoManifest; assetBundle.Unload(false); } } }
public void Dispose() { if (assetBundle != null) { assetBundle.Unload(false); assetBundle = null; } if (fileStream != null) { fileStream.Dispose(); fileStream = null; } if (cryptoStream != null) { cryptoStream.Dispose(); cryptoStream = null; } GC.SuppressFinalize(this); }
/// <summary> パッケージファイル化(暗号化). </summary> private static async Task CreatePackage(string assetBundleFilePath, string packageFilePath, AesCryptoStreamKey cryptoKey) { // アセットバンドル読み込み. byte[] data = null; using (var fileStream = new FileStream(assetBundleFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { data = new byte[fileStream.Length]; await fileStream.ReadAsync(data, 0, data.Length); } // 暗号化・書き込み. using (var fileStream = new FileStream(packageFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { using (var aesStream = new SeekableCryptoStream(fileStream, cryptoKey)) { aesStream.Write(data, 0, data.Length); } } }
public SeekableAssetBundle(SeekableAssetBundle seekableAssetBundle) { this.assetBundle = seekableAssetBundle.assetBundle; this.fileStream = seekableAssetBundle.fileStream; this.cryptoStream = seekableAssetBundle.cryptoStream; }
public SeekableAssetBundle(AssetBundle assetBundle, FileStream fileStream, SeekableCryptoStream cryptoStream) { this.assetBundle = assetBundle; this.fileStream = fileStream; this.cryptoStream = cryptoStream; }