/// <summary> /// Moves the given code-gen source code definition to another file /// </summary> /// <returns>The full paths to the created source code files </returns> public static string[] MoveType(CgType cgType, string outputDirectory = null, CgClassModifier typeModifier = CgClassModifier.AsIs) { if (cgType == null) { return(new string[0]); } outputDirectory = outputDirectory ?? NfSettings.AppData; //TODO - this is inconsistent where enclosure is one but src files are many var srcCodeFile = cgType.GetMySrcCodeFiles()?.FirstOrDefault() ?? Path.Combine(NfSettings.AppData, cgType.Name + Settings.LangStyle.FileExtension); var fileName = cgType.Name; var fileExtension = Path.GetExtension(srcCodeFile); var outFilePath = Path.Combine(outputDirectory, fileName + fileExtension); //don't overwrite whatever is already there if (File.Exists(outFilePath)) { outFilePath = Path.Combine(outputDirectory, fileName + NfString.GetNfRandomName() + fileExtension); } var newlines = new List <string>(); var hasNs = !string.IsNullOrWhiteSpace(cgType.Namespace); if (hasNs) { newlines.AddRange(Settings.LangStyle.ToNamespaceDecl(cgType).ConvertToLf().Split('\n')); } if (File.Exists(srcCodeFile)) { var start = cgType.GetMyStartEnclosure().Item1; var end = cgType.GetMyEndEnclosure().Item1; var srcLines = File.ReadAllLines(srcCodeFile); newlines.AddRange(srcLines.Skip(start - 1).Take(end + 1 - start)); } else { var genLines = typeModifier == CgClassModifier.AsInterface ? Settings.LangStyle.ToInterface(cgType) : Settings.LangStyle.ToClass(cgType, CgAccessModifier.Public, typeModifier); newlines.AddRange(genLines.ConvertToLf().Split('\n')); } if (hasNs) { newlines.Add(Settings.LangStyle.GetEnclosureCloseToken(null)); } //write the type def to file File.WriteAllLines(outFilePath, newlines, Encoding.UTF8); return(new[] { outFilePath }); }
/// <summary> /// Decrypts a file which was encrypted by its sister method <see cref="EncryptFile"/> /// </summary> /// <param name="path"></param> /// <param name="certCerPath"></param> /// <param name="certPassword"></param> public static void DecryptFile(string path, string certCerPath, string certPassword) { //test inputs TestEnDeCryptInputs(path, certCerPath, certPassword); //import the cert var cert = new X509Certificate2(); cert.Import(certCerPath, certPassword, X509KeyStorageFlags.Exportable); var privKey = cert.PrivateKey as RSACryptoServiceProvider; if (privKey == null) { throw new ItsDeadJim("The private could not be resolved."); } var plainTextFile = Path.Combine(Path.GetDirectoryName(path) ?? Environment.CurrentDirectory, Path.GetFileNameWithoutExtension(path) ?? NfString.GetNfRandomName()); using (var aes = new AesManaged()) { aes.KeySize = 256; aes.BlockSize = 128; aes.Mode = CipherMode.CBC; var bufferK = new byte[4]; var bufferIv = new byte[4]; using (var inFs = new FileStream(path, FileMode.Open)) { inFs.Seek(0, SeekOrigin.Begin); inFs.Read(bufferK, 0, 3); inFs.Seek(4, SeekOrigin.Begin); inFs.Read(bufferIv, 0, 3); var lenK = BitConverter.ToInt32(bufferK, 0); var lenIv = BitConverter.ToInt32(bufferIv, 0); var startC = lenK + lenIv + 8; var enKey = new byte[lenK]; var iv = new byte[lenIv]; inFs.Seek(8, SeekOrigin.Begin); inFs.Read(enKey, 0, lenK); inFs.Seek(8 + lenK, SeekOrigin.Begin); inFs.Read(iv, 0, lenIv); var decKey = privKey.Decrypt(enKey, false); using (var trans = aes.CreateDecryptor(decKey, iv)) { using (var outFs = new FileStream(plainTextFile, FileMode.Create)) { var blockSz = aes.BlockSize / 8; var data = new byte[blockSz]; inFs.Seek(startC, SeekOrigin.Begin); using (var outCrypto = new CryptoStream(outFs, trans, CryptoStreamMode.Write)) { var count = 0; do { count = inFs.Read(data, 0, blockSz); outCrypto.Write(data, 0, count); } while (count > 0); outCrypto.FlushFinalBlock(); outCrypto.Close(); } outFs.Close(); } inFs.Close(); } // end decryptor } // end fs in } //end aes managed }