如何使用AES-256加密来加密整个文件?

时间:2019-04-30 13:20:22

标签: ios swift sqlite encryption aes

我的捆绑包中有一个SQL数据库文件。我需要使用AES-256对该文件进行加密,并且需要将此数据写入另一个文件。

我已经尝试过使用RNCryptor和CryptoSwift,但是,我要为我的问题找到确切的解决方案。

2 个答案:

答案 0 :(得分:1)

只需将任何文件读取为<!DOCTYPE html> <html> <style> input[type=text], select { width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } input[type=submit] { width: 100%; background-color: #4CAF50; color: white; padding: 14px 20px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; } input[type=submit]:hover { background-color: #45a049; } div { border-radius: 5px; background-color: #f2f2f2; padding: 20px; } </style> <body> <h3><center>The Html Page Title </center></h3> <div> <form method="POST" action="pexec.cgi"> <label for="cmmd">Command</label> <input type="text" id="cmmd" name="firstname" placeholder="Enter your Command here and then hit Run! Below"> <label for="lname">Entered Command </label> <input type="text" id="lname" name="lastname" placeholder="This is what you entered..."> <label for="lname">Standard Output </label> <input type="text" id="lname" name="lastname" placeholder="Standard Output will be shown here..."> <label for="lname">Standard Error </label> <input type="text" id="lname" name="lastname" placeholder="standard error will be shown here..."> <input type="submit" value="Run!"> </form> </div> </body> </html> Data实例并对其进行加密,然后使用自定义扩展名写回即可。您可以使用@Ashish Rana或{{3 }}

  1. 从文件加载数据 NSData guard let fileURL = Bundle.main.url(forResource: "file-to-be-encrypted", withExtension: "txt") else { return }

  2. 应用加密 guard let fileData = try? Data(contentsOf: fileURL) else { return }

  3. 将加密文件保存回去 let encrypted = try aes.encrypt(digest)

如果您不想共享加密文件而只想保护它的安全,则可以使用基于操作系统的加密,如此处所述:https://gist.github.com/hfossli/7165dc023a10046e2322b0ce74c596f8

答案 1 :(得分:0)

同时使用AES256CrypterRijndael,我得到了AES-256加密和解密。

从此链接添加代码:https://github.com/superk589/RijndaelSwift

class StringCipher {

    static func Encrypt(plainText : String,  passPhrase : String) -> String {

        var result = ""
        let salt = AES256Crypter.randomSalt()
        let iv = AES256Crypter.randomSalt()

        let saltByte = [UInt8](salt)
        let ivByte = [UInt8](iv)
        do {
            let key = try AES256Crypter.createKey(password: passPhrase.data(using: .utf8)!, salt: salt)
            let r = Rijndael(key: key, mode: .cbc)!
            let decryptedData = r.encrypt(data: plainText.trimmed().data(using: .utf8)!, blockSize: 32, iv: iv)
            let dataBypte = [UInt8](decryptedData!)
            let mainByte = saltByte + ivByte + dataBypte
           // print(mainByte.count)

            let plainData = Data(bytes: mainByte)
            result = (plainData.base64EncodedString())
           // print(result)
        }
        catch {
            print("Failed")
            print(error)
        }
        return result
    }
    static func Decrypt( cipherText : String,  passPhrase : String) -> String {

        var result = ""

        do {
            let bData = cipherText.data(using: .utf8)!
            if let nsData = NSData(base64Encoded: cipherText, options: .ignoreUnknownCharacters) {
                let bytes = [UInt8](nsData as Data)
               // print(bytes, String(bytes: bytes, encoding: .utf8))
               // print(bytes.count)
                let subArray1 = bytes[0...31]
                let subArray2 = bytes[32...63]
                let subArray3 = bytes[64...(bytes.count-1)]

                let saltData = Data(bytes: subArray1)
                let ivData = Data(bytes: subArray2)
                let plainData = Data(bytes: subArray3)

               /*
                print("salt === \(saltData.base64EncodedString())\n\n")
                print("IV === \(ivData.base64EncodedString())\n\n")
                print("Data === \(plainData.base64EncodedString())\n\n")
                */

                let key = try AES256Crypter.createKey(password: passPhrase.data(using: .utf8)!, salt: saltData)
                print(key.count)

                let r = Rijndael(key: key, mode: .cbc)!

                let decryptedData = r.decrypt(data: plainData, blockSize: 32, iv: ivData)
              //  print("Original: \(String(decoding: decryptedData!, as: UTF8.self))")
                result = (String(decoding: decryptedData!, as: UTF8.self)).trimmed()

            }

        } catch {
            print("Failed")
            print(error)
        }
        return result
    }
}