Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS:RSA使用公钥加密(带模数和指数)_Iphone_Ios_Xml_Encryption_Rsa - Fatal编程技术网

Iphone iOS:RSA使用公钥加密(带模数和指数)

Iphone iOS:RSA使用公钥加密(带模数和指数),iphone,ios,xml,encryption,rsa,Iphone,Ios,Xml,Encryption,Rsa,我正在尝试使用公钥对NSData进行RSA加密。公钥的格式如下: <RSAKeyValue> <Modulus>yOTe0L1/NcbXdZYwliS82MiTE8VD5WD23S4RDsdbJOFzCLbsyb4d+K1M5fC+xDfCkji1zQjPiiiToZ7JSj/2ww==</Modulus> <Exponent>AWAB</Exponent> </RSAKeyValue> 我似乎在任何地方都找不到明

我正在尝试使用公钥对NSData进行RSA加密。公钥的格式如下:

<RSAKeyValue>
  <Modulus>yOTe0L1/NcbXdZYwliS82MiTE8VD5WD23S4RDsdbJOFzCLbsyb4d+K1M5fC+xDfCkji1zQjPiiiToZ7JSj/2ww==</Modulus>
  <Exponent>AWAB</Exponent>
</RSAKeyValue>

我似乎在任何地方都找不到明确的答案。

哇,难怪要找到答案这么难。我在加密兔子洞里呆了两天,但这并不漂亮

捷径

使用。一个主要缺点是:成本为189美元O

艰难的道路

解析XML,用于生成模数和指数的公钥数据。如果可行,请使用该公钥创建一个伪密钥链follow,现在以SecKeyRef格式提取公钥,并将其传递给问题中的encryptRSA方法。最后,清除,删除虚拟钥匙链。理论上听起来不错,但我从未彻底测试过,如果你测试过,请告诉我

我想这对你有帮助! 您可以创建一个java文件,如: 此java函数将生成base64String的公钥

public static RSAPublicKey getPublicKey(String modulus, String exponent) {
    try {
        BigInteger b1 = new BigInteger(modulus,16);
        BigInteger b2 = new BigInteger(exponent,16);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
        return (RSAPublicKey) keyFactory.generatePublic(keySpec);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public static  String  encodePublicKey(byte[] encoded) throws Exception{
    BASE64Encoder base64Encoder= new BASE64Encoder();
    String s=base64Encoder.encode(encoded);
    return s;
你应该像这样使用:encodePublicKeypublicKey.getEncoded


明白了

我在不使用任何第三方LIB的情况下使用了以下使用公钥加密的方法,我想这可能会帮助那些在实现了它之后寻找相同方法的人,就像我一样:D

+(NSString *)encryptRSA:(NSString *)plainTextString key:(SecKeyRef)publicKey
{
    size_t cipherBufferSize = SecKeyGetBlockSize(publicKey);
    uint8_t *cipherBuffer = malloc(cipherBufferSize);
    uint8_t *nonce = (uint8_t *)[plainTextString UTF8String];
    SecKeyEncrypt(publicKey,
                  kSecPaddingPKCS1,
                  nonce,
                  strlen( (char*)nonce ),
                  &cipherBuffer[0],
                  &cipherBufferSize);
    NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];
    return [encryptedData base64EncodedStringWithOptions:0];
}

如何将xml解析为NSData对象?答案建议使用付费服务,并否认在开源/免费软件中没有其他替代服务。您建议如何在iOS上运行此功能?OP正在iOS中寻找答案,请重新查看您的答案并尝试提供一些相关代码。否则这个答案就没用了。。。
+(NSString *)encryptRSA:(NSString *)plainTextString key:(SecKeyRef)publicKey
{
    size_t cipherBufferSize = SecKeyGetBlockSize(publicKey);
    uint8_t *cipherBuffer = malloc(cipherBufferSize);
    uint8_t *nonce = (uint8_t *)[plainTextString UTF8String];
    SecKeyEncrypt(publicKey,
                  kSecPaddingPKCS1,
                  nonce,
                  strlen( (char*)nonce ),
                  &cipherBuffer[0],
                  &cipherBufferSize);
    NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];
    return [encryptedData base64EncodedStringWithOptions:0];
}