RSA公钥生成 - Swift [英] RSA public key generation -- Swift

查看:538
本文介绍了RSA公钥生成 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Azure键库发送 e n 参数,这些参数等于RSA模数( n )和RSA public exponent( e < STRONG>)。使用第三方库可以轻松创建,但输出值不会在没有模数和指数的情况下生效。



我需要创建这两个部分的RSA公钥。我必须创建RSA OAEP。
有没有人遇到类似的问题?任何想法如何实现?



我已尝试使用 SCZ- BasicEncodingRules-iOS ,但是输出的代码不正确。



这里是代码:

 让moduleString:字符串= MK-g0TLMqtefmosgBSTQi3dWh8h-rn4lQA8sQgNs_Gkf5TvgKWtYGJ4jRGUU-eK2bmyAAomVUojYBBlRYBkTRekm99DlD9T6U9yI3v11pZVl3yQgVXBEkiTZug3Inn_IAOGPQ3Q5OT6fEj1sRRxmMv93CQukQguSKuU4v2tmElgvyhg_eKIQbNx0JRCI4-1Z5GazxNjtwk7tWcA6PAbV0zZe2AaW0TlYVil_U8NckgHBguBoTHqVAbzb-MNA-HRa4QlBmdemcSaiDr5GikoOdmQ1-Lu6koqUkepx16pgqPvVw3o_NuXAZCS37c7bfgtXWTJcAIjiQaEyRcIV2bXsUQ 

设指数:字符串= AQAB

让moduleData:NSData! =(moduleString
as NSString).dataUsingEncoding(NSUTF8StringEncoding)as NSData!
让expData:NSData! =(指数
作为NSString).dataUsingEncoding(NSUTF8StringEncoding)as NSData!
let arrayaaa:NSArray = [moduleData,expData]
let key:NSData = arrayaaa.berData()


解决方案

所以,我找到了一个解决方案。不幸的是,对于iOS 8及更高版本,SCZ-BasicEncodingRules-iOS 具有错误的解码算法。它输出的密钥具有不正确的前缀。



如果你面临同样的问题,这里是一个帮助我的解决方案,但它是 Objective-C 码。 原始来源

$ b $ (NSData *)模数指数:(NSData *)指数
{
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 * modesBytes =(uint8_t *)[模数字节];
uint8_t * exponentBytes =(uint8_t *)(exponent == nil?DEFAULT_EXPONENT:[exponent bytes]);

//(1)计算长度
// - 模块长度
int lenMod =(int)[模数长度];
if(modulusBytes [0]> = 0x80)
lenMod ++; //位置为UNSIGNED_FLAG_FOR_BIGNUM
int lenModHeader = 2 +(lenMod> = 0x80?1:0)+(lenMod> = 0x0100?1:0);
// - 指数长度
int lenExp = exponent == nil? sizeof(DEFAULT_EXPONENT):(int)[exponent length];
int lenExpHeader = 2;
// - 身体长度
int lenBody = lenModHeader + lenMod + lenExpHeader + lenExp;
// - 总长度
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,0x00,sizeof(uint8_t)* lenTotal);

//(2)填充字节缓冲区
// - 序列标签
byteBuffer [index ++] = SEQUENCE_TAG;
// - 总长度
如果(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;
// - 模数长度
如果(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;
// - 模数值
if(modulusBytes [0]> = 0x80)
byteBuffer [index ++] = UNSIGNED_FLAG_FOR_BIGNUM;
memcpy(byteBuffer + index,modulusBytes,sizeof(uint8_t)* [模数长度]);
index + = [模数长度];
// - 指数长度
byteBuffer [index ++] = INTEGER_TAG;
byteBuffer [index ++] = lenExp;
// - 指数值
memcpy(byteBuffer + index,exponentBytes,sizeof(uint8_t)* lenExp);
index + = lenExp;

if(index!= lenTotal)
NSLog(@length mismatch:index =%d,lenTotal =%d,index,lenTotal);

NSMutableData * buffer = [NSMutableData dataWithBytes:byteBuffer length:lenTotal];
free(byteBuffer);

返回缓冲区;
}

该算法与标准Java KeyFactory 生成类匹配。


Azure key vault sends e and n parameters which equals RSA modulus(n) and RSA public exponent(e). It's easy to create with 3rd party libraries, but output value wouldn't be valid without modulus and exponent.

I need to create RSA public key of these two parts.Then I have to create RSA OAEP. Has anyone encountered a similar problem? Any idea how to achieve this?

I have tried to use SCZ-BasicEncodingRules-iOS, but it output incorrect code.

Here is code:

let moduleString: String = "mK-g0TLMqtefmosgBSTQi3dWh8h-rn4lQA8sQgNs_Gkf5TvgKWtYGJ4jRGUU-eK2bmyAAomVUojYBBlRYBkTRekm99DlD9T6U9yI3v11pZVl3yQgVXBEkiTZug3Inn_IAOGPQ3Q5OT6fEj1sRRxmMv93CQukQguSKuU4v2tmElgvyhg_eKIQbNx0JRCI4-1Z5GazxNjtwk7tWcA6PAbV0zZe2AaW0TlYVil_U8NckgHBguBoTHqVAbzb-MNa-HRa4QlBmdemcSaiDr5GikoOdmQ1-Lu6koqUkepx16pgqPvVw3o_NuXAZCS37c7bfgtXWTJcAIjiQaEyRcIV2bXsUQ"

    let exponent: String = "AQAB"

    let moduleData: NSData! = (moduleString
        as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
    let expData: NSData! = (exponent
        as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
    let arrayaaa: NSArray = [moduleData, expData]
    let key: NSData = arrayaaa.berData()

解决方案

So, I have found a solution. Unfortunately SCZ-BasicEncodingRules-iOS has wrong decoding algorithm for iOS 8 and above. It outputs key with incorrect prefix.

If you faced with the same issue, here is a solution that helped me, but it's Objective-C code. Original source:

   + (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, 0x00, 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;
}

This algorithm matches with standard Java KeyFactory generation class.

这篇关于RSA公钥生成 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆