2014-10-09 39 views
2

我想在iOS OSX中以編程方式創建證書請求(我將發送到服務器),而不使用openSSL。我已經成功創建了RSA密鑰對,但在完成剩下的任務時失敗了。我的代碼完全符合我的需求,但它是用Java編寫的,並且想知道是否有人幫助我將其轉換爲目標c。ios以編程方式創建證書請求

下面是Java代碼:

 test.generateKeys(); // generate RSA key pair 

     PrivateKey privateKey = test.keys.getPrivate(); 
     PublicKey publicKey = test.keys.getPublic(); 

     SecureRandom sr = new SecureRandom(); 
     String token = "123456"; // dummy token 
     String uuid = "4670ff33-d9f7-4026-957d-25c00e4dec54"; // dummy uuid 

     // with Bouncy Castle 
     ContentSigner signGen = new JcaContentSignerBuilder("SHA1withRSA").setSecureRandom(sr).build(privateKey); 
     X500Principal subject = new X500Principal("O=" + token + ", CN=" + uuid); 
     PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(subject, publicKey); 
     PKCS10CertificationRequest request = builder.build(signGen); 

     String bc = Hex.encodeHexString(request.getEncoded()); 
     System.out.println(PEMtoString(request)); 

我不是加密很好,對加密層蘋果正在開發的文檔是相當差的記錄,以便我在這裏有點失落。我遇到了很多類似的樣本,但總是缺少一些東西。 Thx提前。

+0

強烈建議您使用OpenSSL的。如果沒有,準備好幾個星期的努力。請參閱http://stackoverflow.com/questions/14741512/creating-pem-file-programmatically-in-objective-c – 2014-10-10 01:39:29

+0

我會使用openSSL,但據我瞭解,蘋果是開放的使用OpenSSL的行爲新的加密層,這是令人沮喪的他們正在開發,所以我不想編程一些將來會被拒絕的東西。像那樣 – AntonijoDev 2014-10-14 13:33:21

回答

5

如果有人在同樣的問題上絆倒,這裏是使用蘋果公用密碼層(無openSSL)的解決方案。

https://github.com/ateska/ios-csr

無需幾周編碼只是一個簡單的包含。

SCCSR *sccsr = [[SCCSR alloc] init]; 
    sccsr.commonName = @"some name"; 
    sccsr.organizationName = @"some organisation"; 
// // aditional data you can set 
// sccsr.countryName = @""; 
// sccsr.organizationalUnitName = @""; 
// sccsr.subjectDER = nil; 
// // 
//  
    NSData *certificateRequest = [sccsr build:pPublicKey privateKey:privateKey]; 

    NSString *str = [certificateRequest base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; 

    NSString *strCertificateRequest = @"-----BEGIN CERTIFICATE REQUEST-----\n"; 
    strCertificateRequest = [strCertificateRequest stringByAppendingString:str]; 
    strCertificateRequest = [strCertificateRequest stringByAppendingString:@"\n-----END CERTIFICATE REQUEST-----\n"]; 

SCCSR.h - >從網上下載提供的鏈接