Decrypt a string encrypted on iOS with Python

Introduction

This cryptography includes a part of my study, so if you make a mistake and this will improve it, please leave a comment: bow:

environment

encryption

This time we will use encryption using AES. No matter how much HTTPS communication is used, unless MITM measures are specified, sending and receiving of passwords etc. can be seen in plain text. Let's at least encrypt it.

Implementation is on the iOS side.

Library

Make a project appropriately Library installation

python


$ pod init

Podfile


pod 'CryptoSwift', '~> 0.6'

python


$ pod install

Sample source

Hoge.swift


import Foundation
import CryptoSwift  //At the time of writing here, "⌘+B "should be done

class Hoge: NSObject {
    func hoge(_ text: String) {
        let bytes = [UInt8](text.utf8)
        //Change each person according to the AES key method
        let key = [UInt8]("12345678901234567890123456789012".utf8)
        let iv: [UInt8] = AES.randomIV(AES.blockSize)

        do {
            let aes = try AES(key: key, iv: iv, blockMode: .CBC)
            let encrypted = try aes.encrypt(bytes)
            let encryptedData = Data(bytes: encrypted, count: encrypted.count)
            let sendData = NSMutableData(bytes: iv, length: iv.count)
            sendData.append(encryptedData)
            let sendDataBase64 = sendData.base64EncodedString(options: .lineLength64Characters)
            print("Encrypt: \(sendDataBase64)")
            // Encrypt: rLKCA1hNmqu2dq+08E9mK2lBlspQN0+CYBkWkCQz7IvHhh+qbfysc26Oh1SS4Adq
        } catch let error {
            print("Error: \(error)")
        }
    }
}

I think that encryption was possible for the time being. The character string used this time is "Lorem ipsum dolor sit amet". After that, Alamofire will fly to the server. I will omit it.

Decryption

Basically, I use Django when making a server side that seems to require encryption, but I do not touch the Django-specific part, so it is not bad ...

Library

It is recommended because you can write the encryption and decryption process very easily.

Sample source

decrypt.py


from simple_aes_cipher import AESCipher


encrypt = 'rLKCA1hNmqu2dq+08E9mK2lBlspQN0+CYBkWkCQz7IvHhh+qbfysc26Oh1SS4Adq'
cipher = AESCipher('12345678901234567890123456789012')
plain_text = cipher.decrypt(encrypt)

print(plain_text)  # Lorem ipsum dolor sit amet

It seems that it was successfully decrypted: clap :: clap :: clap ::

Finally

If you like this, I'd appreciate it if you could comment or request editing!

Recommended Posts

Decrypt a string encrypted on iOS with Python
Decrypt files encrypted with OpenSSL with Python 3
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Decrypt files encrypted with openssl from python with openssl
Folium: Visualize data on a map with Python
Visualize grib2 on a map with python (matplotlib)
Make one repeating string with a Python regular expression.
How to convert / restore a string with [] in python
Make a breakpoint on the c layer with python
I made a Python3 environment on Ubuntu with direnv.
A note on speeding up Python code with Numba
[Python] Use a string sequence
Create a directory with python
String format with Python% operator
Quickly take a query string with API Gateway-> Lambda (Python)
[python] Reverse with slices! !! (There is also a commentary on slices!)
Create a message corresponding to localization with python translation string
Get the matched string with a regular expression and reuse it when replacing on Python3
Building a Python environment on Mac
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Building a Python environment on Ubuntu
Solve ABC166 A ~ D with Python
Create a virtual environment with Python!
I made a fortune with Python.
String replacement with Python regular expression
Make a recommender system with python
Create a python environment on centos
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Notes on using rstrip with python.
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
Getting started with Python 3.8 on Windows
Build a python3 environment on CentOS7
Create a random string in Python
[Python] Inherit a class with class variables
I made a daemon with Python
Write a batch script with Python3.5 ~
[Memo] Tweet on twitter with python
Convert to a string while outputting standard output with Python subprocess
Control the motor with a motor driver using python on Raspberry Pi 3!
A note on what you did to use Flycheck with Python
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
[Introduction to Python] How to split a character string with the split function
Build a Python environment on your Mac with Anaconda and PyCharm
Try to extract a character string from an image with Python3
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Create a Python function decorator with Class
Creating a simple PowerPoint file with Python
[Python] A program that creates stairs with #
Building a Python3 environment with Amazon Linux2
Let's make a shiritori game with Python
Run servo with Python on ESP32 (Windows)
Install Python as a Framework with pyenv
Build a blockchain with Python ① Create a class