Beispiel #1
0
 internal string   convertPassword(string password, AppConst.MPE_Flag flag)
 {
     if (flag.HasFlag(AppConst.MPE_Flag.isRemoveSpace))
     {
         password = Util.toRemoveInnerSpaces(password);
     }
     if (flag.HasFlag(AppConst.MPE_Flag.isIgnoreCase))
     {
         password = Util.toUpperCase(password);
     }
     if (flag.HasFlag(AppConst.MPE_Flag.isIgnoreZenHan))
     {
         password = Util.toZenkaku(password);
     }
     if (flag.HasFlag(AppConst.MPE_Flag.isIgnoreHiraKata))
     {
         password = Util.toHiragana(password);
     }
     return(password);
 }
Beispiel #2
0
        /**
         * First zip the file and then encrypt it.
         * (J) 最初にファイルをzip圧縮し、その後に暗号化する。
         *
         * encTargetList : Files or directories for encryption.
         * hints : question's hint
         * passwordLists : password List for hint.
         * nRequiredPasswords :  number of passwords are required for decryption.
         * flag : Encryption flag
         * outputFS : output file stream.
         */
        internal static void Encrypt(
            List <string> encTargetList,
            string[] hints,
            List <string>[] passwordLists,
            int nRequiredPasswords,
            AppConst.MPE_Flag flag,
            FileStream outputFS,
            IProgress <string> progress = null)
        {
            var tmpFileCompress = System.IO.Path.GetRandomFileName();   // temporary file name for zip file.

            try
            {
                progress?.Report("(1/4) creating archive.");
                // zip
                CompressionLevel compressionLevel = flag.HasFlag(AppConst.MPE_Flag.isNoCompress) ? CompressionLevel.NoCompression : CompressionLevel.Optimal;
                using (FileStream zipFS = File.Create(tmpFileCompress))
                {
                    using (ZipArchive archive = new ZipArchive(zipFS, ZipArchiveMode.Create))
                    {
                        foreach (var item in encTargetList)
                        {
                            string rootPathFile = item.ToString();
                            if (File.Exists(rootPathFile))
                            {   // Add the file as it is.
                                //(J) ファイルはそのまま追加
                                archive.CreateEntryFromFile(rootPathFile, System.IO.Path.GetFileName(rootPathFile), compressionLevel);
                            }
                            else if (Directory.Exists(rootPathFile))
                            {   // Add directory recursively
                                //(J) ディレクトリは再帰的に追加
                                string   parentPath = System.IO.Directory.GetParent(rootPathFile).ToString();
                                string[] pathFiles  = Directory.GetFileSystemEntries(rootPathFile, "*", System.IO.SearchOption.AllDirectories);
                                if (pathFiles.Length == 0)
                                {   // Empty directory added only entry.
                                    //(J) 空ディレクトリはエントリのみ追加
                                    archive.CreateEntry(System.IO.Path.GetFullPath(rootPathFile).Substring(parentPath.Length + 1) + "/");
                                }
                                foreach (var pathFile in pathFiles)
                                {
                                    string entryPath = pathFile.Substring(parentPath.Length + 1);   // Archive the specified directory as root. (J)指定したディレクトリをルートとしてアーカイブ
                                    if (File.Exists(pathFile))
                                    {
                                        archive.CreateEntryFromFile(pathFile, entryPath, compressionLevel);
                                    }
                                    else if (Directory.Exists(pathFile))
                                    {
                                        archive.CreateEntry(entryPath + "/");
                                    }
                                }
                            }
                        }
                    }
                }

                // File is not properly terminated without Dispose().
                // Dispose() archive, zipFS will also be closed(), so open it again.
                //(J) 一旦 archive を Dispose() しないと正しくファイルが終端化されない。
                //(J) archive を Dispose() すると zipFS も Close()されてしまうため、開きなおす。

                progress?.Report("(2/4) encrypting archive.");
                var tmpFileEncrypted = System.IO.Path.GetRandomFileName();  // temporary file name for encrypted file.
                try
                {
                    // encryption
                    RNGCryptoServiceProvider r = new RNGCryptoServiceProvider();
                    byte[] dataKey             = new byte[Crypto.AES_KEY_SIZE_BYTE]; // Encryption key for data.
                    r.GetBytes(dataKey);

                    using (FileStream zipFS = File.Open(tmpFileCompress, FileMode.Open))
                    {
                        using (FileStream encryptedFS = File.Create(tmpFileEncrypted))
                        {
                            Crypto.encryptStream(zipFS, encryptedFS, dataKey);
                        }
                    }
                    // Since it became unnecessary, try to delete here
                    //(J) 不要になったのでここで削除を試みる
                    File.Delete(tmpFileCompress);

                    using (FileStream encryptedFS = File.Open(tmpFileEncrypted, FileMode.Open))
                    {
                        Encrypt_2(encryptedFS, hints, passwordLists, nRequiredPasswords, flag, r, dataKey, outputFS, progress);
                    }
                    progress?.Report("(4/4) finished.");
                }
                finally
                {
                    File.Delete(tmpFileEncrypted);
                }
            }
            finally
            {
                File.Delete(tmpFileCompress);
            }
        }