本文介绍了RSA_private_decrypt()随机失败:块类型不是02的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用openssl 0.9.6g与C ++。我已经生成了RSA公共/私人密钥对。我有基于1.0.0d版本的openssl的框架。我使用openssl.net公钥加密文本,当我尝试在C ++代码中解密时,我会随机失败。有时候我可以检索数据,但有些时候我得到以下错误:

 错误:0407106B:rsa例程:RSA_padding_check_PKCS1_type_2 :块类型不是02 

我不知道究竟发生了什么。如果我在openssl.net(最初加密的数据)中使用加密数据和私钥,我可以解密。



我的C ++代码是:

  const char * p = ----- BEGIN RSA私钥----- \\\
\
MIICXgIBAAKBgQDOx0DsbBURESmUVivbiHXqJdaJw8j3v27RC5ArUKIFbzgoqge + \\\
\
ZA9IcWA + WDjW4auzrzhK3sQTHRbkYCkSZsHdkHFu1viYDtK0Y1PUzm45xGAjOD2R\\\
\
TbYTMDyUAwDwERoIamPFLa /千欧/ 4AQnL6F2C6NQPtnkCITLsB0 + pbQLvLzwIDAQAB\\\
\
AoGBAJ7aDfNSuQBqieHOLVUyyTs / JEijowFJDG4GDxRnloDMR31d2iMWpXMGvmRG\\\
\
zCuwJIMeqgVCi6kPYOXDRepADM + zPo3Qh6hSS5AhzgY27xitjOqZVrJX7J / Y1N + i\\\
\
的Fd + GPvj7FuOvQTQw9MVkEDQYvTaC3hXVOS5akzqwes5gAL45AkEA6OMl4 + ROmDhV\\\
\
bBwif4sy4Em1MhJMU8RpWzk6f / ETp7gwejdgnU4oHOLz8d7j5rhBnC1mXlUnsaOA\\\
\
3rYimRqDdQJBAONMxKUXtbEOtP34IecM4uC0COXmYiTo9FdUoaoLOxVTBTo2antb\\\
\
aEZHvGJJFhvn4ucWtDju15rGCkwhNImDPbMCQQDLNjm / y14RUUaomTw5EtuH / okP\\\
\
cgwMJBzSrvMu5MuD1CLo4h155dL2scqvSRCQKEOH5c2njqKeTM + Mhl1WP15pAkEA\\\
\
0lYp6rREGDKJ3jMN / Gd6U4GGcmh9Dbbc1Sul5ls6M4N 95N6SLtDpb4 / WjgUgBnbR\\\
\
DBOAdubecYUt5kEcAtyF7QJAYU0OICV21B6t8lsoULHTYDOMm5P7PuMN6w3F + h0Z\\\
\
sdyMfD6pBKBw / bBNwApBaypIKK9leLdbebor23reKBi + 9w == \\\
\
----- END RSA PRIVATE KEY- ----;
strKey = p;
BIO * pBPK = NULL;
RSA * pPrivKey;
pBPK = BIO_new_mem_buf((void *)strKey.data(),-1);
pPrivKey = PEM_read_bio_RSAPrivateKey(pBPK,NULL,NULL,NULL);
BIO_free(pBPK);
unsigned char * from =(unsigned char *)strData.data();
unsigned char * to = new unsigned char [RSA_size(pPrivKey)];
int res = RSA_private_decrypt(RSA_size(pPrivKey),from,to,pPrivKey,1);

我的openssl.net代码是:

 串公钥= @----- BEGIN PUBLIC KEY ----- 
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOx0DsbBURESmUVivbiHXqJdaJ
w8j3v27RC5ArUKIFbzgoqge + ZA9IcWA + WDjW4auzrzhK3sQTHRbkYCkSZsHdkHFu
1viYDtK0Y1PUzm45xGAjOD2RTbYTMDyUAwDwERoIamPFLa /千欧/ 4AQnL6F2C6NQPt
nkCITLsB0 + pbQLvLzwIDAQAB
----- END PUBLIC KEY -----;
Encoding enc = Encoding.ASCII;
string text =1234;
byte [] msg = enc.GetBytes(text);
CryptoKey key = CryptoKey.FromPublicKey(publicKey,);
RSA rsa = key.GetRSA();
byte [] res = rsa.PublicEncrypt(msg,RSA.Padding.PKCS1);
rsa.Dispose();
strRes = Convert.ToBase64String(res);

Base64加密数据:

  xJTlJeLlm3mufFAdUWFUPfF8KmzFEpIL5K2ibFnnmq4zNECW1conDsWytwGCrqPKOlbmIuhBlg2cUqw6FC6ntyOBSwgRiUht1mBKK + 91e7M0h0jb7UTxEmpue + 3sVCgjIW6I9GJDo5EA9q5xZA7xzlBh8DGJqOatpLbOt + jJDOw = 

原始数据:1234



RSA_private_decrypt()返回-1。

解决方案

我最近一直在那里。
这还没有解决?如果是这样,可能会有所帮助。
您应该在加密过程中确保几件事。


  1. in_data_length,这取决于您使用的填充。如果使用PSCK1_PADDING,请确保它小于 sizeof(rsa_key) - 11 。与文档或手册页中的其他文件相同。

  2. out数据应指向大小等于 sizeof(rsa_key)的内存。
    以上事情在我的情况下工作,希望它可以是有帮助的


I am using openssl 0.9.6g with C++. I have generated RSA public/private keypair. I have openssl.net framework which is based on 1.0.0d version of openssl. I am encrypting text using public key with openssl.net and when I try to decrypt in my C++ code, I am getting random failure. Sometimes I am able to retrieve the data but some other times I am getting following error:

error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02

I am not sure what exactly is happening. If I use the encrypted data and the private key in openssl.net (which originally encrypted the data), I am able to decrypt.

My C++ code is:

const char *p="-----BEGIN RSA PRIVATE KEY-----\n\
MIICXgIBAAKBgQDOx0DsbBURESmUVivbiHXqJdaJw8j3v27RC5ArUKIFbzgoqge+\n\
ZA9IcWA+WDjW4auzrzhK3sQTHRbkYCkSZsHdkHFu1viYDtK0Y1PUzm45xGAjOD2R\n\
TbYTMDyUAwDwERoIamPFLa/kQ/4AQnL6F2C6NQPtnkCITLsB0+pbQLvLzwIDAQAB\n\
AoGBAJ7aDfNSuQBqieHOLVUyyTs/JEijowFJDG4GDxRnloDMR31d2iMWpXMGvmRG\n\
zCuwJIMeqgVCi6kPYOXDRepADM+zPo3Qh6hSS5AhzgY27xitjOqZVrJX7J/Y1N+i\n\
Fd+GPvj7FuOvQTQw9MVkEDQYvTaC3hXVOS5akzqwes5gAL45AkEA6OMl4+ROmDhV\n\
bBwif4sy4Em1MhJMU8RpWzk6f/ETp7gwejdgnU4oHOLz8d7j5rhBnC1mXlUnsaOA\n\
3rYimRqDdQJBAONMxKUXtbEOtP34IecM4uC0COXmYiTo9FdUoaoLOxVTBTo2antb\n\
aEZHvGJJFhvn4ucWtDju15rGCkwhNImDPbMCQQDLNjm/y14RUUaomTw5EtuH/okP\n\
cgwMJBzSrvMu5MuD1CLo4h155dL2scqvSRCQKEOH5c2njqKeTM+Mhl1WP15pAkEA\n\
0lYp6rREGDKJ3jMN/Gd6U4GGcmh9Dbbc1Sul5ls6M4N95N6SLtDpb4/WjgUgBnbR\n\
DBOAdubecYUt5kEcAtyF7QJAYU0OICV21B6t8lsoULHTYDOMm5P7PuMN6w3F+h0Z\n\
sdyMfD6pBKBw/bBNwApBaypIKK9leLdbebor23reKBi+9w==\n\
-----END RSA PRIVATE KEY-----";
strKey = p;
BIO *pBPK = NULL;
RSA *pPrivKey;
pBPK = BIO_new_mem_buf ( ( void* ) strKey.data(), -1 );
pPrivKey = PEM_read_bio_RSAPrivateKey ( pBPK, NULL, NULL, NULL );
BIO_free ( pBPK );
unsigned char* from = ( unsigned char* ) strData.data();
unsigned char* to = new unsigned char[ RSA_size ( pPrivKey )];
int res=RSA_private_decrypt(RSA_size ( pPrivKey ), from,to, pPrivKey, 1 );

And my openssl.net code is:

string publicKey = @"-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOx0DsbBURESmUVivbiHXqJdaJ
w8j3v27RC5ArUKIFbzgoqge+ZA9IcWA+WDjW4auzrzhK3sQTHRbkYCkSZsHdkHFu
1viYDtK0Y1PUzm45xGAjOD2RTbYTMDyUAwDwERoIamPFLa/kQ/4AQnL6F2C6NQPt
nkCITLsB0+pbQLvLzwIDAQAB
-----END PUBLIC KEY-----";
Encoding enc = Encoding.ASCII;
string text = "1234";
byte[] msg = enc.GetBytes(text);
CryptoKey key = CryptoKey.FromPublicKey(publicKey, "");
RSA rsa = key.GetRSA();
byte[] res = rsa.PublicEncrypt(msg, RSA.Padding.PKCS1);
rsa.Dispose();
strRes = Convert.ToBase64String(res);

Base64 encrypted data:

xJTlJeLlm3mufFAdUWFUPfF8KmzFEpIL5K2ibFnnmq4zNECW1conDsWytwGCrqPKOlbmIuhBlg2cUqw6FC6ntyOBSwgRiUht1mBKK+91e7M0h0jb7UTxEmpue+3sVCgjIW6I9GJDo5EA9q5xZA7xzlBh8DGJqOatpLbOt+jJDOw=

Original data: 1234

RSA_private_decrypt() returns -1.

解决方案

I have been there recently.Is this still unsolved ? If so it could be helpful.You should make sure several things during encrypted.

  1. "in_data_length", it depends on which padding you used. Making sure it smaller than sizeof(rsa_key) - 11 if PSCK1_PADDING used, same as others in docs or man pages.
  2. "out data" should point to the memory whose size equal to sizeof(rsa_key).Above things making worked in my case, hope it could be helpful

这篇关于RSA_private_decrypt()随机失败:块类型不是02的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:29