- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在创建一个基本的套接字程序,我想将 C 中的加密字符串发送到 Java 程序。我的 C 程序使用公共(public) PEM key 加密该字符串。我已将匹配的私有(private) PEM key 转换为 DER key ,现在想要解密发送到我的 Java 程序的字符串。我该怎么做?
目前,我在尝试按原样运行代码时收到 IllegalBlockSizeException,指出“数据不得长于 256 字节”。
这就是我现在所拥有的:
C 客户端程序...
//Get our public key from the publickey file created by server
FILE *publicKeyFile = fopen("publicKey.pem", "rb");
RSA *rsa = RSA_new();
rsa = PEM_read_RSA_PUBKEY(publicKeyFile, &rsa, NULL, NULL);
if(rsa == NULL) {
printf("Error with public key...\n");
}
else {
//if the public key is correct we will encrypt the message
RSA_public_encrypt(2048, sigMessage, sigMessage, rsa, RSA_PKCS1_PADDING);
}
Java解密...
public static String decrypt(byte[] encryptedMessage) {
try {
Cipher rsa;
rsa = Cipher.getInstance("RSA");
PrivateKey ourKey = getKey("resources/privateKey.der");
rsa.init(Cipher.DECRYPT_MODE, ourKey);
byte[] utf8 = rsa.doFinal(encryptedMessage);
return new String(utf8, "UTF8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static PrivateKey getKey(String filePath) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
File f = new File(filePath);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);
}
最佳答案
除非您拥有超过 16K 的 RSA key ,否则您无法使用 RSA 加密 2,048字节。您可能有一个 2048 位 RSA key ,这将您限制在 256 字节以下。查看 openssl 手册页 RSA_public_encrypt :
flen must be less than RSA_size(rsa) - 11 for the PKCS #1 v1.5 based padding modes, less than RSA_size(rsa) - 41 for RSA_PKCS1_OAEP_PADDING and exactly RSA_size(rsa) for RSA_NO_PADDING. The random number generator must be seeded prior to calling RSA_public_encrypt().
和RSA_size :
RSA_size() returns the RSA modulus size in bytes. It can be used to determine how much memory must be allocated for an RSA encrypted value.
不过,您不应该使用 2048 位 key 加密完整的 256 字节。您希望始终使用带有 RSA 加密的随机填充,并选择 OAEP 而不是 v1.5。
关于java - 如何使用匹配的 DER 私钥解密 PEM 公钥加密字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35420538/
起初,这是我的第一篇文章——如果这篇文章没有得到很好的描述和设计,我很抱歉。 我的问题是我需要使用 Spring Boot 通过 SSL 连接连接到 GCP 中的数据库。在 GCP 内部生成三个所需的
是否可以创建相同大小的 openssl 证书(server.key.der 或 server.crt.der).. 1024 或 2048 或任何固定大小?我喜欢这一点,每次我创建证书时它都会更改大小
我有一个 DER 格式的证书文件和一个 DER 格式的 key 文件, key 是加密的。 我想创建一个包含 PEM 格式的证书和加密 key 的 cer 文件,例如: -----BEGIN CERT
我正在尝试将 certifictae 和 key 写入 stm32 的闪存 EEPROM,我知道我们可以写入字符串,但是否有可能在闪存中写入整个证书(.der)。我正在使用 stm32f7 微 Con
我正在尝试将 X509 证书写入内存中的 DER 格式。将其写入文件非常有效。 我需要 PEM 格式的证书,不带“-----BEGIN PRIVATE KEY-----”页眉、页脚或换行符。我无法弄清
为什么模数用前导零填充?我正在阅读 PKCS#1 和 PKCS#8,但没有找到任何相关信息。 在 c# 中必须删除前导零,有人知道为什么吗? 在 http://etherhack.co.uk/asym
我有一个 X.509 证书,其中包含一组带有以下隐式 [0] 标记的数据: A0 81 C6 (value)... 我有一个标准文档的摘录: The IMPLICIT [0] tag is not u
如何从代表 DER 格式证书的十六进制字符串中提取通用名称?我需要显示证书的名称,以便客户端可以选择他们希望在 Web 应用程序中使用的证书。 限制: 我需要在网络浏览器中执行此提取客户端操作。 Op
使用 BounceCaSTLe 我可以使用以下代码。它生成一个 key 对并返回 ASN.1 DER 编码格式。 //Generate new key var generator = new RsaK
我最初有一个受密码保护的 PEM 文件,其中包含使用 BouncyCaSTLe 读入 Java 应用程序的私钥。 PEM 文件开头为 -----开始 RSA 私钥----- 这让我相信它是 PKCS#
在十六进制编辑器中是否存在某种 DER 数字签名? 我得到了一个二进制内容大约为 1.3 KB 的 .cer 文件。有人告诉我它是 DER 格式,我的阅读表明具有此扩展名的证书几乎都是 DER 或带有
我有一个应用程序需要安装证书,它使用 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:CERTIFICATE_URL]]
我正在尝试验证文件的签名。我按照这些说明生成证书: // generate a private key with size of 2048 bits openssl genrsa -out priva
我想创建一个能够对大整数(即 ECDSA signature r and s)进行 DER 编码和解码的 Google Go 应用程序。据我了解,我应该使用 asn1 package , 但我应该调用
我正在尝试为 Elasticsearch Searchguard 创建证书.一项要求是证书必须在 SAN 中包含 oid:1.2.3.4.5.5。我正在使用 GO 生成该证书。经过一些尝试和错误后,我
我需要从我的示例 ASN1 编译并生成一个 .DER 文件 BCAS DEFINITIONS ::= BEGIN BBCard ::= SEQUENCE { name IA5String (SIZ
我有一个 X509Certificate,我将它写入/打印到一个文件中,如下所示。 (我不是在写编码字节,因为我想阅读证书的内容) X509Certificate cer = generateCert
我正在尝试对安全网站进行 POST 调用: 步骤: 1) 转到https://www.mywebsite.com (供引用) 2) 将上述网站的 keystore 导出到C:\Program File
这是我的结构: SET OF SEQUENCE: INTEGER: XX INTEGER: YY 我的编码是这样的: 11 08 10 06 02 01 XX 02 01 Y
我见过很多问题,但没有一个真正提供好的答案,或者即使他们尝试了,他们也会假设提出问题的人并没有问他实际上在问什么。郑重声明,我不是在询问签名、加密或 PEM 文件! 就我而言,我将收到一个加密数据的小
我是一名优秀的程序员,十分优秀!