【发布时间】:2021-07-02 00:18:42
【问题描述】:
今天,我使用 openssl 实现了 rsa 加密逻辑。没有像openssl这样的外部库有可能吗?使用由模数和指数组成的公钥。
我的源码大致是这样的。
-(NSString *)hexWithData:(unsigned char *)data ofLength:(NSUInteger)len
{
NSMutableString *tmp = [NSMutableString string];
for (NSUInteger i = 0; i < len; i++) {
[tmp appendFormat:@"%02x", data[i]];
}
return tmp;
}
-(void)EncryptionWithRSA
{
RSAGenWithSecurity *akClass = [[RSAGenWithSecurity alloc] init];
NSString *plainText = [akClass ParseJWT];
const char *plain = [plainText UTF8String];
RSA *publickey = [self GenKeyWithRSA];
int rsa_length = RSA_size(publickey);
unsigned char *crip[rsa_length];
NSString *result = [[NSString alloc] init];
int iRsaRet = RSA_public_encrypt(strlen(plain), (const unsigned char *)plain, (unsigned char *)crip, publickey, RSA_PKCS1_PADDING);
if(iRsaRet <= 0) {
NSLog(@"encrypt failed");
result = @"";
} else {
result = [self hexWithData:crip ofLength:rsa_length];
// result = [NSString stringWithUTF8String:(char *)crip];
NSLog(@"encrypt success: %@", result);
}
}
很抱歉通过翻译问你一个不成熟的问题。
【问题讨论】:
标签: objective-c public-key-encryption modulo exponent