java 到 python 转换:x509 / dsa / sha1withdsa 加密如何?

发布于 2024-08-02 20:32:41 字数 771 浏览 8 评论 0原文

我有以下 Java 代码,我正在尝试将其转换为 python,但我不太确定如何执行此操作:

import java.security.spec.X509EncodedKeySpec;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;

byte[] key = KeyReader.read(filestream) 
  //KeyReader.read(inputstream) just reads in the bytes 1 at a time from the filestream
X509EncodedKeySpec pubKey = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");

PublicKey pub = keyFactory.generatePublic(pubKey);

Signature signature = Signature.getInstance("SHA1withDSA");
signature.initVerify(pub)
signature.update(a_byte_string) //doesn't matter

我对如何在 python 中执行此操作有点迷失。具体来说,SHA1withDSA 部分。我只是对 python 加密库(确切地说是 m2crypto)了解不够,无法映射函数(我也找不到任何关于如何做到这一点的像样的文章)。

I've got the following Java code that I'm trying to convert to python, and I'm not really sure how to do this:

import java.security.spec.X509EncodedKeySpec;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;

byte[] key = KeyReader.read(filestream) 
  //KeyReader.read(inputstream) just reads in the bytes 1 at a time from the filestream
X509EncodedKeySpec pubKey = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");

PublicKey pub = keyFactory.generatePublic(pubKey);

Signature signature = Signature.getInstance("SHA1withDSA");
signature.initVerify(pub)
signature.update(a_byte_string) //doesn't matter

I'm kinda lost as to how to do this in python. Specifically, the SHA1withDSA part. I just don't know enough about the python crypto libs (m2crypto to be exact) to map the functions (nor could I find any decent writeups on how to do this).

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2024-08-09 20:32:41

我不太明白Java代码,但这就是你想要做的吗?

from M2Crypto import X509

x509 = X509.load_cert(filename)
assert x509.verify() == 1

I don't exactly understand the Java code, but is this what you are trying to do?

from M2Crypto import X509

x509 = X509.load_cert(filename)
assert x509.verify() == 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文