惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Help Net Security
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
博客园 - Franky
B
Blog RSS Feed
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
量子位
V
Visual Studio Blog
Y
Y Combinator Blog
小众软件
小众软件
N
Netflix TechBlog - Medium
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网

Ittavern.com

Wimage - Hosting Open-Source Image Uploader with Podman and external S3 Storage Switching from Hugo to picopaper ETag in nginx - Simple Resource Caching Sending nginx Logs to Loki with Grafana Alloy How to: Cisco ISE backup to SFTP repository with public key authentication Dummy IP & MAC Addresses for Documentation & Sanitization Deploying ISSO Commenting System for Static Content using Docker Generate a Vanity v3 Hidden Service Onion Address with mkp224o ssh-audit Primer - Audit your SSH Server mtr - More Detailed Traceroute - Network Troubleshooting My Personal Backup Strategy - August 2024 iperf3 - User Authentication with Password and RSA Public Keypair Adding a trash can to Linux with trash-cli Bandwidth Measurement using netcat on Linux Getting started with rsync - Comprehensive Guide Cron Jobs on Linux - Comprehensive Guide with Examples SSH Server Hardening Guide v2 Port Knocking with knockd and Linux - Server Hardening Getting started with rclone - Data transmission Getting started with dig - DNS troubleshooting Getting started with Fail2Ban on Linux Getting started with netcat on Linux with examples URL explained - The Fundamentals Troubleshooting Asking The Right Questions Create tmux layouts using bash scripts Getting started with tcpdump - Ittavern.com Curl on Linux - Reference Guide Getting started with nmap scripts My Offsite Backup - March 2023 Getting started with iperf3 - Network Troubleshooting
Encryption using SSH Keys with age in Linux
2025-10-26 · via Ittavern.com

In this article I want to share a method to use your SSH keypair to encrypt messages. We are going to use age in Ubuntu 24.04.

The installation guide can be found in the official repo.


Limitations #

Before we start with usage, let me share some limitations. Not all SSH key types are suited for encryption - even tho there seem to be workarounds. In a Github comment it was mentioned by 'str4d' that sk-* SSH keys won't work as they only provide support for authentication.

The same seems to be the case for ECDSA (Elliptic Curve Digital Signature Algorithm) SSH keys as I got the following error message while testing:

age: warning: recipients file "./age-testing.pub": ignoring unsupported SSH key of type "ecdsa-sha2-nistp521" at line 1


In this article I'll be working with EdDSA-ed25519 and RSA SSH keys.

# RSA (Rivest–Shamir–Adleman):
    ssh-keygen -t rsa -b 4096 -f ~/.ssh/nameofthekey
# EdDSA ed25519:
    ssh-keygen -t ed25519 -f ~/.ssh/nameofthekey

Additionally, the ssh-agent is not supported.


Usage #

Common use cases are to encrypt data to allow you to store ore transfer it securly in an untrusted or unknwon environment. You can make sure that only recipients with the right private key can decrypt the files, messages, or whatever.


Simple Examples #

Used version:

age --version
1.1.1

Encryption of a simple string with SSH public key:

echo "Cheers" | age -R ~/.ssh/id_ssh.pub > cheers.txt

Encrypted content:

cat cheers.txt 

age-encryption.org/v1
-> ssh-ed25519 7uu5gg 4ivp9LPXTVu6ryrhuSskhL5A3RuQWL8XAg5mxbx6v0s
kGJzFPj2TiwrvrWmVonCsGcWeYmQ7qsV5WXNrf6c0H0
--- Rr+SI6g+73XM6R3CTa7WVp4eEDBgdmZMlsjhHihwjz4

Decrypt file with SSH private key:

cat cheers.txt | age -d -i ~/.ssh/id_ssh

Cheers

To encrypt files, we build upon the example from the official documentation:

tar cvz ./data | age -R ~/.ssh/id_ssh.pub | base64 > data.tar.gz.age

./data/
./data/random-video.mp4

Remove the source files:

rm -r ./data

Decrypt files:

cat data.tar.gz.age   | base64 --decode   | age -d -i ~/.ssh/id_ssh   | tar xzv

./data/
./data/random-video.mp4

Side Note: I'll use base64 encoding to make it more compatible with more services as some tools might not like the binary encoding.

Addition: Instead of the base64 encoding, you could use a, --armor to have a more compatible format like this:

# Encryption
tar cvz ./data | age -a -R ~/.ssh/id_ssh.pub > data.tar.gz.armor.age

# Format
head -5 data.tar.gz.armor.age

-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNzaC1lZDI1NTE5IDd1dTVnZyBjL0Zo
eTJUWDdDR0YzdzdjdDJEZjVxV0NRS1kxMlJWZEt5Y1hONGp4OHljCmJCdjBrUWlZ
QitTcC9Na1BDV2NCbHJsSlhaVHRJMElJMkJxUVdSd3ZFRjAKLS0tIGdSRHJFRVBV
NkFFbjNTbStBLzg0THJCM0lCMHdKbzhvRS9YemhISlRUY0kKK7Cxnu172NVbpBaa

# Decryption
cat data.tar.gz.armor.age  | age -d -i ~/.ssh/id_ssh   | tar xzv

Thank you technomancy for pointing it out!


Practical Example #

There a multiple CLI paste service that allow you to share configs, error messages, and so on. Examples are 0x0.st or linedump.com.

Encrypting the payload makes sure that nobody else can read the content.

Upload

String:
echo "Cheers" | age -R ~/.ssh/id_ssh.pub | base64 | curl -X POST --data-binary @- https://linedump.com
File:
age -R ~/.ssh/id_ssh.pub -o - file.txt | base64 | curl -X POST --data-binary @- https://linedump.com
Command:
ip -br a | age -R ~/.ssh/id_ssh.pub | base64 | curl -X POST --data-binary @- https://linedump.com

Download

Save to file:
curl -s https://linedump.com/{paste_id} | base64 -d | age -d -i ~/.ssh/id_ssh > output3.txt

Side Notes: A disclaimer: Linedump is a project of mine which was one reason to look into the encryption with SSH keypairs for some automation.


Multiple recipients #

age allows you to encrypt for multiple recipients which can decrypt it individually, which is great for a team or some automation and syncing.

Simply use multiple -r/--recipient flags - which requires the public key in the command or simply add the public keys to a file and use -R - one key per line.

Official documentation:

cat recipients.txt

# Alice
age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p
# Bob
age1lggyhqrw2nlhcxprm67z43rta597azn8gknawjehu9d9dl0jq3yqqvfafg

age -R recipients.txt example.jpg > example.jpg.age