简体   繁体   English

如何使用模数和指数生成公钥?

[英]How to generate Publickey with use of modulus and an exponent?

Hi I am new for developing the ios application.嗨,我是开发 ios 应用程序的新手。 I have a modulus and an exponent and i need to generate SecKey and then use this to encrypt some data (RSA encryption).我有一个模数和一个指数,我需要生成 SecKey,然后使用它来加密一些数据(RSA 加密)。 Please any one help in swift.请任何人迅速提供帮助。

You should not implement crypto algorithms or key generation by yourself (not being an expert in cryptography).您不应该自己实现加密算法或密钥生成(不是密码学专家)。 Where security matters, use mature and well-known libraries and tools.如果安全很重要,请使用成熟且知名的库和工具。

On iOS it's worth checking out SecKey API ( SecKeyEncrypt(_:_:_:_:_:_:) and so on).在 iOS 上,值得查看 SecKey API( SecKeyEncrypt(_:_:_:_:_:_:)等等)。 It was mentioned at WWDC 2016 Session 706 starting at 16:10 . 在 16:10开始的 WWDC 2016 Session 706 上提到了它。

You may find useful investigating the CryptoCompatibility example project which "shows common cryptographic operations using Apple APIs."您可能会发现调查CryptoCompatibility示例项目很有用,该项目“显示了使用 Apple API 的常见加密操作”。

As a cross-platform solution you can use OpenSSL which also provides RSA API .作为跨平台解决方案,您可以使用 OpenSSL,它也提供RSA API

A few similar questions have been asked/answered already, here's one where you can take a modulus and exponent and get a .PEM format out of it: Generate RSA Public Key from Modulus and Exponent已经提出/回答了一些类似的问题,这里有一个您可以使用模数和指数并从中获得 .PEM 格式的问题: 从模数和指数生成 RSA 公钥

Here's another, although this one uses OpenSSL: https://stackoverflow.com/a/31010530/209855这是另一个,虽然这个使用 OpenSSL: https : //stackoverflow.com/a/31010530/209855

Additionally, you've probably run into some people recommending https://github.com/StCredZero/SCZ-BasicEncodingRules-iOS like here https://stackoverflow.com/a/10643894/209855此外,您可能会遇到一些推荐https://github.com/StCredZero/SCZ-BasicEncodingRules-iOS 的人,比如这里https://stackoverflow.com/a/10643894/209855

But if you're like me, nothing actually worked or gave you exactly what you wanted.但如果你像我一样,没有什么真正起作用或给你你想要的东西。

If you read through the issues on that github repo, you'll find something broke around iOS 8 and it no longer generates the correct data.如果您通读该 github 存储库上的问题,您会发现 iOS 8 出现了一些问题,它不再生成正确的数据。

However, someone posted a fix for this: https://github.com/Meniny/Meniny.github.io/blob/5895a2d51502881a7d6cda418beafa546874dfa7/_posts/2017-08-12-RSA_public_key_with_modulus_and_exponent.md I'll reproduce the code here, in case it disappears in the future.然而,有人为此发布了一个修复程序: https : //github.com/Meniny/Meniny.github.io/blob/5895a2d51502881a7d6cda418beafa546874dfa7/_posts/2017-08-12-RSA_public_key_with_modulus_and_exponent,我在这里复制代码。它在未来消失。

+ (NSData * __nullable)generateRSAPublicKeyWithModulus:(NSData * __nonnull)modulus exponent:(NSData * __nonnull)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;
}

And last but not least, to generate the RSA Public Key from a Modulus and Exponent in Swift 3:最后但并非最不重要的一点是,从 Swift 3 中的模数和指数生成 RSA 公钥:

public func encrypt(_ string: String, modulus: String, exponent: String) -> String? {
  if let modData = Data(base64Encoded: modulus),
    let expData = Data(base64Encoded: exponent),
    let keyData = PublicKeyRSA.generatePublicKey(withModulus: modData, exponent: expData) {
      /// encrypt...
  }
}

PublicKeyRSA.generatePublicKey is returning the data in bytes, not returning the public key. PublicKeyRSA.generatePublicKey 以字节为单位返回数据,而不是返回公钥。 We have to use the public keys to encrypt our data.我们必须使用公钥来加密我们的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM