linux ssh生成公私钥并私钥免密登录ssh

使用非对称密钥登陆 Linux
1、使用 ssh-keygen 生成公私钥,一路回车

2、服务器(Linux)端 ~/.ssh/authorized_keys 文件存放公钥,保证 SSH 服务开启,默认端口 22。

3、自己保存私钥,登陆时使用 ssh 命令

# 生成密钥
ssh-keygen -t rsa -b 4096 -f ~/data/key/id_test_rsa -C "xxxxxx@163.com"

-t 密钥类型, dsa | ecdsa | ed25519 | rsa
-b RSA类型密钥的大小(长度),通常至少应该是 2048,默认 3096
-f 指定私钥的文件名,e.g. ~/.ssh/private_key_name
-C 指定一个注释

#~/data/key下会生成两个文件,私钥:id_test_rsa,公钥:id_test_rsa.pub

# 将公钥放到 ~/.ssh/authorized_keys 文件中
cat id_rsa.pub >> authorized_keys

# 私钥登录
ssh user@host/ip[:port] -i [identity_file]

e.g. root@106.14.23.168 -i ~/.ssh/id_rsa

-i identity_file 指定私钥文件

#如果提示缺权限,给私钥600权限
chmod 600 id_rsa


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个使用OpenSSL库生成RSA私钥对,并使用私钥解密的C语言示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/rsa.h> #include <openssl/pem.h> #define KEY_LENGTH 2048 #define PUB_KEY_FILE "public.pem" #define PRI_KEY_FILE "private.pem" int main() { RSA *rsa = NULL; unsigned char *encrypted = NULL, *decrypted = NULL; int encrypted_length, decrypted_length; // 生成RSA密钥对 rsa = RSA_generate_key(KEY_LENGTH, RSA_F4, NULL, NULL); if (rsa == NULL) { printf("Failed to generate RSA key pair\n"); return -1; } // 把私钥保存到文件中 FILE *priv_key_file = fopen(PRI_KEY_FILE, "wb"); if (priv_key_file == NULL) { printf("Failed to open private key file\n"); RSA_free(rsa); return -1; } PEM_write_RSAPrivateKey(priv_key_file, rsa, NULL, NULL, 0, NULL, NULL); fclose(priv_key_file); // 把公钥保存到文件中 FILE *pub_key_file = fopen(PUB_KEY_FILE, "wb"); if (pub_key_file == NULL) { printf("Failed to open public key file\n"); RSA_free(rsa); return -1; } PEM_write_RSAPublicKey(pub_key_file, rsa); fclose(pub_key_file); // 使用私钥解密数据 const char *message = "Hello, World!"; encrypted_length = RSA_public_encrypt(strlen(message) + 1, (unsigned char *)message, &encrypted, rsa, RSA_PKCS1_PADDING); if (encrypted_length == -1) { printf("Failed to encrypt message\n"); RSA_free(rsa); return -1; } decrypted = (unsigned char *)malloc(RSA_size(rsa)); decrypted_length = RSA_private_decrypt(encrypted_length, encrypted, decrypted, rsa, RSA_PKCS1_PADDING); if (decrypted_length == -1) { printf("Failed to decrypt message\n"); RSA_free(rsa); free(encrypted); free(decrypted); return -1; } printf("Decrypted message: %s\n", decrypted); RSA_free(rsa); free(encrypted); free(decrypted); return 0; } ``` 该示例中,使用`RSA_generate_key`函数生成RSA私钥对,使用`PEM_write_RSAPrivateKey`和`PEM_write_RSAPublicKey`函数将私钥公钥保存到文件中。 使用`RSA_public_encrypt`函数使用公钥加密数据,使用`RSA_private_decrypt`函数使用私钥解密数据。注意,在使用`RSA_public_encrypt`函数加密数据时,需要指定要加密的数据长度,因为RSA加密算法只能加密比密钥长度小的数据。在使用`RSA_private_decrypt`函数解密数据时,需要使用`RSA_size`函数得到解密后数据的最大长度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyTHP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值