Tag: rsa

iOS SecKeyRef(公钥)发送给服务器

现在我的公钥存在一个问题,我使用SecKeyGeneratePair来生成公共和私有的。 现在我必须将我的公钥发送到服务器。 我用下面的方法将SecKeyRef转换为NSData,总是得到相同的公钥。 不过,我把它转换成base64格式并发送给服务器。 但它不工作,服务器(Java)开始抛出错误。 这是我的iOS代码: – (NSData *)getPublicKeyBits { OSStatus sanityCheck = noErr; NSData * publicKeyBits = nil; NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init]; // Set the public key query dictionary. [queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass]; [queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag]; [queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType]; [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData]; // Get the key bits. sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef […]

将PEM公钥读入iOS

我有一个由java使用这个代码生成的base64公钥: RSAPublicKeySpec rsaKS = new RSAPublicKeySpec(modulus, pubExponent); RSAPublicKey rsaPubKey = (RSAPublicKey) kf.generatePublic(rsaKS); byte[] encoded = rsaPubKey.getEncoded(); String base64 = Base64.encodeToString(encoded, Base64.DEFAULT); Log.e(null, "base64: " + base64); 这导致Base64string。 在OSX中,我可以使用这个代码得到一个SecKeyRef: // Create the SecKeyRef using the key data CFErrorRef error = NULL; CFMutableDictionaryRef parameters = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL); CFDictionarySetValue(parameters, kSecAttrKeyType, kSecAttrKeyTypeRSA); CFDictionarySetValue(parameters, kSecAttrKeyClass, kSecAttrKeyClassPublic); SecKeyRef keyRef […]

RSA使用公钥encryption

我正在写iOS应用程序。 服务器将RSA公钥发送给应用程序。 现在应用程序必须使用RSAalgorithmencryption一些信息 请给我一些参考。 谢谢

我如何在iOS中使用PKCS8 RSA DER私钥?

在运行时,我的iOS应用程序接收一个由公有私有RSA密钥对的文件,由其他人的Java生成: KeyPairGenerator keygenerator; keygenerator = KeyPairGenerator.getInstance("RSA"); keygenerator.initialize(4096); KeyPair keypair = keygenerator.generateKeyPair(); PrivateKey privateKey = keypair.getPrivate().getEncoded(); PublicKey publicKey = keypair.getPublic().getEncoded(); 我已经成功地读取和使用公钥,使用这种方法 ,从密钥剥去一些序言。 我现在想要使用私钥 。 同样的方法不起作用,我认为序言是不同的。 博客build议它导入PKCS#1 PEM密钥,但接着说它们是二进制的,所以我认为它们只是Base64编码的DER密钥。 我还发现,也许我所拥有的密钥是PKCS#8编码。 当然可以使用 openssl pkcs8 -nocrypt -inform der < pk8.der > pvt.pem 在示例私钥和openssl不抱怨。 公钥是PKCS#1还是私有PKCS#8? 但是我真的想要使用CommonCrypto和安全框架,而不是在OpenSSL上进行链接。 在Mac OS上有libsecurity中的函数来读取PKCS#8,但是这还没有到iOS。 老实说,我尝试阅读源代码,但是我无法弄清楚他们究竟是在什么地方剥下钥匙。 [TL; DR]如何从DER私钥中剥离版本和algorithm PKCS#8字段,并使用CommonCrypto或一些C / C ++ / ObjC获取普通密钥?

将XML Dsig格式转换为DER ASN.1公钥

我正在处理一个iPhone应用程序,它从ASP.NET web服务中以如下格式检索RSA公钥: <RSAKeyValue> <Modulus>qdd0paiiBJ+xYaN4TKDdbEzrJJw9xlbRAltb5OPdegjLoW60yOjL/sni52WVsGC9QxpNitZR33dnUscmI0cTJoxkXypPjbD94UpH+p4el2tuKBypHlE7bERApuUp55y8BiRkbQNFH8smZFWDwtIc/PsJryeGf8fAryel8c5V3PU=</Modulus> <Exponent>AQAB</Exponent> </RSAKeyValue> 然后我需要将这个响应转换成适当格式的NSData * (从一些激烈的谷歌search,很可能是'ASN.1 DER'二进制格式。我已经有代码将两个部分从Base64表示转换为原始二进制值,但我不能为我的生活找出一个合理的方式来创build一个一体化的二进制密钥。 等待密钥的代码是Apple的CryptoExercise示例项目( 此处的代码)的SecKeyWrapper类的-addPeerPublicKey:(NSString *) keyBits:(NSData *)方法。 我将非常乐意以另一种方式实现这一点 – 我需要的只是encryption一个string(不需要解密)。 据我所知,虽然内置的安全框架有我所需要的,如果我能够缩小这种格式的差距。 如果有一种方法来转换密钥,并从webservice发送Base64编码,那也适用于我 – 但是我找不到ASN.1编码它的任何方法。

如何使一个有效的p12文件被SecPKCS12Import正确导入

我已经解决了将XML RSA私钥转换为PEM文件的先决条件问题,但遇到导入P12私钥时收到空数据的另一个问题。 以下是我的步骤: 将PEM文件转换为P12文件 openssl> pkcs12 -export -in rsa.pem -inkey rsa.pem -out rsa.p12 -nocerts 阅读P12文件到iOS项目 NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"MyPrivateKey" ofType:@"p12"]; NSData *p12data = [NSData dataWithContentsOfFile:path]; if (![self getPrivateKeyRef]) RSAPrivateKey = getPrivateKeywithRawKey(p12data); 导入P12私钥 SecKeyRef getPrivateKeywithRawKey(NSData *pfxkeydata) { NSMutableDictionary * options = [[[NSMutableDictionary alloc] init] autorelease]; // Set the public key query dictionary //change […]

iOS – 从指数+模数创buildSecKeyRef

我想通过指数和模数作为私钥来解密iPhone上的RSA编码的blob。 在Java中(使用javax.crypto),可以通过如下代码轻松实现: // 1) key RSAPublicKeySpec keySpec = new RSAPublicKeySpec(myModulus, myPublicExponent); KeyFactory fact = KeyFactory.getInstance("RSA"); Key pubKey = fact.generatePublic(keySpec); // 2) cypher Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, keySpec); // 3) use cypher to decode my block to an output stream 但与iPhone安全API我不能创build一个SecKeyRef(密钥),而不是通过生成一对或导入证书,我没有/要。 有没有办法创build一个模数+指数手动键? 如果是这样,你可以给我一个线索,如何? 提前致谢

使用RSA在iOS上签名和validation

如何使用RSA密钥在iOS上签名和validation一些数据(最好使用系统自己的libcommonCrypto )?

从base64编码的string获取SecKeyRef

我正在开发一个iOS应用程序,我得到一个base64编码的公钥,例如: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3gn +口令是tJ1 + PbP0GHa6hmM35WsVyibpypWAwRuBYY4MGfh3VWoXgiyiLo5HJTW1eR9BUFq3z + yOG1rwzSabZ8I4zneWm0kH7xErSjNrMbmjirbL7e6TQNa1ujP / x4x9XVbqf3vIsNVs19kn / QSX / HGzd5Ct3TGAo0AT0T4JwkCfciwIDAQAB 我想用这个公钥编码一些文本,但我找不到一种方法来将此string转换为有用的公钥。 我需要做什么?

从密钥链中的密钥对中提取公钥

问题:检索公钥 对于iPhone应用程序,我需要创build一个rsa密钥对,将其存储在密钥链中并检索公钥。 幸运的是,苹果发布了一个Crypto练习示例,可以find我需要的一切( SecKeyWrapper类,函数generateKeyPair和getPublicKeyBits )。 但是在尝试使用这些函数之后,对于不同的密钥对(而不是不同的密钥对的不同公钥),我总是得到相同的输出。 我的generateKeyPair和getPublicKeyBits函数的实现: 我首先通过调用generateKeyPairWithKeySizeInBits (这似乎工作正常),然后我用getPublicKeyBits和NSLog提取公钥的他们…创build一个密钥对… – (void) generateKeyPairWithKeySizeInBits:(int)bits withPublicIdentifier:(NSString *)publicIdentifier andPrivateIdentifier:(NSString *)privateIdentifier { NSLog(@"begin generating key…"); OSStatus status = noErr; NSMutableDictionary* privateKeyAttr = [[NSMutableDictionary alloc] init]; NSMutableDictionary* publicKeyAttr = [[NSMutableDictionary alloc] init]; NSMutableDictionary* keyPairAttr = [[NSMutableDictionary alloc] init]; NSData* publicTag = [publicIdentifier dataUsingEncoding:NSUTF8StringEncoding]; NSData* privateTag = [privateIdentifier dataUsingEncoding:NSUTF8StringEncoding]; SecKeyRef publicKey = […]