前言
CertUtil.exe是一個命令行程序,作為證書服務的一部分安裝。可以使用CertUtil.exe轉儲和顯示證書頒發機構(CA)配置信息、配置證書服務、備份和恢復CA組件,以及驗證證書、密鑰對和證書鏈。如果certutil在沒有附加參數的證書頒發機構上運行,它將顯示當前的證書頒發機構配置。如果certutil在非認證中心上運行,則該命令默認運行certutil [-dump]命令。
但是我只用了計算文件hash,以及編碼解碼文件功能,對我來說在Windows上不用安裝額外軟件就能實現這些功能很方便。詳細說明可以參考【官方文檔】
CertUtil計算文件hash
在Windows的cmd下執行CertUtil -hashfile -?
會出現幫助文檔,根據Hash algorithms: MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512
可以得到常用計算文件hash方法,md5和sha1比較常用。
Usage:
CertUtil [Options] -hashfile InFile [HashAlgorithm]
Generate and display cryptographic hash over a file
Options:
-Unicode -- Write redirected output in Unicode
-gmt -- Display times as GMT
-seconds -- Display times with seconds and milliseconds
-v -- Verbose operation
-privatekey -- Display password and private key data
-pin PIN -- Smart Card PIN
-sid WELL_KNOWN_SID_TYPE -- Numeric SID
22 -- Local System
23 -- Local Service
24 -- Network Service
Hash algorithms: MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512
CertUtil -? -- Display a verb list (command list)
CertUtil -hashfile -? -- Display help text for the "hashfile" verb
CertUtil -v -? -- Display all help text for all verbs
計算MD2
CertUtil -hashfile test.txt MD2
MD2 hash of test.txt:
5892bec299b1bae578d5aff607f6eb61
CertUtil: -hashfile command completed successfully.
計算MD4
CertUtil -hashfile test.txt MD4
MD4 hash of test.txt:
a9c5d7b55b8173b39870bcdf919c71b7
CertUtil: -hashfile command completed successfully.
計算MD5
計算md5是比較常用的文件hash方式。
CertUtil -hashfile test.txt MD5
MD5 hash of test.txt:
02877f0b701bec19ed79911ccc7b20e3
CertUtil: -hashfile command completed successfully.
計算SHA1
CertUtil -hashfile test.txt SHA1
SHA1 hash of test.txt:
ae36676c8c6a617ed5d3a84b88c70cd5d9c362a5
CertUtil: -hashfile command completed successfully.
計算SHA256
CertUtil -hashfile test.txt SHA256
SHA256 hash of test.txt:
8df1da6d5c8b7a89ed3da070b652946d9db143fd22e98afdccf5d3d1ac3b29e9
CertUtil: -hashfile command completed successfully.
計算SHA384
CertUtil -hashfile test.txt SHA384
SHA384 hash of test.txt:
bc3cf094587ee7473fd460fac1f37db2d3fd7085cfe8d024cfb004d03824d555f46939bd8920a4db32d9e178da064560
CertUtil: -hashfile command completed successfully.
計算SHA512
CertUtil -hashfile test.txt SHA512
SHA512 hash of test.txt:
3b338d0a80a81f95cb8a654c2bcd9ec6385ee9da785708f8e5bb5eba37934d4c2b745faa823f3598fb56b32a32a833defdce3c195f7e4e3a1fa3b3b69f166969
CertUtil: -hashfile command completed successfully.
文件base64編碼
在Windows的cmd執行CertUtil -encode -?
可以查看編碼文件為base64格式文件。
Usage:
CertUtil [Options] -encode InFile OutFile
Encode file to Base64
Options:
-f -- Force overwrite
-Unicode -- Write redirected output in Unicode
-UnicodeText -- Write output file in Unicode
-gmt -- Display times as GMT
-seconds -- Display times with seconds and milliseconds
-v -- Verbose operation
-privatekey -- Display password and private key data
-pin PIN -- Smart Card PIN
-sid WELL_KNOWN_SID_TYPE -- Numeric SID
22 -- Local System
23 -- Local Service
24 -- Network Service
CertUtil -? -- Display a verb list (command list)
CertUtil -encode -? -- Display help text for the "encode" verb
CertUtil -v -? -- Display all help text for all verbs
下面是對一個文件進行base64編碼。結果文件首行和末行會有特殊標記。
CertUtil -encode test.txt test.txt.out
Input Length = 8
Output Length = 70
CertUtil: -encode command completed successfully.
cat test.txt
test
cat test.txt.out
-----BEGIN CERTIFICATE-----
dGVzdCAgDQo=
-----END CERTIFICATE-----
文件base64解碼
在Windows的cmd執行CertUtil -decode -?
可以查看解碼base64格式文件。
Usage:
CertUtil [Options] -decode InFile OutFile
Decode Base64-encoded file
Options:
-f -- Force overwrite
-Unicode -- Write redirected output in Unicode
-gmt -- Display times as GMT
-seconds -- Display times with seconds and milliseconds
-v -- Verbose operation
-privatekey -- Display password and private key data
-pin PIN -- Smart Card PIN
-sid WELL_KNOWN_SID_TYPE -- Numeric SID
22 -- Local System
23 -- Local Service
24 -- Network Service
CertUtil -? -- Display a verb list (command list)
CertUtil -decode -? -- Display help text for the "decode" verb
CertUtil -v -? -- Display all help text for all verbs
下面是對一個base64文件進行解碼。
CertUtil -decode test.txt.out test.txt.de
Input Length = 70
Output Length = 8
CertUtil: -decode command completed successfully.
cat test.txt.out
-----BEGIN CERTIFICATE-----
dGVzdCAgDQo=
-----END CERTIFICATE-----
cat test.txt.de
test
文件hex編碼
在Windows的cmd執行CertUtil -encodehex -?
可以查看編碼文件為hex格式文件。
Usage:
CertUtil [Options] -encodehex InFile OutFile [type]
Encode file in hexadecimal
Options:
-f -- Force overwrite
-Unicode -- Write redirected output in Unicode
-UnicodeText -- Write output file in Unicode
-gmt -- Display times as GMT
-seconds -- Display times with seconds and milliseconds
-v -- Verbose operation
-privatekey -- Display password and private key data
-pin PIN -- Smart Card PIN
-sid WELL_KNOWN_SID_TYPE -- Numeric SID
22 -- Local System
23 -- Local Service
24 -- Network Service
CertUtil -? -- Display a verb list (command list)
CertUtil -encodehex -? -- Display help text for the "encodehex" verb
CertUtil -v -? -- Display all help text for all verbs
下面是對一個文件進行hex編碼。結果類似Linux下的xxd格式,展示了文件hex的值。
CertUtil -encodehex test.txt test.hex
Input Length = 23
Output Length = 139
CertUtil: -encodehex command completed successfully.
cat test.txt
test1
test2
test3
cat test.hex
0000 74 65 73 74 31 0d 0a 74 65 73 74 32 0d 0a 74 65 test1..test2..te
0010 73 74 33 0d 0a 0d 0a st3....
文件hex解碼
在Windows的cmd執行CertUtil -decodehex -?
可以查看解碼hex格式文件。
Usage:
CertUtil [Options] -decodehex InFile OutFile [type]
Decode hexadecimal-encoded file
type -- numeric CRYPT_STRING_* encoding type
Options:
-f -- Force overwrite
-Unicode -- Write redirected output in Unicode
-gmt -- Display times as GMT
-seconds -- Display times with seconds and milliseconds
-v -- Verbose operation
-privatekey -- Display password and private key data
-pin PIN -- Smart Card PIN
-sid WELL_KNOWN_SID_TYPE -- Numeric SID
22 -- Local System
23 -- Local Service
24 -- Network Service
CertUtil -? -- Display a verb list (command list)
CertUtil -decodehex -? -- Display help text for the "decodehex" verb
CertUtil -v -? -- Display all help text for all verbs
下面是對一個hex文件進行解碼。
CertUtil -decodehex test.hex test.hex.out
Input Length = 139
Output Length = 23
CertUtil: -decodehex command completed successfully.
cat test.hex
0000 74 65 73 74 31 0d 0a 74 65 73 74 32 0d 0a 74 65 test1..test2..te
0010 73 74 33 0d 0a 0d 0a st3....
cat test.hex.out
test1
test2
test3
結語
熟練使用這個命令,可以方便的在Windows電腦上校驗文件,以及編碼解碼文件。特別是編碼為hex格式文件,可以分析文件的二進制數據。我以前遇到過文件包含不可見字符導致代碼編譯報錯,卻找不到哪里錯誤,這時候分析文件的二進制數據一目了然了。