首页 > 其他分享 >gpg加密实践

gpg加密实践

时间:2023-08-22 19:33:24浏览次数:29  
标签:加密 22 -- gpg 实践 key root ID

gpg文件加密

测试环境 linux ubuntu

1.创建秘钥

root@ubuntu:~# gpg --gen-key
gpg (GnuPG) 1.4.20; Copyright (C) 2015 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 
Key does not expire at all
Is this correct? (y/N) y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) <[email protected]>"

Real name: fczlm
Email address: [email protected]
Comment: gpg test
You selected this USER-ID:
    "fczlm (gpg test) <[email protected]>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.

  

如果报错

Not enough random bytes available. Please do some other work to give
the OS a chance to collect more entropy! (Need 100 more bytes)

执行

apt-get install rng-tools

rng -r /dev/urandom

 

gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 1A9A90CB marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/1A9A90CB 2023-08-22
      Key fingerprint = E5D3 F137 B2F9 6BFB 0B85  EA08 CE08 06B1 1A9A 90CB
uid                  fczlm (gpg test) <[email protected]>
sub   2048R/FAD977B7 2023-08-22

  

/root/.gnupg

-rw------- 1 root root 9398 Aug 22 18:03 gpg.conf
-rw------- 1 root root 2384 Aug 22 18:19 pubring.gpg
-rw------- 1 root root 2384 Aug 22 18:19 pubring.gpg~
-rw------- 1 root root  600 Aug 22 18:19 random_seed
-rw------- 1 root root 5172 Aug 22 18:19 secring.gpg
-rw------- 1 root root 1360 Aug 22 18:19 trustdb.gpg

  

公钥:pubring.gpg,私钥:secring.gpg

公钥的密钥ID 1A9A90CB

私钥密码是解密时需要的,密钥ID是对文件进行加密需要的。

 

2. 生成撤销证书

可选,以备密钥作废时,请求外部的公钥服务器撤销公钥。

root@ubuntu:~/.gnupg# gpg --gen-revoke 7FF3CFEC

sec  2048R/7FF3CFEC 2023-08-22 fczlm (gpg test) <[email protected]>

Create a revocation certificate for this key? (y/N) y
Please select the reason for the revocation:
  0 = No reason specified
  1 = Key has been compromised
  2 = Key is superseded
  3 = Key is no longer used
  Q = Cancel
(Probably you want to select 1 here)
Your decision? 0
Enter an optional description; end it with an empty line:
> 
Reason for revocation: No reason specified
(No description given)
Is this okay? (y/N) y

You need a passphrase to unlock the secret key for
user: "fczlm (gpg test) <[email protected]>"
2048-bit RSA key, ID 7FF3CFEC, created 2023-08-22

ASCII armored output forced.
Revocation certificate created.

Please move it to a medium which you can hide away; if Mallory gets
access to this certificate he can use it to make your key unusable.
It is smart to print this certificate and store it away, just in case
your media become unreadable.  But have some caution:  The print system of
your machine might store the data and make it available to others!
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
Comment: A revocation certificate should follow

iQEfBCABAgAJBQJk5JjPAh0AAAoJEKP7Mkd/88/s4rwH/13808TQiv5MuYcitYZl
htvn3wBvz86/bL5tsNNMeiPhtgETNM3wBSKIDe+KOiq97Ow1rGZcEkgUbM/GAoRm
3mBXwOROBsZAEFfK52tp*****e1SEnXNvEePpOqDfVSY
IoLlIxiKoB937wZb52kZ5+wI1stRkrYOxQHfri9W9pDocZ+CQcYCIbgM5fUxtYik
cf5j47sM8rkBio6fn1fsLCeYfV8gKUyZ3BiUpvt4983tFVnQIppCiS31LCDmHqQ3
Px3FOAvqdj7ndjMJdsbNWG/x/yu/0wYt/jzGx12ykN/LMP979bbEu4sT0a+9H/y0
2rk=
=5m9j
-----END PGP PUBLIC KEY BLOCK-----

  

3.秘钥管理

列出秘钥

gpg -k 或者 gpg --list-keys           //列举公钥
gpg -K 或者 gpg --list-secret-keys   //列举私钥

 

删除秘钥

gpg --delete-key [用户ID]         # 删除公钥
gpg --delete-secret-keys [用户ID]    # 删除私钥

  

导出秘钥

gpg --armor  --export [用户ID] --output public-key.txt
gpg --armor  --export-secret-keys --output private-key.txt

  

4.加解密

加密text.txt

gpg --recipient [用户ID] --output test.txt.gpg --encrypt test.txt

  

解密

gpg --output test2.txt --decrypt test.txt.gpg 

  

输入密码即可

 

标签:加密,22,--,gpg,实践,key,root,ID
From: https://www.cnblogs.com/fczlm/p/17649474.html

相关文章

  • java应用接口自动化覆盖率统计实践
    一、背景Java应用接口自动化覆盖率统计的意义在于:确保测试覆盖率:通过自动化覆盖率统计,可以确保测试用例对应用程序的各个接口进行了全面的覆盖。这有助于发现潜在的代码错误、逻辑漏洞或者未处理的异常情况。提高代码质量:通过自动化覆盖率统计,可以发现代码中未被测试到的部......
  • java-sdk接口测试覆盖率统计实践
    一、背景接口覆盖率统计在JavaSDK开发中具有重要的意义。它衡量了代码中接口被测试用例覆盖的程度,即测试用例对接口的执行情况进行了多少次验证。接口覆盖率统计的意义包括:质量保证:接口覆盖率统计可以帮助开发团队评估测试的全面性和质量,确保代码的正确性和稳定性。高覆盖率......
  • 大型网络中路由器故障切换技术的应用与实践
    在大型网络中,为了保证服务的高可用性和可靠性,需要配置多个路由器来分担流量和提供服务。但是当其中一台路由器出现故障或停机时,整个网络会出现不可用的情况。为了解决这个问题,可以使用路由器故障切换技术。路由器故障切换技术是指在多台路由器之间实现自动切换,以保证网络的高可用性......
  • 查询速度最高提升50倍!火山引擎ByteHouse在广告投放领域实践分享
     更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群 据QuestMobile报告显示,移动互联网已经进入了下半场,在使用人数和使用时长方面已经没有明显增长,互联网已经流量趋于饱和。 作为广告投放主要阵地,由于互联网平台流量红利逐渐消退,越来越......
  • SpringBoot 测试实践 - 2:单元测试与集成测试
    单元测试vs.集成测试只编写单测,无法测试方法之间的集成情况,而且某些需求可能会修改多个方法,这可能会影响方法对应的单测,涉及到大量的相关单测的修改,这样的维护成本很高可以把重心放在完善集成测试上,专注从外部判断程序是否符合预期。对于一些非常重要的方法,增加单元测试可以减......
  • DDD实践:实现基于快照机制的变更追踪
    王有志,一个分享硬核Java技术的互金摸鱼侠加入Java人的提桶跑路群:共同富裕的Java人去年我们在重构项目中落地了DDD,当时花了点时间研究了下阿里巴巴大淘宝技术发布的《阿里技术专家详解DDD系列》,其中第三讲《阿里技术专家详解DDD系列 第三讲 - Repository模式》中提到了一项......
  • 加密算法分类
    密码加密算法针对密码存储的加密算法通常会使用一些特定的哈希函数或密码学技术,以确保用户密码在存储时是安全的。bcrypt:这是一种基于Blowfish加密算法的密码哈希函数。它适用于存储密码,因为它的加密强度可以根据需要进行调整,以抵御暴力破解和彩虹表等攻击。scrypt:与bc......
  • spark on k8s 开发部署简单实践
    实际上就是一个简单的实践,方便参考,对于开发以及运行,集成ci/cd以及dophinscheduler任务调度为了方便开发的spark应用共享以及使用基于s3进行文件存储(当然dophinscheduler也是支持自己的资源库的)参考图 玩法说明基于gitlab进行代码管理,通过ci/cd进行sparkapp的构建,同......
  • 实现单元测试和集成测试的.NET最佳实践
    实现单元测试和集成测试的.NET最佳实践在现代软件开发中,测试是确保应用程序质量和稳定性的关键步骤。在.NET开发中,单元测试和集成测试是两种常见的测试类型,它们有助于在开发过程中及时发现和修复问题。本篇博客将介绍.NET中实现单元测试和集成测试的最佳实践,以确保您的应用程序具有......
  • Vue 项目性能优化实践
    Vue已经成为前端必备利器了,Vue首页加载速度慢是很常见的问题,dist文件的体积差不多都在10m左右,解决方式也有很多,最简单的方式增加服务器的配置性能,当然在预算有限的情况下,如果通过优化来提升速度呢。比如在一台普通配置服务器上,差不多加载速度在15s左右,那是没办法接受的,不管是用......