ios 中使用openSSL 使用模数及指数生成公钥解密问题


我能获取到RSA得模数以及指数。通过这两个生成一个公钥来解密密文。现在问题是我自己写得demo是OK得,但是公司项目缺解密不出啦~~~(公司项目密文74b0d3cc0c7a8e7071fe0ae202dac9f6087e761d9b15373aaa97674d94f57f02ae16d0be96233128fe4a252c783a75adc5dba2ef2495c58e09a6d7c7ecb9b80ad6c3437f9900b6747f9ae575b7e0bcf4ac446deb228759a4322ca23cd51343f77516e4aea13a694f0f0094645b239e625ee92ab1d28977b21d9f35e43d43ab3d
模数95661960577075768790096792106821175856028442640972984901594815533550049746127121287770126940894261001362600363761669199475323038239404032242125755549658719828449202656915146789362334844181779031719459519671074556496317495618814676654350531172002282992916865745528076845844430431070809018375732684684570966203
指数65537)

   
  - (NSString*)decrypt:(NSString*)encryText modulus:(NSString*)mod exponent:(NSString*)exp
  
{

NSString * hexString = encryText;
int hexStringLength= [hexString length] / 2;

//unsigned char enc_bin[144];
unsigned char dec_bin[hexStringLength];
//int enc_len;
int dec_len;
RSA * rsa_pub = RSA_new();


const char *N=[mod UTF8String] ;
const char *E=[exp UTF8String];



char * myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
bzero(myBuffer, [hexString length] / 2 + 1);
for (int i = 0; i < [hexString length] - 1; i += 2) {
unsigned int anInt;
NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
[scanner scanHexInt:&anInt];
myBuffer[i / 2] = (char)anInt;
}

printf("Mybuffer: %s",myBuffer);

if (!BN_dec2bn(&rsa_pub->n, N)) {
printf("NO CARGO EL MODULO");
}
printf(" N: %s\n", N);
printf(" n: %s\n", BN_bn2dec(rsa_pub->n));


if (!BN_dec2bn(&rsa_pub->e, E)) {
printf("NO CARGO EL EXPONENTE");
}
printf(" E: %s\n", E);
printf(" e: %s\n", BN_bn2dec(rsa_pub->e));

printf("public key size : %d bits\n", RSA_size(rsa_pub));
NSMutableString *entring = [NSMutableString stringWithCapacity:128];
for (int i = 0; i < hexStringLength; i++) {
[entring appendFormat:@"%x", myBuffer[i]];
}
/* decrypt */
if ((dec_len = RSA_public_decrypt(hexStringLength, (unsigned char*)myBuffer, dec_bin, rsa_pub,RSA_NO_PADDING))<0) {
printf("NO\n ");
}
printf("decrypted data:\n %s", dec_bin);
// print_hex(dec_bin, dec_len);

NSMutableString *decryptString = [[NSMutableString alloc] initWithBytes:dec_bin
length:strlen((char *)dec_bin)
encoding:NSUTF8StringEncoding];
return decryptString;
}

ios openssl

東方妖女乱舞 10 years, 4 months ago

Your Answer