-
Notifications
You must be signed in to change notification settings - Fork 83
Closed
Description
I'm trying to use the DES encryption using ECB mode but it's not working. I validate it using the online encryption tool that works. I changed my keys and plain text to not display our own key and text and used a different pair. IDZSwiftCommonCrypto yields different results everytime I tap my Start button. Here's my code:
import UIKit
import IDZSwiftCommonCrypto
class ViewController: UIViewController {
@IBOutlet weak var keyTextField: UITextField! // key is thekey
@IBOutlet weak var plainTextField: UITextField! // plain text is username123
@IBOutlet weak var outputTextView: UITextView! // output should be b742acfaa07e3d05cf2dc9aaa0258fc2
@IBOutlet weak var encryptionSegmentedControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().statusBarStyle = .LightContent
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func startButtonTapped(sender: UIButton) {
if encryptionSegmentedControl.selectedSegmentIndex == 0 {
let hexKey = arrayFromHexString(convertToHexFromString(keyTextField.text!))
let encryptedBytesArray = Cryptor(operation:.Encrypt, algorithm:.DES, options:.ECBMode, key:hexKey, iv:[UInt8]()).update(arrayFromHexString(convertToHexFromString(plainTextField.text!)))?.final()
outputTextView.text = hexStringFromArray(encryptedBytesArray!)
} else {
let hexKey = arrayFromHexString(convertToHexFromString(keyTextField.text!))
let decryptedBytesArray = Cryptor(operation:.Decrypt, algorithm:.DES, options:.ECBMode, key:hexKey, iv:[UInt8]()).update(plainTextField.text!)?.final()
outputTextView.text = hexStringFromArray(decryptedBytesArray!)
}
}
// MARK: - Custom
@IBAction func dismissKeyboard() {
self.view.endEditing(true)
}
func convertToHexFromString(rawString: String) -> String! {
var hexString = "\(rawString.dataUsingEncoding(NSUTF8StringEncoding)!)"
hexString = hexString.stringByReplacingOccurrencesOfString(" ", withString: "").stringByTrimmingCharactersInSet(NSCharacterSet.symbolCharacterSet())
print("Formatted hex string: \(hexString)")
return hexString
}
func encryptUsingText(hexText: String, hexKey: [UInt8]) -> String! {
let cryptor = Cryptor(operation:.Encrypt, algorithm:.DES, options:.ECBMode, key:hexKey, iv:[UInt8]())
let cipherText = cryptor.update(hexText)?.final()
print("Bytes array: \(cipherText!)")
return String(bytes: cipherText!, encoding: NSUTF8StringEncoding)
}
func decryptUsingText(rawText: String, hexKey: [UInt8]) -> String! {
let cryptor = Cryptor(operation:.Decrypt, algorithm:.DES, options:.None, key:hexKey, iv:[UInt8]())
let decryptedCipherText = cryptor.update(rawText)?.final()
print("Bytes array: \(decryptedCipherText!)")
return String(bytes: decryptedCipherText!, encoding: NSUTF8StringEncoding)
}
}
Metadata
Metadata
Assignees
Labels
No labels