美文网首页iOS安全
Swift实现AES256加解密

Swift实现AES256加解密

作者: 小鹏学长168 | 来源:发表于2022-08-22 14:07 被阅读0次

    计算摘要

    let data = Data([0x01, 0x02, 0x03])
    
    let hash = data.md5()
    let hash = data.sha1()
    let hash = data.sha224()
    let hash = data.sha256()
    let hash = data.sha384()
    let hash = data.sha512()
    

    AES256加解密

    let password: [UInt8] = Array("xiaopeng".utf8)
    let salt: [UInt8] = Array("wan".utf8)
    
    /* Generate a key from a `password`. Optional if you already have a key */
    let key = try PKCS5.PBKDF2(
        password: password,
        salt: salt,
        iterations: 4096,
        keyLength: 32, /* AES-256 */
        variant: .sha128
    ).calculate()
    
    /* Generate random IV value. IV is public value. Either need to generate, or get it from elsewhere */
    let iv = AES.randomIV(AES.blockSize)
    
    /* AES cryptor instance */
    let aes = try AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7)
    
    /* Encrypt Data */
    let inputData =  Data([0x01, 0x02, 0x03])
    let encryptedBytes = try aes.encrypt(inputData.bytes)
    let encryptedData = Data(encryptedBytes)
    
    /* Decrypt Data */
    let decryptedBytes = try aes.decrypt(encryptedData.bytes)
    let decryptedData = Data(decryptedBytes)
    
    

    相关文章

      网友评论

        本文标题:Swift实现AES256加解密

        本文链接:https://www.haomeiwen.com/subject/kssigrtx.html