2011-04-28 135 views
3

我们尝试使用“RSA_public_encrypt()”方法(Symbian上的openSSL)执行RSA加密,但我们没有取得成功。 加密本身成功,但加密的文本(我们试图匹配到一个散列)不是它应该是 (在其他平台上,我们检查了这是我们现在它工作正常,我们在这里得到不同的值) 。 我们认为这可能是由于没有以正确格式提供给“RSA_public_encrypt()”方法的输入。没有填充的OpenSSL RSA加密无法正确加密

的代码:

#define RSA_E 0x10001L 
void Authenticator::Test(TDesC8& aSignature, TDesC8& aMessage) { 
RSA *rsa; 
char *plainkey; 
char *cipherkey; 
int buffSize; 

// create the public key 
BIGNUM * bn_mod = BN_new(); 
BIGNUM * bn_exp = BN_new(); 

const char* modulus2 = "137...859"; // 309 digits 
// Convert to BIGNUM 
int len = BN_dec2bn(&bn_mod, modulus2); // with 309 digits -> gives maxSize 128 (OK!) 
BN_set_word(bn_exp, RSA_E); 

rsa = RSA_new(); // Create a new RSA key 
rsa->n = bn_mod; // Assign in the values 
rsa->e = bn_exp; 
rsa->d = NULL; 
rsa->p = NULL; 
rsa->q = NULL; 

int maxSize = RSA_size(rsa); // Find the length of the cipher text 
// maxSize is 128 bytes (1024 bits) 

// session key received from server side, what format should this be in ??? 
plainkey = "105...205"; // 309 digits 

cipherkey = (char *)malloc(maxSize); 
memset(cipherkey, 0, maxSize); 
if (rsa) { 
    buffSize = RSA_public_encrypt(maxSize, (unsigned char *) plainkey, (unsigned char *) cipherkey, rsa, RSA_NO_PADDING); 

    unsigned long err = ERR_get_error(); 

    free(cipherkey); 
    free(plainkey); 
} 

/* Free */ 
if (rsa) 
    RSA_free(rsa); 

}

我们有一个普通的键是309个字符,但MAXSIZE是128个字节(RSA_NO_PADDING,所以输入具有等于模数的大小), 所以只有前128个字符被加密(不确定这是否正确?),这可能是为什么我们的加密文本不是它应该是的。或者还有别的我们不正确的做法? 那么什么是转换我们纯文本的正确方式,以便'plainkey'的所有数据都被加密?

我们也试着用一个长度为256个字符的十六进制字符串'9603cab ...'尝试过,但没有成功。 因此,如果正确转换为字节(128),可以使用它,如果是的话,该转换将如何工作?

任何帮助非常感谢,谢谢!

回答

1

看看这个文件,也许它可以帮助:https://gist.github.com/850935

+0

谢谢你,我不再工作在这个项目,所以我将无法对其进行测试。您的代码是专门针对Symbian还是纯C++? – user729103 2012-03-07 15:45:48

+1

Plain c调用openssl api – clyfe 2012-03-07 17:27:42