我有一个 exe 文件,当您以文本模式打开时,该文件
SUBID:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$在数字签名之前的某处具有此数据。
我想$用一些字符串值替换这些值subID,比如SUBID:1234567890ABCDEFGHIJKLM不破坏文件的数字签名。(手动进行此替换不会破坏数字签名)。
我现在正在做的是:
- 在字符串变量中读取 exe 文件的内容
FileContents。 - 获取 中的
SUBID索引FileContents, - 子串
Filecontents从 0indexOf ("SUBID")到一个新的字符串copyStr - 附加
"SUBID:"+subID在copyStr FileContents然后在copyStr中追加剩下的内容最后将字符串转换回字节形式并制作exe如下:
String fileContent = readFile("MySetup.exe", Charset.defaultCharset()); int index_subid = fileContent.indexOf("SUBID"); String copyStr =fileContent.substring(0, index_subid); copyStr += "SUBID:" + subID; copyStr += fileContent.substring(index_subid + "SUBID:".length()+ subID.length()); InputStream is = new ByteArrayInputStream(copyStr.getBytes());//函数readFile()
public static String readFile(String path, Charset encoding) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return encoding.decode(ByteBuffer.wrap(encoded)).toString(); }
但问题是虽然数据的替换发生正确,但我生成的 exe 已损坏,可能是因为从字节到文本的转换。
那么有没有其他方法可以替换SUBIDexe中标记后的数据?