• 跳至主要導覽
  • 跳至主要內容
  • 跳至主要資訊欄
Office 指南

Office 指南

辦公室工作實用教學

  • Excel
  • Word
  • PowerPoint
  • Windows
  • PowerShell
  • R

OpenSSL 指令產生、驗證 RSA 數位簽章教學與範例

介紹如何使用 OpenSSL 指令產生檔案的數位簽章,並驗證簽章的有效性。

建立 RSA 金鑰

首先使用以下 OpenSSL 指令產生長度為 8912 位元的 RSA 私鑰,並設定保護金鑰的密碼。

# 產生 8912 位元的 RSA 私鑰,以 AES256 加密保護
openssl genrsa -aes256 -out private.pem 8912
Generating RSA private key, 8912 bit long modulus (2 primes)
..................................................................+++
.........................+++
e is 65537 (0x010001)
Enter pass phrase for private.pem:
Verifying - Enter pass phrase for private.pem:

有了 RSA 私鑰(private.pem)之後,再用私鑰產生公鑰(public.pem):

# 從 RSA 私鑰產生 RSA 公鑰
openssl rsa -in private.pem -pubout -out public.pem
Enter pass phrase for private.pem:
writing RSA key

RSA 的私鑰可以用來對檔案產生數位簽章,而公鑰則可用來驗證數位簽章的有效性。

產生數位簽章

使用 RSA 私鑰對檔案產生數位簽章:

# 產生數位簽章
openssl dgst -sha512 -sign private.pem -out digest.sha512 myfile.txt
Enter pass phrase for private.pem:

在使用私鑰之前,鑰先輸入私鑰的保護密碼,將私鑰解開之後,才會以私鑰產生檔案的數位簽章檔案 digest.sha512。

驗證數位簽章

將原始檔案 myfile.txt 與數位簽章檔案 digest.sha512 一起傳送給接收者後,接收者即可使用 RSA 公鑰 public.pem 驗證數位簽章的有效性:

# 驗證數位簽章
openssl dgst -sha512 -verify public.pem -signature digest.sha512 myfile.txt
Verified OK

驗證數位簽章時若出現 Verified OK 就代表簽章是有效的,亦即原始檔案 myfile.txt 沒有被竄改過。

如果出現 Verification Failure 的訊息,就代表數位簽章有問題,myfile.txt 可能損毀或是遭到竄改。

參考資料

  • EclipseSource:Tutorial: Code Signing and Verification with OpenSSL
  • Raymii.org:Sign and verify text/files to public keys via the OpenSSL Command Line
  • Enrico Zimuel:Sign and verify using OpenSSL
  • Jumpnow Technologies:Signing code with OpenSSL

分類:Linux 標籤:OpenSSL, 資訊安全

主要資訊欄

搜尋

近期文章

  • Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤
  • Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例
  • Linux 網路設定 ip 指令用法教學與範例
  • Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例
  • Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例
  • Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例
  • Linux 以 Cryptsetup 與 LUKS 加密磁碟教學與範例
  • Linux 使用 age 簡潔的加密、解密工具使用教學與範例

推薦網站

  • Udemy 線上教學課程
  • Coursera 線上教學課程

關注本站

  • 電子郵件
  • Facebook

公益

  • 家扶基金會
  • 台灣世界展望會
  • Yahoo 奇摩公益
  • igiving 公益網
  • 兒福聯盟

Copyright © 2021 · Office Guide