使用由非对称密钥配对的 openssl R 加密的 Python 解密 openssl 字符串

发布于 2025-01-11 16:15:33 字数 4212 浏览 3 评论 0原文

我在 R 中使用过

crypted <- cyphr::encrypt_string("secret", key=cyphr::keypair_openssl("~/.ssh/id_rsa_given.pub","~/.ssh/id_rsa"))

decrypted <- cyphr::decrypt_string(crypted,cyphr::keypair_openssl("~/.ssh/id_rsa_given.pub","~/.ssh/id_rsa")

这在 R 中有效,两者都是:加密和解密。

但我不知道如何在 Python 中读取这个加密字符串。

REPREX

R

bash

ssh-keygen -t rsa -f mykeytest

ls -g -o mykeytest*
# -rw------- 1 2655 Mar  4 10:08 mykeytest
# -rw-r--r-- 1  577 Mar  4 10:08 mykeytest.pub
 

R 没有外部文件

crypted <- cyphr::encrypt_string("mysecret", key=cyphr::keypair_openssl("mykeytest.pub","mykeytest"))
decrypted <- cyphr::decrypt_string(crypted,cyphr::keypair_openssl("mykeytest.pub","mykeytest"))
decrypted
# [1] "mysecret"

R 有外部文件

writeBin(cyphr::encrypt_string("mysecret", key=cyphr::keypair_openssl("mykeytest.pub","mykeytest") ), "mysecret.txt")
mydecrypted2<-cyphr::decrypt_string(readBin("mysecret.txt", base::raw(), file.size("mysecret.txt")), 
              cyphr::keypair_openssl("mykeytest.pub","mykeytest") 
)
mydecrypted2
# [1] "mysecret"

Python

我的 POC Python 的开头

import sys
import sshpubkeys
import os

f_pub = open("mykeytest.pub", "r")
f_priv = open("mykeytest", "rb")
f_string_mysecret=  open("mysecret.txt", "rb")

key_pub=f_pub.read()
key_priv=f_priv.read()
input_string=f_string_mysecret.read()


# https://github.com/ojarva/python-sshpubkeys
ssh_pub=sshpubkeys.SSHKey(key_pub)
ssh_pub.parse()
ssh_pub.rsa

#  https://stackoverflow.com/questions/59029092/how-to-load-openssh-private-key-using-cryptography-python-module/69758595#69758595

from cryptography.hazmat.primitives.serialization import load_ssh_private_key
from hashlib import sha1

key_priv_b = load_ssh_private_key(key_priv,b'mypassword')



然后呢?

这就是问题所在?

您有一个纯Python答案来解密来自 cyphr::encrypt_string() 和 cyphr::keypair_openssl() 的

字符串 解决方法 rpy2

import rpy2.robjects as R
myfct = R.r(r'''
  library(cyphr)
  function(myfile) {
    cyphr::decrypt_string(
      readBin(myfile, base::raw(), file.size(myfile)),
      cyphr::keypair_openssl("mykeytest.pub","mykeytest") )
  }
''')
myfct("mysecret.txt")
# <rpy2.robjects.vectors.StrVector object at 0x7f0512114100> [RTYPES.STRSXP]
# R classes: ('character',)
# ['mysecret']

# in string : 

myfct("mysecret.txt")[0]
# 'mysecret'

它适用于我的真正问题。

cyphr::decrypt_string() 的 R 源代码的路径

但是它对于 R 来说有那么特别吗?没有标准的Python实现吗?

则 R 源和 C 源的路径

我的问题开头的链接:

I've used in R

crypted <- cyphr::encrypt_string("secret", key=cyphr::keypair_openssl("~/.ssh/id_rsa_given.pub","~/.ssh/id_rsa"))

and

decrypted <- cyphr::decrypt_string(crypted,cyphr::keypair_openssl("~/.ssh/id_rsa_given.pub","~/.ssh/id_rsa")

This works in R, both : crypt and decrypt.

But I don't find how to read this crypted string in Python.

REPREX

R

bash

ssh-keygen -t rsa -f mykeytest

ls -g -o mykeytest*
# -rw------- 1 2655 Mar  4 10:08 mykeytest
# -rw-r--r-- 1  577 Mar  4 10:08 mykeytest.pub
 

R without external file

crypted <- cyphr::encrypt_string("mysecret", key=cyphr::keypair_openssl("mykeytest.pub","mykeytest"))
decrypted <- cyphr::decrypt_string(crypted,cyphr::keypair_openssl("mykeytest.pub","mykeytest"))
decrypted
# [1] "mysecret"

R with external file

writeBin(cyphr::encrypt_string("mysecret", key=cyphr::keypair_openssl("mykeytest.pub","mykeytest") ), "mysecret.txt")
mydecrypted2<-cyphr::decrypt_string(readBin("mysecret.txt", base::raw(), file.size("mysecret.txt")), 
              cyphr::keypair_openssl("mykeytest.pub","mykeytest") 
)
mydecrypted2
# [1] "mysecret"

Python

Beginning of my POC Python

import sys
import sshpubkeys
import os

f_pub = open("mykeytest.pub", "r")
f_priv = open("mykeytest", "rb")
f_string_mysecret=  open("mysecret.txt", "rb")

key_pub=f_pub.read()
key_priv=f_priv.read()
input_string=f_string_mysecret.read()


# https://github.com/ojarva/python-sshpubkeys
ssh_pub=sshpubkeys.SSHKey(key_pub)
ssh_pub.parse()
ssh_pub.rsa

#  https://stackoverflow.com/questions/59029092/how-to-load-openssh-private-key-using-cryptography-python-module/69758595#69758595

from cryptography.hazmat.primitives.serialization import load_ssh_private_key
from hashlib import sha1

key_priv_b = load_ssh_private_key(key_priv,b'mypassword')



And after ?

That is the question ?

Have you a pure python answer to decrypt string from cyphr::encrypt_string() and cyphr::keypair_openssl()

Workaround with rpy2

import rpy2.robjects as R
myfct = R.r(r'''
  library(cyphr)
  function(myfile) {
    cyphr::decrypt_string(
      readBin(myfile, base::raw(), file.size(myfile)),
      cyphr::keypair_openssl("mykeytest.pub","mykeytest") )
  }
''')
myfct("mysecret.txt")
# <rpy2.robjects.vectors.StrVector object at 0x7f0512114100> [RTYPES.STRSXP]
# R classes: ('character',)
# ['mysecret']

# in string : 

myfct("mysecret.txt")[0]
# 'mysecret'

An it works for my real issue.

Path of the R source of cyphr::decrypt_string()

But is it so special to R ? There is not standard python implementation ?

Path of the R source and C Source if I have well understood

Links at the start of my question:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文