我试图用公钥加密NSData。公钥采用这种格式:
<RSAKeyValue>
<Modulus>yOTe0L1/NcbXdZYwliS82MiTE8VD5WD23S4RDsdbJOFzCLbsyb4d+K1M5fC+xDfCkji1zQjPiiiToZ7JSj/2ww==</Modulus>
<Exponent>AWAB</Exponent>
</RSAKeyValue>在从XML字符串中提取模数和指数之后,如何从下面的方法中提取作为publicKey的publicKey?
+ (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,
kSecPaddingOAEP,
nonce,
strlen( (char*)nonce ),
&cipherBuffer[0],
&cipherBufferSize);
NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];
return [encryptedData base64EncodedString];
}我似乎哪儿也找不到确切的答案。
发布于 2013-10-24 06:09:19
难怪这么难找到答案。我在秘密兔子洞里呆了两天,那里不漂亮。
easy
使用Chilkat iOS RSA库。一个主要的缺点是:花费189美元!
The hard way
解析XML,使用基本编码规则-iOS生成一个模数和指数外的公钥数据。如果这有效,使用该公钥创建一个虚拟密钥链(遵循这里的示例代码),现在以SecKeyRef格式提取公钥并将其传递给问题中的encryptRSA方法。最后,清理,删除虚拟钥匙链。理论上听起来不错,但我从来没有对此进行过彻底的测试,如果你这样做了,请告诉我!
发布于 2019-05-27 14:10:34
我使用了以下方法来使用公钥加密,而不使用任何第三方库,在他们像我一样实现它之后,猜测它可能会帮助那些正在寻找公钥的人: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];
}发布于 2014-12-04 10:51:33
我想这会对你有帮助!您可以像file一样创建一个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;U应该使用类似:encodePublicKey(publicKey.getEncoded());
明白了!
https://stackoverflow.com/questions/19459359
复制相似问题