開源工具 DotnetRSA 快速生成和轉換RSA秘鑰


一.簡介

DotnetRSA 是一個利用 .NET Core 2.1 開發的 .NET Global Tool,是可以想npm全局安裝一樣,安裝在你的系統中,只需敲一行命令便可以快速生成RSA加密算法所需的秘鑰,目前支持三種格式的秘鑰,分別為:xml、pkcs1、pkcs8。它還支持三種格式秘鑰的相互轉換。

二.安裝

使用 DotnetRSA 需要你的系統具備.NET Core >=2.1 的環境,若沒有請訪問官網,按照提示進行安裝:https://www.microsoft.com/net/learn/get-started/windows

輸入一句命令便可以安裝:

dotnet tool install -g dotnetrsa

安裝成功會有如下提示:

1528035457178

同時輸入命令 dotnetrsa 會出現如下提示:

1528035498598

三.使用

1.生成秘鑰

生成秘鑰使用命令 dontetrsa gen,加入 -h 獲取幫助:

Generate xml, pkcs1, pkcs8 keys.

Usage: dotnetrsa gen [options]

Options:
  -h|--help           Show help information
  -f|--format         Required.Gen keys's format.The value must be xml, pkcs1 ,pkcs8.
  -s|--size <int>     Key Size.Default 2048.
  --pem               Pem Format. true of false.Default false.
  -o|--output <path>  File output path.If you do not specify it will be output in the current directory.

-f 或者 --format:指需要生成的格式,可以為 xml、pkcs1、pkcs8

-s 或者 --size :指需要生成的秘鑰長度,默認為2048

--pem :只是否帶有pem格式,值為 true 或者 false,默認為 false

-o 或者 --output :指秘鑰保存的路徑,默認保存在當前文件夾

生成一個長度為2048的xml格式的秘鑰示例:

dotnetrsa gen -s 2048 -f xml

2.轉換秘鑰

轉換秘鑰使用命令 dontetrsa convert ,加入 -h` 獲取幫助:

Usage: dotnetrsa convert [arguments] [options]

Arguments:
  KeyFilePath         Required.Secret key file path.

Options:
  -h|--help           Show help information
  -f|--from <format>  Required.Source format.The value must be xml, pkcs1,pkcs8.
  -t|--to <format>    Required.Target format.The value must be xml, pkcs1,pkcs8.
  -k                  Required.Key type.The value must be pri, pub.'pub' represents the public key.
  -o|--output <path>  File output path.If you do not specify it will be output in the current directory.

KeyFilePath:指定被轉換的秘鑰的路徑

-f 或者 --from :該字段為必須指定,指被轉換的秘鑰的格式,值只能為 xml、pkcs1、pkcs8

-t 或者 --to:該字段為必須指定,指需要轉換的目標格式,值只能為 xml、pkcs1、pkcs8

-k :指被轉換的秘鑰的類型,公鑰或者私鑰,值只能為 pri 或者 pub ,分別代表公鑰和私鑰

-o :轉換的秘鑰的輸出路徑,默認為當前目錄

將一個格式為xml的私鑰轉換為pkcs1,示例:

dotnetrsa convert c:\xml_private.key -f xml -t pkcs1 -k pri

四.參考資料

DotnetRSA 生成和轉換秘鑰使用了開源項目 RSAUtil :https://github.com/stulzq/RSAUtil

DotnetRSA 的開源地址:https://github.com/stulzq/dotnetrsa


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM