当前位置:网站首页>Experimental report on information management and information system [information security and confidentiality] of Huazhong Agricultural University

Experimental report on information management and information system [information security and confidentiality] of Huazhong Agricultural University

2022-06-11 05:47:00 swindler.

Information security and confidentiality test report

1. The experiment purpose

This experiment uses Xcode As a development tool ,swift As the main development language , Simply write the application page , It presents the encryption and decryption in the course of information security and decryption , Combine theory with practice to consolidate and strengthen the absorption and application of knowledge . The algorithm implementation part mainly uses AES Algorithm for encryption and decryption , Use MD5 Abstract algorithm for encryption .

2. Experimental process

2.1 Build a page

The main page elements are as follows :

class ViewController: UIViewController,UITextFieldDelegate {
    
    
    var flag = false
    var label1:UILabel!
    var label2:UILabel!
    var output1:String! = ""
    var output2:String! = ""
    var result:UILabel!
    override func viewDidLoad() {
    
        super.viewDidLoad()
        let screen = UIScreen.main.bounds
        self.view.backgroundColor = .systemYellow
        // Add tags 
       let labelHeight:CGFloat = 100
       let labelWidth:CGFloat = 100
       let labelTopView:CGFloat = 100
      self.label1 = UILabel(frame: CGRect(x:(screen.size.width - labelWidth)/2 , y: labelTopView, width: labelWidth, height: labelHeight))
       self.label1.text = " Plaintext :"
       self.label1.font = .boldSystemFont(ofSize: 22)
        self.label1.textColor = .systemBlue
       self.label1.textAlignment = .center
       self.view.addSubview(self.label1)
        
        // add to textField
        let textFieldHeight:CGFloat = 50
        let textFieldWidth:CGFloat = 300
        let textFieldTopView:CGFloat = 200
        let textField = UITextField(frame: CGRect(x: (screen.size.width - textFieldWidth)/2, y: textFieldTopView, width: textFieldWidth, height: textFieldHeight))
        textField.placeholder = " Please enter relevant information "
        textField.borderStyle = .roundedRect
        textField.clearButtonMode = .whileEditing
        textField.becomeFirstResponder()
        textField.returnKeyType = .done
        textField.textColor = .systemBlue
        textField.allowsEditingTextAttributes = .random()
        textField.delegate = self
        self.view.addSubview(textField)
        
        // Add a button 
        let button = UIButton(type: .custom)
        let buttonHeight:CGFloat = 60
        let buttonWidth:CGFloat = 100
        let buttonTopView:CGFloat = 650
        let frame = CGRect(x: (screen.size.width - buttonWidth)/2, y: buttonTopView, width: buttonWidth, height: buttonHeight)
        button.frame = frame
        button.backgroundColor = .red
        button.layer.cornerRadius = 6
        button.layer.borderColor = UIColor.red.cgColor
        button.layer.borderWidth = 1
        button.setTitle(" encryption ", for: UIControl.State())
        button.titleLabel?.font = .boldSystemFont(ofSize: 17)
        button.addTarget(self, action: #selector(modeChanged(_:)), for: .touchUpInside)
        self.view.addSubview(button)
        
        // Add a result callout :
        self.label2 = UILabel(frame: CGRect(x: (screen.size.width - labelWidth)/2, y: 270, width: labelWidth, height: labelHeight))
        self.label2.text = " Ciphertext :"
        self.label2.font = .boldSystemFont(ofSize: 22)
        self.label2.textAlignment = .center
        self.label2.textColor = .systemBlue
        self.view.addSubview(self.label2)
        
        // Add result labels 
        let resultHeight:CGFloat = 100
        let resultWidth:CGFloat = 300
        let resultTopView:CGFloat = 370
        self.result = UILabel(frame: CGRect(x: (screen.size.width - resultWidth)/2, y: resultTopView, width: resultWidth, height: resultHeight))
        self.result.text = ""
        self.result.font = .boldSystemFont(ofSize: 22)
        self.result.textColor = .systemBlue
        self.result.textAlignment = .center
        self.result.lineBreakMode = NSLineBreakMode.byWordWrapping
        self.result.numberOfLines = 0
        self.result.layer.cornerRadius = 5
        self.result.layer.masksToBounds = true
        self.result.layer.borderWidth = 1
        self.result.layer.borderColor = .init(red: 1, green: 1, blue: 1, alpha: 1)
        self.result.layer.backgroundColor = .init(red: 1, green: 1, blue: 1, alpha: 1)
        self.view.addSubview(result)
        
        // Add toggle button 
        let toggleHeight:CGFloat = 40
        let togggleWidth:CGFloat = 50
        let toggleTopView:CGFloat = 550
        let toggleButton = UIButton(frame: CGRect(x: (screen.size.width - togggleWidth)/2, y: toggleTopView, width: togggleWidth, height: toggleHeight))
        toggleButton.backgroundColor = .gray
        toggleButton.layer.borderColor = UIColor.gray.cgColor
        toggleButton.layer.borderWidth = 1
        toggleButton.layer.cornerRadius = 6
        toggleButton.setTitle(" Switch ", for: UIControl.State())
        toggleButton.titleLabel?.font = .boldSystemFont(ofSize: 13)
        toggleButton.addTarget(self, action: #selector(toggle(_:)), for: .touchUpInside)
        self.view.addSubview(toggleButton)

2.2 Algorithm implementation

2.2.1 MD5 Implementation of algorithm encryption

1. stay Teminal The terminal uses the command line to create a Podfile

$ touch Podfile
$ open -e Podfile

2. In the just popped Podfile Add the following to the file

pod ‘CryptoSwift’

3. After downloading the third-party library successfully , Can be in Viewcontroller.swift The first line of the file introduces

import CryptoSwift

because Swift Third party library CryptoSwift yes Swift A collection of standards and secure encryption algorithms implemented in , therefore md5 The implementation of the algorithm can directly call the md5() The method can

The function code of the algorithm embedded page is as follows :

  @objc func modeChanged(_ sender:AnyObject){
           
            self.result.text = output1           
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    
            textField.resignFirstResponder()
        output1 = textField.text?.md5()
        //output2 = textField.text.
            return true   
}

2.2.2 AES Implementation of algorithm encryption and decryption

encryption
func Encode_AES_ECB(strToEncode:String)->String {
    
        var encodeString = ""
        do {
    
            let aes = try AES(key: Padding.zeroPadding.add(to: key.bytes, blockSize: AES.blockSize), blockMode: ECB(),padding: .pkcs7)
            let encoded = try aes.encrypt(strToEncode.bytes)
            encodeString = encoded.toBase64()
            print(encodeString)    
        }catch{
    
            print(error.localizedDescription)
        }
        return encodeString
    }
Decrypt
func Decode_AES_ECB(strToDecode:String)->String {
    
        var decodeString = ""
        let data = NSData(base64Encoded: strToDecode, options: NSData.Base64DecodingOptions.init(rawValue: 0))
        var encrypted:[UInt8] = []
        let count = data?.length
        for i in 0..<count!{
    
            var temp:UInt8 = 0
            data?.getBytes(&temp, range: NSRange(location: i, length: 1))
            encrypted.append(temp)
        }
        do {
    
            let aes = try AES(key: Padding.zeroPadding.add(to: key.bytes, blockSize: AES.blockSize), blockMode: ECB(), padding: .pkcs7)
            let decode = try aes.decrypt(encrypted)
            let encoded = Data(decode)
            decodeString = String(bytes: encoded.bytes, encoding: .utf8)!
        }catch{
    
            print(error.localizedDescription)
        }
    return decodeString
    }

The function code of the algorithm embedded function page is as follows :

  @objc func modeChanged(_ sender:AnyObject){
    
        if self.label1.text == " Plaintext :" {
    
            result.text = Encode_AES_ECB(strToEncode: output1)
        }else if self.label1.text == " Ciphertext :"  {
    
            result.text = Decode_AES_ECB(strToDecode: output1)
        }
      
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    
            textField.resignFirstResponder()
            output1 = textField.text
            return true
    
}
    @objc func toggle(_ sender:AnyObject){
    
        if flag {
    
            self.label1.text = " Ciphertext :"
            self.label2.text = " Plaintext :"
            self.button.setTitle(" Decrypt ", for: UIControl.State())
            self.result.text = ""
            flag = false
            
        }else{
    
            self.label1.text = " Plaintext :"
            self.label2.text = " Ciphertext :"
            self.button.setTitle(" encryption ", for: UIControl.State())
            self.result.text = ""
            flag = true
        }
        
    }

2.3 Results display

  • The page display :
     Insert picture description here
  • md5 Abstract algorithm :
     Insert picture description here
  • AES Algorithm :
    encryption :
     Insert picture description here
    Switch :
     Insert picture description here
    Decrypt :
     Insert picture description here

Experimental experience

Through this experiment , My harvest is as follows :

  • take “ Information security and confidentiality ” This course combines theory and practice more closely . It's on paper , Only listening to the teacher talk about abstract theoretical knowledge in the classroom makes me not fully understand this course , The experiment makes the teacher's pages PPT Become lines of code that can be implemented and manipulated , Enhanced sense of achievement in learning , When doing experiments , In my mind, I recall the relevant knowledge that the teacher talked about in class , The mastery and understanding of corresponding courses have been deepened !
  • Enhanced the improvement of personal skill level . Self taught mobile terminal for nearly half a year iOS Development , But never put the new knowledge outside the classroom into the classroom of their own experience . This experiment gave me a good chance , Not just the implementation of the algorithm , The construction of web page , The composition of the elements , The processing logic between various elements and events also makes me ponder , Racking my brains . In the end, Kung Fu is not inferior to those who want it , The whole application runs smoothly !
  • Great improvement of learning ability . Due to the use of iOS Medium swift Language experiment , So most of the information on the Internet can not be used directly , thus , Force yourself to study carefully CryptoSwift The source code of the library and the old version on the network Xcode Version encryption algorithm is summarized , Finally, it is suitable for Xcode11, as well as iOS10 The encryption and decryption algorithm and its function realization on the above simulator .
  • Last , Thank Mr. Zhang and the students for their encouragement and patient help !
原网站

版权声明
本文为[swindler.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020534562922.html