2017-11-11 568 views
2

我要提前說我對加密技術不瞭解太多(僅限基礎知識)。我正在嘗試實施Credential OpenHome服務,並且想要加密密碼以將其發送到設備。加密RSA/ECB/OAEPWithSHA-256AndMGF1Padding Swift

該設備提供C語言編寫一個函數返回一個公共密鑰字符串,看起來像這樣:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCzjFGuEKD0uWxzb47oRbiSP2uDwVJPeWU7m9VXi626V6lameTzdtwj2eYVZTIAsAW7yW4or2skn7oHqFG4GvhMzgMwoQjKFxeCPPFXRSotnt26AN1DhvFJp3V/d+MpmkzI07iWcD5eNe4EVNK9GSE4JOEHhJ/JYBVMiu04XE5aqwIDAQAB 

Android的實施已經做了和給出的規格是

RSA/ECB/OAEPWithSHA-256AndMGF1Padding

也有一個網站加密

時,讓 「指示」

http://wiki.openhome.org/wiki/Av:Developer:CredentialsService

到目前爲止我這些庫嘗試:

SwiftyRSA,海姆達爾,SwCrypt

我真的東西,我的主要故障是一個我不明白我有什麼我需要和最後如何使用swift來實現它。

最好在年底,我將有一個功能,如

func encryptMessage(message:String, whithPublicKey key:String)->String 

非常感謝你。

+0

不知道你的錯誤或障礙就是我的公鑰和私鑰,其中android上產生!該過程看起來像需要使用ECB分組密碼和OAEP填充中的RSA服務器共享的公鑰來生成密碼的加密消息並將其發送回服務器。 OAEPWithSHA-256AndMGF1Padding是非常不尋常的,所以只需檢查哪些庫支持這一點。 – carbonr

+0

公鑰由服務器給出。所以我假設服務器有一個用於解密消息的私鑰。我想知道如何使用提供的公鑰和規範來加密郵件。 –

+0

「我到目前爲止試過這些圖書館......」和?什麼地方出了錯?你的代碼在哪裏嘗試? –

回答

1

經過長時間的研究,我剛剛實現了我自己的解決方案,而不是使用庫並且不理解發生了什麼。知道會發生什麼總是件好事,而這種情況並非火箭科學。

在iOS上,如果您想加密/解密,您需要使用存儲在鑰匙串上的密鑰。如果,在我的情況下,我已經給了我的公鑰,我可以導入它,而且我也可以用私鑰做同樣的事情。請參閱我的HelperClass Here

然後,從只能從iOS的10,你可以調用此2種方法進行加密和解密

 let error:UnsafeMutablePointer<Unmanaged<CFError>?>? = nil 
     let plainData = "A Plain text...".data(using: .utf8) 
     if let encryptedMessageData:Data = SecKeyCreateEncryptedData(publicSecKey, .rsaEncryptionOAEPSHA256, plainData! as CFData,error) as Data?{ 
      print("We have an encrypted message") 

      let encryptedMessageSigned = encryptedMessageData.map { Int8(bitPattern: $0) } 
      print(encryptedMessageSigned) 

      if let decryptedMessage:Data = SecKeyCreateDecryptedData(privateSecKey, .rsaEncryptionOAEPSHA256, encryptedMessageData as CFData,error) as Data?{ 
       print("We have an decrypted message \(String.init(data: decryptedMessage, encoding: .utf8)!)") 
      } 
      else{ 
       print("Error decrypting") 
      } 

     } 
     else{ 
      print("Error encrypting") 
     } 

另外,如果你的iOS前10希望你具備的功能:

func SecKeyEncrypt(_ key: SecKey, 
      _ padding: SecPadding, 
      _ plainText: UnsafePointer<UInt8>, 
      _ plainTextLen: Int, 
      _ cipherText: UnsafeMutablePointer<UInt8>, 
      _ cipherTextLen: UnsafeMutablePointer<Int>) -> OSStatus 

func SecKeyDecrypt(_ key: SecKey, 
      _ padding: SecPadding, 
      _ cipherText: UnsafePointer<UInt8>, 
      _ cipherTextLen: Int, 
      _ plainText: UnsafeMutablePointer<UInt8>, 
      _ plainTextLen: UnsafeMutablePointer<Int>) -> OSStatus 

但是,這些給予較少的選項,他們相當搶手。

值得一提的是,使用

public static String createStringFromPublicKey(Key key) throws Exception { 
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key.getEncoded()); 
return new String(Base64.encode(x509EncodedKeySpec.getEncoded(), Base64.NO_WRAP), "UTF-8"); 

}

public static String createStringFromPrivateKey(Key key) throws Exception { 
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key.getEncoded()); 
return new String(Base64.encode(pkcs8EncodedKeySpec.getEncoded(), Base64.NO_WRAP), "UTF-8"); 

}