SwiftyRSA icon indicating copy to clipboard operation
SwiftyRSA copied to clipboard

Using SwiftyRSA to encrypt password and decrypt in python server

Open IntelliMind opened this issue 4 years ago • 0 comments

I have simple requirement to

  • encrypt password in native mobile application developed using Swift / SwiftyRSA
  • decrypt password on server side using python - https://pycryptodome.readthedocs.io/en/latest/src/examples.html
  1. SwiftyRSA can encrypt the password and generate a base64 string
private func encryptWithRSA() throws -> String {
        let realm = IMDRealmProvider.appPreferencesRealm()
        guard let rsaPublicKey = realm.objects(IMDRSAPublicKey.self).first
            else {
                throw IMDRSAPublicKey.KeyError.missing
        }
        guard !rsaPublicKey.isExpired else {
            throw IMDRSAPublicKey.KeyError.expired
        }
        
        let publicKey = try PublicKey(pemEncoded: rsaPublicKey.key)
        return  try ClearMessage(string: self, using: .utf8)
                .encrypted(with: publicKey, padding: .init(rawValue: 0))
                .base64String
  • I tried to play with padding and changed to PKCS1 as well but did not help.
  1. Python when I try using
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto import Random
import ast

def decrypt(private_key, encrypted_message):
    ''' decrypt message '''

    decryptor = PKCS1_OAEP.new(private_key)
    decrypted_message = decryptor.decrypt(ast.literal_eval(str(encrypted_message)))

    return decrypted_message

In Python, I get message "Error in decrypting message ('Incorrect decryption.',)"

The above code used to work fine in Swift 3.2, however after upgrading to Swift 5 it doesn't work anymore. For SwiftyRSA I am using latest version.

IntelliMind avatar Aug 19 '20 23:08 IntelliMind