【发布时间】:2019-02-01 12:20:14
【问题描述】:
我正在开发一个简单的应用程序,它应该对命令行给出的字符串进行加密,然后将其写入注册表。不幸的是,如果密码包含像 " & < > | 这样的特殊字符,我会遇到麻烦。例如:
Encrypt.exe /Password:Password /reg --> works fine
Encrypt.exe /Password:Password"&<>| /reg --> the password variable contains: Password&<>| /reg which is wrong
Encrypt.exe /Password:Password&<>| --> this outputs: "> was unexpected at this time." but no idea where this text is coming from
Encrypt.exe /Password:"Password&<>|" --> this works, but note that the password can't contains a " char and the encrypted/decrypted string will contain a " at the beginning and at the end
所以我的问题是,如何将字符串 Password&<>| 正确传递给我的应用程序?
【问题讨论】:
-
不是一个真正的 C++ 问题。而是一个 Powershell 或 CMD.exe 问题。
-
在参数前后使用双引号并在密码中使用反斜杠:
"Password\"&<>|"->Password"&<>| -
@Tezirg:是的,这可行,但关键是,我不知道密码。用户必须输入它。
-
注意,对于 CMD,转义规则对于在命令行上键入的命令与在
*.BAT或*.CMD脚本中执行的命令略有不同。 (不确定这在这种情况下是否相关。) -
假设这是 VC++,那么在将命令行解析为
argv数组时使用双引号来忽略空格。在这种情况下,对应的argv元素中不会出现双引号,即argv[1]将是/Password:Password&<>|。通过用反斜杠将其转义来包含文字引号。这些是您必须记录的唯一规则。
标签: batch-file arguments