This cryptography includes a part of my study, so if you make a mistake and this will improve it, please leave a comment: bow:
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.
Make a project appropriately Library installation
python
$ pod init
Podfile
pod 'CryptoSwift', '~> 0.6'
python
$ pod install
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.
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 ...
It is recommended because you can write the encryption and decryption process very easily.
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 ::
If you like this, I'd appreciate it if you could comment or request editing!
Recommended Posts