soyersoyer / SwCrypt

RSA public/private key generation, RSA, AES encryption/decryption, RSA sign/verify in Swift with CommonCrypto in iOS and OS X
MIT License
720 stars 124 forks source link

RSA decrypt failure on iOS 8.1,please help #30

Closed dearwayne closed 6 years ago

dearwayne commented 6 years ago

Xcode: 9.3.1 Code:

let data = "Hello Test".data(using: .utf8)!
let (privateKey, publicKey) = try! CC.RSA.generateKeyPair(1024)
// encrypt
let encrypted = try! CC.RSA.encrypt(data, derKey: publicKey, tag: Data(), padding: .pkcs1, digest: .none)
// decrypt
do {
    let (decrypted,_) = try CC.RSA.decrypt(encrypted, derKey: privateKey, tag: Data(), padding: .pkcs1, digest: .none)
    print("decrypted:\(String(data:decrypted, encoding:.utf8)!)")
}catch{
    print("decrypt fail")
}

decypt failure on Simulator:iPhone 5s 8.1,but success on Simulator: iPhone SE 11.3.

CCCryptorStatus 
CCRSACryptorDecrypt(
    CCRSACryptorRef privateKey, 
    CCAsymetricPadding padding, 
    const void *cipherText, 
    size_t cipherTextLen,
    void *plainText, 
    size_t *plainTextLen, 
    const void *tagData, 
    size_t tagDataLen, 
    CCDigestAlgorithm digestType)
__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);

It should be support iOS 5.0 and upper,But why decrypt failure on iOS 8.1?

soyersoyer commented 6 years ago

Please send me what was the error.

dearwayne commented 6 years ago

Please send me what was the error.

The error: SwCrypt.swift:1139: [decrypt(_:derKey:tag:padding:digest:)] SwCryptTest.CC.CCError: decodeError (-4304)

soyersoyer commented 6 years ago

Thanks! It looks like it's a bug in the Apple's CommonCrypto or CoreCrypto components. The good news it disappeared in the iOS 8.3.

fukemy commented 2 years ago

i got same problem when decode base64 string like this:

jH1pRwsdUmuqFZIfUIFux25Dbk8LY7gyteiAlQXeW8nOirUIHzzYvadBkmMQNhGqB19bd74tnCbyv8iQwovUpZmxleCsYWFCgzqkx+jQ9TGE4AavwUxJcrdjkXc7AOfQtJHfcgHMVziKrKiZNXi3j3tPIAhA+xFxjZTcSdssIUhcWv9nuPJcjgj18++qd6nL07LVEkqyHqENhX8HnwbWJ8jjeavoO6MV03nCTG2zqOnLThreoTSXl0HX8nhKRim/sklYslYFTXtiMwSY/VtGE5nq7qHgndRDOdJk15w8pS36uuuYbBbhNah9dNT38aHUt0cpZKISMZSghcC6+K1RMg==

using RSA256 like server, i got data but can not convert to string

            guard let data = Data(base64Encoded: dataStr) else { return }
            let d = try? CC.RSA.decrypt(data, derKey: priv, tag: Data(), padding: .oaep, digest: .sha256)
            guard let decryptData = d else { return }
            print("data: \(String(data: decryptData.0, encoding: .utf8))")

always print nil