2011-04-28 132 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