SSH私钥新格式转换

ssh-keygen生成的密钥都变成以-----BEGIN OPENSSH PRIVATE KEY-----开头的字符串,导致finalshell无法使用ssh证书进行登录。

这里使用PuTTY对ssh私钥进行格式转换。

下载地址https://www.puttygen.com/#Download_PuTTYgen_on_Windows

然后安装,按如下步骤导入你的私钥,并生成老格式的私钥:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSH公钥和私钥是一对密钥,它们被用于在SSH协议下进行身份验证和加密通信。这里提供一个简单的SSH公钥和私钥换算器的Python代码示例,可以将一个格式正确的SSH私钥转换为公钥或者将公钥转换私钥。 ```python import base64 import hashlib def generate_public_key(private_key): """ 从SSH私钥生成SSH公钥 """ key_type, _, private_key = private_key.split(' ', 2) private_key = base64.b64decode(private_key) if key_type == 'ssh-rsa': # RSA公钥格式: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ... exponent_length = int.from_bytes(private_key[0:4], byteorder='big') exponent = int.from_bytes(private_key[4:4+exponent_length], byteorder='big') modulus_length = int.from_bytes(private_key[4+exponent_length:8+exponent_length], byteorder='big') modulus = int.from_bytes(private_key[8+exponent_length:8+exponent_length+modulus_length], byteorder='big') public_key = 'ssh-rsa ' + base64.b64encode( b'\x00\x00\x00\x07ssh-rsa' + exponent_length.to_bytes(4, byteorder='big') + exponent.to_bytes((exponent.bit_length() + 7) // 8, byteorder='big') + modulus_length.to_bytes(4, byteorder='big') + modulus.to_bytes((modulus.bit_length() + 7) // 8, byteorder='big') ).decode('ascii') return public_key elif key_type == 'ssh-ed25519': # Ed25519公钥格式: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMcDa... public_key = 'ssh-ed25519 ' + base64.b64encode(private_key[32:]).decode('ascii') return public_key else: raise ValueError('Unsupported key type') def generate_private_key(public_key, private_key=None): """ 从SSH公钥生成SSH私钥 """ key_type, _, public_key = public_key.split(' ', 2) public_key = base64.b64decode(public_key) if key_type == 'ssh-rsa': # RSA私钥格式: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ... if private_key is None: raise ValueError('Private key is required for RSA key type') exponent_length = private_key.find(b'\x00\x00\x00\x07ssh-rsa') + 11 exponent_length = int.from_bytes(private_key[exponent_length:exponent_length+4], byteorder='big') exponent = int.from_bytes(private_key[exponent_length+4:exponent_length+4+exponent_length], byteorder='big') modulus_length = int.from_bytes(private_key[exponent_length+4+exponent_length:exponent_length+4+exponent_length+4], byteorder='big') modulus = int.from_bytes(private_key[exponent_length+4+exponent_length+4:exponent_length+4+exponent_length+4+modulus_length], byteorder='big') private_key = b''.join([ b'\x00\x00\x00\x07ssh-rsa', exponent_length.to_bytes(4, byteorder='big'), exponent.to_bytes((exponent.bit_length() + 7) // 8, byteorder='big'), modulus_length.to_bytes(4, byteorder='big'), modulus.to_bytes((modulus.bit_length() + 7) // 8, byteorder='big'), private_key[public_key.__len__():] ]) private_key = key_type.encode('ascii') + b' ' + base64.b64encode(private_key).replace(b'\n', b'') + b' ' return private_key elif key_type == 'ssh-ed25519': # Ed25519私钥格式: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMcDa... if private_key is None: # 使用公钥生成随机私钥 private_key = hashlib.sha256(public_key).digest() private_key = key_type.encode('ascii') + b' ' + base64.b64encode(private_key).replace(b'\n', b'') + b' ' return private_key else: raise ValueError('Unsupported key type') ``` 使用示例: ```python # 生成RSA密钥对 from cryptography.hazmat.primitives.asymmetric import rsa private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048) public_key = private_key.public_key() ssh_private_key = generate_private_key('ssh-rsa ' + base64.b64encode(public_key.public_bytes(encoding=serialization.Encoding.DER, format=serialization.PublicFormat.PKCS1)).decode('ascii'), private_key=private_key.private_bytes(encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption())) ssh_public_key = generate_public_key(ssh_private_key) # 生成Ed25519密钥对 from cryptography.hazmat.primitives.asymmetric import ed25519 private_key = ed25519.Ed25519PrivateKey.generate() public_key = private_key.public_key() ssh_private_key = generate_private_key('ssh-ed25519 ' + base64.b64encode(public_key.public_bytes(encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw)).decode('ascii')) ssh_public_key = generate_public_key(ssh_private_key) ``` 注意:这个示例代码仅适用于支持PKCS#1和PKCS#8格式的RSA和Ed25519密钥。如果要使用其他格式的密钥,代码需要进行相应的修改。此外,为了安全起见,不应该在生产环境中使用此代码,而应该使用已经被广泛测试和验证的SSH密钥生成库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值