[ios] 암호화 알고리즘 성능

iOS 앱을 개발할 때 데이터의 보안과 암호화는 매우 중요합니다. iOS 플랫폼에서는 데이터를 암호화하기 위한 여러 가지 알고리즘을 제공하고 있습니다. 이번 포스트에서는 iOS에서 지원하는 주요 암호화 알고리즘의 성능에 대해 살펴보겠습니다.

1. AES (Advanced Encryption Standard)

AES는 현대적인 대칭키 블록 암호화 알고리즘으로 매우 높은 보안 수준을 제공합니다. iOS에서는 AES-128, AES-192, AES-256 세 가지 키 길이를 지원하며, CommonCrypto 라이브러리를 사용하여 구현할 수 있습니다.

AES의 성능은 플랫폼에 따라 다를 수 있지만, iOS 기기는 AES를 하드웨어 수준에서 지원하여 매우 뛰어난 성능을 보여줍니다.

아래는 Swift에서 CommonCrypto를 사용하여 AES를 암호화 및 복호화 하는 예시 코드입니다.

import CommonCrypto

func encryptAES(data: Data, key: Data, iv: Data) throws -> Data {
    var encryptedBytes = Data(count: data.count + kCCBlockSizeAES128)
    var bytesEncrypted = 0
    
    let status = key.withUnsafeBytes { keyBytes in
        iv.withUnsafeBytes { ivBytes in
            data.withUnsafeBytes { dataBytes in
                encryptedBytes.withUnsafeMutableBytes { encryptedBytes in
                    CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding),
                            keyBytes.baseAddress, key.count, ivBytes.baseAddress,
                            dataBytes.baseAddress, data.count,
                            encryptedBytes.baseAddress, encryptedBytes.count,
                            &bytesEncrypted)
                }
            }
        }
    }
    
    guard status == kCCSuccess else { throw CryptoError.encryptFailed }
    encryptedBytes.removeSubrange(bytesEncrypted..<encryptedBytes.count)
    return encryptedBytes
}

func decryptAES(data: Data, key: Data, iv: Data) throws -> Data {
    var decryptedBytes = Data(count: data.count + kCCBlockSizeAES128)
    var bytesDecrypted = 0
    
    let status = key.withUnsafeBytes { keyBytes in
        iv.withUnsafeBytes { ivBytes in
            data.withUnsafeBytes { dataBytes in
                decryptedBytes.withUnsafeMutableBytes { decryptedBytes in
                    CCCrypt(CCOperation(kCCDecrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding),
                            keyBytes.baseAddress, key.count, ivBytes.baseAddress,
                            dataBytes.baseAddress, data.count,
                            decryptedBytes.baseAddress, decryptedBytes.count,
                            &bytesDecrypted)
                }
            }
        }
    }
    
    guard status == kCCSuccess else { throw CryptoError.decryptFailed }
    decryptedBytes.removeSubrange(bytesDecrypted..<decryptedBytes.count)
    return decryptedBytes
}

2. RSA (Rivest–Shamir–Adleman)

RSA는 공개키 암호 시스템으로, 공개키와 개인키를 사용하여 데이터를 암호화 및 복호화합니다. iOS에서는 Security 프레임워크를 통해 RSA 암호화를 구현할 수 있습니다.

RSA는 고정된 길이의 데이터를 다루므로 큰 파일이나 긴 데이터의 암호화에는 적합하지 않을 수 있습니다. 또한 RSA 암호화의 성능은 대칭키 암호화인 AES와 비교했을 때 상대적으로 느릴 수 있습니다.

아래는 Swift에서 Security 프레임워크를 사용하여 RSA 암호화 키 쌍을 생성하는 예시 코드입니다.

import Security

func generateRSAKeyPair() {
    let parameters: [String: Any] = [
        kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
        kSecAttrKeySizeInBits as String: 2048
    ]
    var publicKey, privateKey: SecKey?
    _ = SecKeyGeneratePair(parameters as CFDictionary, &publicKey, &privateKey)
    // Use publicKey and privateKey as needed
}

결론

iOS에서는 AES와 RSA 암호화 알고리즘을 사용하여 데이터의 보안을 유지할 수 있습니다. AES는 대칭키 암호화로 빠르고 효율적이며, RSA는 공개키 암호화로 안전한 통신을 구현할 수 있습니다. 적절한 상황에 맞게 암호화 알고리즘을 선택하여 iOS 앱의 보안을 강화해보세요.

참고 자료