Created
January 7, 2020 03:38
-
-
Save SunXiaoShan/01da10401f8b8b00c712037df1ba7333 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 建立模擬的 Server public/private keys | |
let keyPair = try! SwiftyRSA.generate2048RSAKeyPairAdv() | |
let privateKey = keyPair.privateKey | |
let publicKey = keyPair.publicKey | |
inputPubkey.text = try! publicKey.base64String() | |
inputPrivkey.text = try! privateKey.base64String() | |
// 這裡簡化流程,正常情況下要拿對方公鑰加密,傳輸出去對方才能用私鑰解開 | |
let defaultEncPublicKey = try! PublicKey(base64Encoded: inputPubkey.text!) | |
let clear = try! ClearMessage(string: orgiTextView.text!, using: .utf8) | |
let encrypted = try! clear.encrypted(with: defaultEncPublicKey, padding: .PKCS1) | |
let EncData = encrypted.base64String | |
crypTextView.text = EncData | |
// 模擬 Server 把資料解密出來 (這邊非原作者加的,是小編在驗證得時候自己加的 debug log) | |
func runDecrypt() { | |
let privateKey = try! PrivateKey(base64Encoded: inputPrivkey.text!) | |
let encrypted = try! EncryptedMessage(base64Encoded: EncData) | |
let clear = try! encrypted.decrypted(with: privateKey, padding: .PKCS1) | |
// Then you can use: | |
let data = clear.data | |
let base64String = clear.base64String | |
let string = try! clear.string(encoding: .utf8) | |
print("Decrypt RSA: \(string)") | |
} | |
runDecrypt() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment