You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Encrypts a string using RSA and encodes the result in Base64.
/// - Parameter password: The password string to encrypt.
/// - Returns: The Base64 encoded encrypted password, or nil if an error occurs.
static func getEncryptedPasswordBase64(password: String) -> String? {
let publicKeyPEM = "........"
func publicKey(from pemEncoded: String) -> SecKey? {
// Decode the base64 string to data.
guard let data = Data(base64Encoded: publicKeyPEM) else { return nil }
// Define the attributes for importing the key.
let options: [String: Any] = [
kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
kSecAttrKeyClass as String: kSecAttrKeyClassPublic,
kSecAttrKeySizeInBits as String: 2048
]
// Create a SecKey from the data.
var error: Unmanaged<CFError>?
guard let key = SecKeyCreateWithData(data as CFData, options as CFDictionary, &error) else {
print("Error creating SecKey from data: \(error!.takeRetainedValue() as Error)")
return nil
}
return key
}
guard let publicKey = publicKey(from: publicKeyPEM),
let data = password.data(using: .utf8) else {
return nil
}
let algorithm: SecKeyAlgorithm = .rsaEncryptionPKCS1
guard SecKeyIsAlgorithmSupported(publicKey, .encrypt, algorithm),
let cipherText = SecKeyCreateEncryptedData(publicKey, algorithm, data as CFData, nil) else {
return nil
}
let pp = (cipherText as Data).base64EncodedString()
return pp
}
```
This is working find on MacOS but in linux I need I cant find doc how I can achieve it using CryptoSwift
any help appreciated
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Beta Was this translation helpful? Give feedback.
All reactions