linux:gpg加密和解密

  • 1 创建密钥
  • 2 查看私钥
  • 3 导出公钥
  • 4 导出私钥
  • 5 导入秘钥
    • 5.1 公钥
  • 6 公钥加密
  • 7 私钥解密

创建密钥

gpg --gen-key

你要求输入一下内容,这些内容,基本英文,还是很好明白

root@g140:~# gpg --gen-key
gpg (GnuPG) 1.4.11; Copyright (C) 2010 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?
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) <heinrichh@duesseldorf.de>" Real name: chenshake
Email address: shake.chen@gmail.com
Comment:
You selected this USER-ID:
"chenshake <shake.chen@gmail.com>" Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key. We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy. gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key AEAA16F3 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/AEAA16F3 2013-10-21
Key fingerprint = B93B 05F8 9D63 5464 6DEE 6A08 EB9E D2BB AEAA 16F3
uid chenshake <shake.chen@gmail.com>
sub 2048R/02B4038D 2013-10-21
就是上面这个过程

在这个生成密钥的过程中,有个步骤很重要,就是生成内存随机数,命令如下:

dd if=/dev/zero of=test.dbf bs=9000 count=1000k .直到生成key为止

查看私钥

# gpg -k
/root/.gnupg/pubring.gpg
------------------------
pub 2048R/AEAA16F3 2013-10-21
uid chenshake <shake.chen@gmail.com>
sub 2048R/02B4038D 2013-10-21

查看公钥

# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub 2048R/AEAA16F3 2013-10-21
uid chenshake <shake.chen@gmail.com>
sub 2048R/02B4038D 2013-10-21

导出公钥

gpg -o chenshake.gpg -a --export chenshake

其中chenshake为用户ID
chenshake.gpg为导出的公钥文件

导出私钥

gpg -o chenshake-private.gpg --export-secret-keys chenshake

导入秘钥

公钥和私钥的导入,都是一样。

gpg --import filename

公钥

(另外一台机器)

当然你需要通过scp把公钥复制到远程的机器

# gpg --import chenshake.gpg
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
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key AEAA16F3: public key "chenshake <shake.chen@gmail.com>" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)

你也可以查看到导入的公钥

# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub 2048R/AEAA16F3 2013-10-21
uid chenshake <shake.chen@gmail.com>
sub 2048R/02B4038D 2013-10-21

公钥加密

一个文件,我这里是加密cobbler.ks 这个文件,加密后是 cobbler.ks.gpg文件,需要私钥才能查看。

# gpg -o cobbler.ks.gpg -er chenshake cobbler.ks
gpg: 02B4038D: There is no assurance this key belongs to the named user pub 2048R/02B4038D 2013-10-21 chenshake <shake.chen@gmail.com>
Primary key fingerprint: B93B 05F8 9D63 5464 6DEE 6A08 EB9E D2BB AEAA 16F3
Subkey fingerprint: FCCA 50DD C98D 644E 00FB 804C 4CD2 9544 02B4 038D It is NOT certain that the key belongs to the person named
in the user ID. If you *really* know what you are doing,
you may answer the next question with yes. Use this key anyway? (y/N) y

私钥解密

私钥查看 cobbler.ks.gpg

# gpg -o cobbler.ks -d cobbler.ks.gpg 

You need a passphrase to unlock the secret key for
user: "chenshake <shake.chen@gmail.com>"
2048-bit RSA key, ID 02B4038D, created 2013-10-21 (main key ID AEAA16F3) gpg: gpg-agent is not available in this session
gpg: encrypted with 2048-bit RSA key, ID 02B4038D, created 2013-10-21
"chenshake <shake.chen@gmail.com>"

需要你输入你当初创建秘钥的时候设置的那个密码。这个时候,你就可以查看到加密的文件。

注明:这篇文章是转载,但是说明了最重要的一个步骤!

gpg加密和解密的更多相关文章

  1. linux:gpg加密和解密

    http://www.bubuko.com/infodetail-650747.html

  2. 使用 gpg 加密文件 - 通过 shell 或 php

    使用 gpg 加密文件 - 通过 shell 或 php 背景:客户提供私钥,并要求我方通过php把加密后的文件传输给他们. 环境 macOS Sierra 10.12.1 php 7.0.8 0.安 ...

  3. Windows上使用Thunderbird与GPG发送和解密公钥加密的电子邮件

    作者:荒原之梦 原文链接:http://zhaokaifeng.com/?p=552 非对称加密的原理: 最先出现的加密方法是对称加密.在对称加密算法中是不区分公钥和私钥的,加密与解密使用的都是同一个 ...

  4. worldpress 的 GPG 加密插件

    worldpress 的 GPG 加密插件资料来源 https://trog.qgl.org/wpgpg/这个插件的作用是,用GPG 加密worldpress 的输出内容,然后在chrome浏览器中上 ...

  5. 四十五.加密与解密 AIDE入侵检测系统 扫描与抓包

    一.加密与解密 1.1 常见的加密算法 对称加密:怎么加密,就怎么解密 DES Date Encryption Standard AES Advance Encryption Standard 非对称 ...

  6. GPG(pgp)加解密中文完整教程

    一.介绍 我们都知道,互联网是不安全的,但其上所使用的大部分应用,如Web.Email等一般都只提供明文传输方式(用https.smtps等例外).所以,当我们需要传输重要文件时,应该对当中的信息加密 ...

  7. Security基础(二):SELinux安全防护、加密与解密应用、扫描与抓包分析

    一.SELinux安全防护 目标: 本案例要求熟悉SELinux防护机制的开关及策略配置,完成以下任务: 将Linux服务器的SELinux设为enforcing强制模式 在SELinux启用状态下, ...

  8. Linux学习66 运维安全-通信加密和解密技术入门

    一.Linux Service and Security 1.OpenSSL(ssl/tls)协议 2.OpenSSH(ssh)协议 3.bind(dns) 4.web(http):httpd(apa ...

  9. ASP.NET加密和解密数据库连接字符串

    大家知道,在应用程序中进行数据库操作需要连接字符串,而如果没有连接字符串,我们就无法在应用程序中完成检索数据,创建数据等一系列的数据库操作.当有人想要获取你程序中的数据库信息,他首先看到的可能会是We ...

随机推荐

  1. zigbee学习基础

    应用场合:功耗(休眠)以及自组网(动态路由,梯度法寻径)是其特点.距离短.功耗低且传输速率不高的各种电子设备之间进行有周期性数据.间歇性数据和低反应时间数据传输的应用(智能家居/仓储中转/伞兵落地协同 ...

  2. [原]Java工程打包注意事项

    注意事项(持续增加...): 如果Java工程中用到了注解,在用eclipse打jar包时需要注意一下,勾上“Add directory entries”,否则注解的类会注册不上

  3. [理解] C++ 中的 源文件 和 头文件

    我是学 C井 的, 现在在工作中主要使用的编程语言是 Java, 还记得当初在第一次接触到 Cpp 的时候, 听到的第一个概念就是 Cpp 的头文件和源文件, 当初理解了好久, 死活都弄不明白, 现在 ...

  4. Codeforces1140D. Minimum Triangulation

    题目链接 本题是区间dp里的三角剖分,板子题,dp[i][j]表示凸多边形i-j构成的最值,转移方程为dp[i][j] = min/max(dp[i][k]+dp[k][j]+w[i,j,k])(i& ...

  5. Git三招

    一.Git提交指令 git init git第一次使用在当前文件夹初始化一个git仓库,第二次不需要 git add . 把当前文件夹所有文件添加到缓存区中. 可以选特定的文件夹或文件.将后面的.改变 ...

  6. HA: Infinity Stones-Write-up

    下载地址:点我 哔哩哔哩:点我 主题还是关于复仇者联盟的,这次是无限宝石的. 信息收集 虚拟机的IP为:192.168.116.137 ➜ ~ nmap -sn 192.168.116.1/24 St ...

  7. day11 作业

    # 1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), # 要求登录成功一次,后续的函数都无需再输入用户名和密码 # FLAG = False # def login(func): ...

  8. Java 自定义DateUtils

    1 /* Date d = new Date(); String s = DateUtils.DateToString(d, "yyyy-MM-dd HH:mm:ss"); Sys ...

  9. vue重置data数据

    可以通过this.$data获取当前状态下的data,通过this.$options.data()获取该组件初始状态下的data. 然后只要使用Object.assign(this.$data, th ...

  10. [RoarCTF2019]forensic

    拿到raw文件拖到kali里,首先看镜像信息. volatility -f /root/mem.raw imageinfo 用建议的profile,Win7SP1x86.先查看下内存中的进程 vola ...