gpt4 book ai didi

c# - 连接时字符串溢出

转载 作者:行者123 更新时间:2023-11-30 14:15:34 25 4
gpt4 key购买 nike

我正在尝试制作一个程序打包器,但我总是失败,因为当我连接三个字符串(一个包含源前缀,一个包含可执行内容,另一个包含源后缀)时,内容溢出到后缀中。代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.IO.Compression;

namespace ProgramPacker
{
public partial class Form1 : Form
{
static string prefix = @"using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ProgramPacker
{
class Program
{
static string inside = @" + "\"";
static string suffix = "\";\n" + @"static void Main(string[] args)
{
string temp = Path.GetRandomFileName() +" + "\"" + @".exe" + "\"" + @";
BinaryWriter sw = new BinaryWriter(new FileStream(temp, FileMode.Create));
sw.Write(inside);
sw.Flush();
sw.Close();
System.Diagnostics.Process.Start(temp);
}
}
}";
public string code = "";
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
BinaryReader br = new BinaryReader(new FileStream(openFileDialog1.FileName, FileMode.Open));
byte[] data = new byte[br.BaseStream.Length];
br.Read(data, 0, (int)br.BaseStream.Length);
br.Close();
string inside = Encoding.UTF7.GetString(data);
code = string.Concat(prefix, string.Concat(inside, suffix));
}

private void button2_Click(object sender, EventArgs e)
{
Console.Write(code);
CSharpCodeProvider cs = new CSharpCodeProvider();
ICodeCompiler compile = cs.CreateCompiler();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = false;
param.ReferencedAssemblies.Add("mscorlib.dll");
param.ReferencedAssemblies.Add("System.dll");
param.ReferencedAssemblies.Add("System.Core.dll");
param.GenerateExecutable = true;
param.OutputAssembly = Environment.CurrentDirectory + "/a.exe";
param.WarningLevel = 4;
CompilerResults comp = compile.CompileAssemblyFromSource(param, code);
foreach (CompilerError error in comp.Errors)
{
MessageBox.Show(error.Line + " " + error.Column + " " + error.ErrorText);
}
MessageBox.Show(comp.PathToAssembly);
MessageBox.Show("Finish!");
}
}

输出:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ProgramPacker
{
class Program
{
static string inside = @"?ãÎoüoy±ôãøÿ?ÿQÔh¦Rt!?^ÿyóë=G:Fÿ»??¿Ç/}òKÿ?úõßû3õMù?c·?Äûòëoª?À_Û>LÖïá^öÿ·õ©üüòcÊ?ª??÷Ô?î:^ȯÏäG¶?ù ?ñË?oñëy:ôã7??ò@ÚyN?w¿=?ëÆ÷ëÛßTëºÓ»yßüø]áå{ö/àïªêL©Oÿ;ú5rù% ä¸?1)Ï?¥?y®ÿ]0QL8û×ÖGòów±?øÿèü[?ª~éá¿ÿó£üüJ~ܹ
Êw¡h??/?úçeñÈ_?¿?ü½L^ywü7?,ùÅû¿ß£ó÷w?É~Ç????ê?ÿQÌyç¿??¹Cö×vZ__>?? ûïx?_ü"õ!ì; ;ùõåÇ?ú3*­¿ @ÿV?èÿ¿Ë??/½P¶y?â¡??ùñÿÑæä/A¶_ò?v??ÿ?ÿQtÄx÷w?O'N?u$ÿk÷U«yÆò?£.Y?ùw(?ßÔ6ÿ]2ÿeUÆò¿?ضö·ßO~ü#*y£Jæ·Ä w yóYëï´
õ}y¡ä¨ù÷YPdú?Z©Óé¼R?æg?ÀSyà쬽Éÿ­ûÁ?ym>ut?ÿA??>?¿[ôçF ê´)9ß9xÿ£¿?
åùïyí ÁTlù
-ùñ?Òÿ·!·
E?£üO5÷]çºy?7Áè¿?Zô?(å§
#ü¥úõ®?o«âû]0Èßä7¿à??¿õ?d?ѵ;Jü_K?úAMìT>Àü1mR0sâ¿ê¨õ{äö×ó??¿.úÿõ½Eä?ÔïÿÎÿ±³v'ÿûìw ?»oÃø1±WtB
É¡wì&øuå£Îÿ?Éw?|úWÿ=ö÷_ÿ·?y®YWöwû¿ßß?Öe'û?%?yß/×Àî-yÿ.?8åóù?ûÇÚ6ÿÂßøï?Áàï?{8Zy?¿Å5yø ÇpÏ9=ó»üÑ>Õ?Èÿ?Y¿,?ëÿ?Êçÿì_}E?÷Ûú|î$ò÷ø}Jÿ^Êtò¿?@á|ò{Ø`<ªÿQ?QLæ½õo)¿ü&¿áÂÿ¼ó¿_÷7yMå8)¿Ùo'¿õ±üü=~#LÚ¿ó§¢Ë÷üO?Æ?å÷WJÍ$¾¸øW?A?Òÿ(09Áÿà³_ëÿù3l?SWYµßô÷¤~?ÀG¿ñ
??Às%ÍyÑ;Ò¸!ÿ?_Iÿ¼¼í/Ôrö
?T´ôÑùyǹßE~üV?ñ¿C~VßùuÿG X8|._¼Å_:Æü-?îü:?øì?|W ¤ÃÓW01?$å×?Û#ùIÿû·1:4û½ü._y^òÙNó?º1?ßIÍ?ÿ¿ß_~|ÖùX?@?në_B?¡µ~_×ãÿhãèïØÏßì¡yïôk_y;HÿÛyÿàâ/sÀ??? $ìw0Jüßü«@?ûo?¿îËX
´úÏ?Æ?Y>øçÿFpyÅÿèËöyK¡²Ì`}ù½Ò¿íÿÀñn"?Í?ëOëi»ò¹Î~çÿ?_òüîÿh£ôáêDøÏÿ¶?ç·fy ÄúçÿÖÿ?yÿ?¿?ã0ÿ¿Ë÷åå?ÿQ|ü/ü9¿?÷×Hè×ÉÏ#õ??æ?ÿâ?x?ß?5µ}~Øy¾ß^øè{¿
y{íü;Òÿ¿óú7¤¡î?» yû?ÀÓøiP?ÿÀ_ôK?Gëªíá@??ú%íwS×ñ¿¨#Û¦ÿÿÛªy\]¼÷Z~yR~ü¶ÿ£hû???ÇãÿQVèÿ©?÷{O?Gë¬ ??¡ø\«L` æcëí¿ ½?»?ü?oë¼Ätÿ>ò÷£­_¯ÖiÄ?¯ÿÓ¿÷Ï?(c¦äàéûèó?ü£´çÕè4üy_
r?qÆ¿ø'?KÿÀ_ó×xùkÔ¿FõkÌ~õ¯1y5Ú_ã'??n~âר~å¯ñkü»¿Æø×رÿÿ5~?_ã7ø5~Í_ã?Z4ÔrñkL~ò׸?5Òßûÿ??ñïñnQ¦?yYÕò³vÇ;¥ùrZÍ?åÅg}õæÙöÁGiÓfËYVVËü³®óæ£ßãè7NgM?/&åuJrustInfo>
</assembly>
;
}
}
}

有什么帮助吗?

编辑:为什么每个人都反对?我刚刚问了一个问题。

最佳答案

不要使用纯 UTF7 或 UTF8 字符串,而是使用 Base64 编码。

// given: byte[] data = new byte[...]
string inside = System.Convert.ToBase64String(data);
code = string.Concat(prefix, string.Concat(inside, suffix));

// in your target code
sw.Write(System.Convert.FromBase64String(inside));

关于c# - 连接时字符串溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9828763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com