iT邦幫忙

2024 iThome 鐵人賽

DAY 19
0
Mobile Development

手機Swift性能專家系列 第 19

Mac 同態加密用法

  • 分享至 

  • xImage
  •  

他的型別只能是UInt64與UInt32所以運算是負數的話會是錯的,最小的值只能是0

 do {
           
            let encryptParams =
                try EncryptionParameters<Bfv<UInt64>>(from: .insecure_n_8_logq_5x18_logt_5)

            
            let context = try Context(encryptionParameters: encryptParams)

            
            let values1: [UInt64] = [8, 5, 12, 12, 15, 5, 6, 7]
            let values2: [UInt64] = [1, 2, 3, 4, 5, 6, 7, 8]

            let plaintext1: Bfv<UInt64>.CoeffPlaintext = try context.encode(values: values1, format: .coefficient)
            let plaintext2: Bfv<UInt64>.CoeffPlaintext = try context.encode(values: values2, format: .coefficient)

            let secretKey = try context.generateSecretKey()
            let ciphertext1 = try plaintext1.encrypt(using: secretKey)
            let ciphertext2 = try plaintext2.encrypt(using: secretKey)

            
            let encryptedSum = try ciphertext1.convertToEvalFormat() - ciphertext2.convertToEvalFormat()
            print("encryptedSum" , encryptedSum)
           


            print(try encryptedSum.decrypt(using: secretKey))
            
        } catch {
            return
        }

注意他只能在執行在 mac os 上在iOS上是不行的


上一篇
swift 同態加密使用
下一篇
基本介紹SwiftUI
系列文
手機Swift性能專家20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言