Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

powershell - 通过CMD和PowerShell进行管道传输时,行为和输出不同(Different behaviour and output when piping through CMD and PowerShell)

I am trying to pipe the content of a file to a simple ASCII symmetrical encryption program i made.

(我试图将文件的内容传输到我制作的简单ASCII对称加密程序中。)

It's a simple program that reads input from STDIN and adds or subtracts a certain value (224) to each byte of the input.

(这是一个简单的程序,可从STDIN读取输入,并对输入的每个字节加或减某个值(224)。)

For example: if the first byte is 4 and we want to encrypt, then it becomes 228. If it exceeds 255, the program just performs some modulo.

(例如:如果第一个字节为4,而我们要加密,则它变为228。如果它超过255,则程序仅执行一些模运算。)

This is the output I get with cmd (test.txt contains "this is a test"):

(这是我通过cmd获得的输出(test.txt包含“这是测试”):)

    type .est.txt | .Crypt.exe --encrypt | .Crypt.exe --decrypt
    this is a test

It also works the other way, thus it is a symmetrical encryption algorithm

(它也以其他方式起作用,因此它是一种对称加密算法)

    type .est.txt | .Crypt.exe --decrypt | .Crypt.exe --encrypt
    this is a test

But, the behaviour on PowerShell is different.

(但是,PowerShell上的行为有所不同。)

When encrypting first, I get:

(第一次加密时,我得到:)

    type .est.txt | .Crypt.exe --encrypt | .Crypt.exe --decrypt
    this is a test_*

And that is what I get when decrypting first:

(这就是我首先解密时得到的:)

屏幕截图

Maybe is an encoding problem.

(也许是编码问题。)

Thanks in advance.

(提前致谢。)

  ask by Adel M. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The type in cmd.exe is not the same type in PowerShell;

(的typecmd.exe是不一样的type在PowerShell中;)

in cmd.exe it's a built-in command whereas in PowerShell it's an alias for Get-Content ...

(在cmd.exe它是一个内置命令,而在PowerShell中,它是Get-Content的别名...)

PS> Get-Command type

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           type -> Get-Content

I'm a little confused when you say it's specifically an ASCII encryption program even though it's operating on bytes, but, regardless, I would try replacing this...

(当您说它特别是ASCII加密程序(即使它对字节进行操作)时,我有些困惑,但是无论如何,我都将尝试替换它。)

type .est.txt

...with either this...

(...或者这...)

Get-Content .est.txt -Encoding Byte

...or this...

(...或这个...)

[System.IO.File]::ReadAllBytes('.est.txt')

In PowerShell 6.0 there is an -AsByteStream parameter...

(在PowerShell 6.0中,有-AsByteStream参数...)

Get-Content .est.txt -AsByteStream

...that would probably be most efficient of all, although I believe with external programs PowerShell buffers the entire stdin/stdout before passing it along so it might not matter (I think Using ffmpeg and ffplay piped together in PowerShell is the question I found when I recently encountered that issue).

(...这可能是最有效的,尽管我相信使用外部程序,PowerShell会在传递整个stdin / stdout之前先缓冲整个stdin / stdout,所以这可能无关紧要(我认为在PowerShell中使用ffmpeg和ffplay一起管道传输是我发现的问题当我最近遇到该问题时)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...