问题描述
我正在 ObjC 中编写代码库,用于创建 API 调用,该调用需要一些加密参数,而几乎不了解密码学: 1) 一个加密的字符串,参数 1 2)加密的IV 3)加密密钥 4)和其他一些参数
首先,我向服务器发出请求,服务器发送模数、指数和公钥。
我被告知使用指数和模数生成公钥,然后使用生成的公钥加密参数 1(如上所述)。
另一端 (Android) 使用 KeyFactory 生成公钥,并具有创建 IV 和执行其他操作的方法。
我使用了这里提到的方法RSA public key generation -- Swift,它为我提供了一个数据格式的公钥,稍后我将其转换为字符串。然后我使用 https://github.com/ideawu/Objective-C-RSA 中的这个代码(RSA.h 和 RSA.m)来加密我的字符串。但是加密确实失败了,在第 51 行,在 RSA.m (stripPublicKeyHeader:) 中。
我在这里尝试了很多帖子都没有成功,我想知道在生成公钥时我做错了什么。另外,关于这些参数,加密的IV是什么,加密的密钥可以是加密的公钥吗?
这是我在 ViewController.m 中的代码:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
Nsstring *modulusstring = @"t....w==";
Nsstring *exponentString = @"A..B";
NSData *modulusData = [modulusstring dataUsingEncoding:NSUTF8StringEncoding];
NSData *exponentData = [exponentString dataUsingEncoding:NSUTF8StringEncoding];
NSData *pubKey = [ViewController generateRSAPublicKeyWithModulus:modulusData exponent:exponentData];
Nsstring *pubKeyString = [pubKey base64EncodedStringWithOptions:NSUTF32StringEncoding];
NSLog(@"%@",pubKeyString);
Nsstring *encryptedString = [RSA encryptString:@"stringToEncrypt" publicKey:pubKeyString];
NSLog(@"%@",encryptedString);
}
+ (NSData *)generateRSAPublicKeyWithModulus:(NSData*)modulus exponent:(NSData*)exponent
{
const uint8_t DEFAULT_EXPONENT[] = {0x01,0x00,0x01,}; //default: 65537
const uint8_t UNSIGNED_FLAG_FOR_BYTE = 0x81;
const uint8_t UNSIGNED_FLAG_FOR_BYTE2 = 0x82;
const uint8_t UNSIGNED_FLAG_FOR_BIGNUM = 0x00;
const uint8_t SEQUENCE_TAG = 0x30;
const uint8_t INTEGER_TAG = 0x02;
uint8_t* modulusBytes = (uint8_t*)[modulus bytes];
uint8_t* exponentBytes = (uint8_t*)(exponent == nil ? DEFAULT_EXPONENT : [exponent bytes]);
//(1) calculate lengths
//- length of modulus
int lenMod = (int)[modulus length];
if(modulusBytes[0] >= 0x80)
lenMod ++; //place for UNSIGNED_FLAG_FOR_BIGNUM
int lenModHeader = 2 + (lenMod >= 0x80 ? 1 : 0) + (lenMod >= 0x0100 ? 1 : 0);
//- length of exponent
int lenExp = exponent == nil ? sizeof(DEFAULT_EXPONENT) : (int)[exponent length];
int lenExpHeader = 2;
//- length of body
int lenBody = lenModHeader + lenMod + lenExpHeader + lenExp;
//- length of total
int lenTotal = 2 + (lenBody >= 0x80 ? 1 : 0) + (lenBody >= 0x0100 ? 1 : 0) + lenBody;
int index = 0;
uint8_t* byteBuffer = malloc(sizeof(uint8_t) * lenTotal);
memset(byteBuffer,sizeof(uint8_t) * lenTotal);
//(2) fill up byte buffer
//- sequence tag
byteBuffer[index ++] = SEQUENCE_TAG;
//- total length
if(lenBody >= 0x80)
byteBuffer[index ++] = (lenBody >= 0x0100 ? UNSIGNED_FLAG_FOR_BYTE2 : UNSIGNED_FLAG_FOR_BYTE);
if(lenBody >= 0x0100)
{
byteBuffer[index ++] = (uint8_t)(lenBody / 0x0100);
byteBuffer[index ++] = lenBody % 0x0100;
}
else
byteBuffer[index ++] = lenBody;
//- integer tag
byteBuffer[index ++] = INTEGER_TAG;
//- modulus length
if(lenMod >= 0x80)
byteBuffer[index ++] = (lenMod >= 0x0100 ? UNSIGNED_FLAG_FOR_BYTE2 : UNSIGNED_FLAG_FOR_BYTE);
if(lenMod >= 0x0100)
{
byteBuffer[index ++] = (int)(lenMod / 0x0100);
byteBuffer[index ++] = lenMod % 0x0100;
}
else
byteBuffer[index ++] = lenMod;
//- modulus value
if(modulusBytes[0] >= 0x80)
byteBuffer[index ++] = UNSIGNED_FLAG_FOR_BIGNUM;
memcpy(byteBuffer + index,modulusBytes,sizeof(uint8_t) * [modulus length]);
index += [modulus length];
//- exponent length
byteBuffer[index ++] = INTEGER_TAG;
byteBuffer[index ++] = lenExp;
//- exponent value
memcpy(byteBuffer + index,exponentBytes,sizeof(uint8_t) * lenExp);
index += lenExp;
if(index != lenTotal)
NSLog(@"lengths mismatch: index = %d,lenTotal = %d",index,lenTotal);
NSMutableData* buffer = [NSMutableData dataWithBytes:byteBuffer length:lenTotal];
free(byteBuffer);
return buffer;
}
@end
任何帮助将不胜感激。谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)