0

我正在使用 Swift 2 为 iOS 创建一个 EPUB 3 阅读器。

我目前面临的问题是字体混淆/字体修饰。我已经阅读了一个关于如何在 Swift 中做到这一点的教程,并将其集成到我的项目中并进行了一些修改。

当我将混淆的 epub 加载到我的应用程序中时,字体未正确加载并回退到其他系统字体。当我加载具有相同字体但没有混淆的 epub 时,一切看起来都很好。显然,这意味着我的混淆代码有问题,但我一辈子都找不到错误。

这是我的代码:

public struct Crypto {

  public func obfuscateFontIDPF(data:NSData, key:String) -> NSData {
    let source = data
    var destination = [UInt8]()
    let shaKey = key.sha1()
    let keyData = shaKey.utf8Array

    var arr = [UInt8](count: source.length, repeatedValue: 0)
    source.getBytes(&arr, length:source.length)

    var outer = 0
    while outer < 52 && arr.isEmpty == false {
        var inner = 0
        while inner < 20 && arr.isEmpty == false {
            let byte = arr.removeAtIndex(0)      //Assumes read advances file position
            let sourceByte = byte
            let keyByte = keyData[inner]
            let obfuscatedByte = sourceByte ^ keyByte
            destination.append(obfuscatedByte)
            inner++
        }
        outer++
    }
    if arr.isEmpty == false {
        while arr.isEmpty == false {
            let byte = arr.removeAtIndex(0)
            destination.append(byte)
        }
    }

    let newData = NSData(bytes: &destination, length:     destination.count*sizeof(UInt8))
    return newData
  }
}

extension String {
  func sha1() -> String {
    var selfAsSha1 = ""

    if let data = self.dataUsingEncoding(NSUTF8StringEncoding)
    {
        var digest = [UInt8](count: Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0)
        CC_SHA1(data.bytes, CC_LONG(data.length), &digest)

        for index in 0..<CC_SHA1_DIGEST_LENGTH
        {
            selfAsSha1 += String(format: "%02x", digest[Int(index)])
        }
    }

    return selfAsSha1
  }

  var utf8Array: [UInt8] {
    return Array(utf8)
  }
}

在这里我称之为混淆方法:

    func parserDidEndDocument(parser: NSXMLParser) {
      if encryptedFilePaths!.count != 0 {
        for file in encryptedFilePaths! {
            let epubMainDirectoryPath = NSString(string: epubBook!.epubMainFolderPath!).stringByDeletingLastPathComponent
            let fullFilePath = epubMainDirectoryPath.stringByAppendingString("/" + file)
            let url = NSURL(fileURLWithPath: fullFilePath)
            if let source = NSData(contentsOfURL: url) {
                let decryptedFont = Crypto().obfuscateFontIDPF(source, key: self.epubBook!.encryptionKey!)
                do {
                    try decryptedFont.writeToFile(fullFilePath, options: .DataWritingAtomic)
                } catch {
                    print(error)
                }
            }
        }
    }
}

如果您发现错误可能出在哪里,请告诉我。

4

1 回答 1

0

我想通了,这是工作代码:

private func obfuscateData(data: NSData, key: String) -> NSData {
    var destinationBytes = [UInt8]()

    // Key needs to be SHA1 hash with length of exactly 20 chars
    let hashedKeyBytes = generateHashedBytesFromString(key)

    var sourceBytes = [UInt8](count: data.length, repeatedValue: 0)
    data.getBytes(&sourceBytes, length: data.length)

    var outerCount = 0
    while outerCount < 52 && sourceBytes.isEmpty == false {
        var innerCount = 0
        while innerCount < 20 && sourceBytes.isEmpty == false {
            let sourceByte = sourceBytes.removeAtIndex(0)
            let keyByte = hashedKeyBytes[innerCount]
            let obfuscatedByte = (sourceByte ^ keyByte)
            destinationBytes.append(obfuscatedByte)
            innerCount += 1
        }
        outerCount += 1
    }

    destinationBytes.appendContentsOf(sourceBytes)
    let destinationData = NSData(bytes: &destinationBytes, length: destinationBytes.count*sizeof(UInt8))
    sourceBytes.removeAll(keepCapacity: false)
    destinationBytes.removeAll(keepCapacity: false)
    return destinationData
}

/// Convert the key string to a SHA1 hashed Byte Array
private func generateHashedBytesFromString(string: String) -> [UInt8] {
    var resultBytes = [UInt8]()
    var hashedString = string.sha1()

    for _ in 0.stride(to: hashedString.characters.count, by: 2) {
        let character = "0x\(hashedString.returnTwoCharacters())"
        resultBytes.append(UInt8(strtod(character, nil)))
    }
    return resultBytes
}


extension String {
  func sha1() -> String {
    var selfAsSha1 = ""

    if let data = self.dataUsingEncoding(NSUTF8StringEncoding) {
        var digest = [UInt8](count: Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0)
        CC_SHA1(data.bytes, CC_LONG(data.length), &digest)

        for index in 0..<CC_SHA1_DIGEST_LENGTH {
          selfAsSha1 += String(format: "%02x", digest[Int(index)])
        }
    }

    return selfAsSha1
  }

  mutating func returnTwoCharacters() -> String {
      var characters: String = ""
      characters.append(self.removeAtIndex(startIndex))
      characters.append(self.removeAtIndex(startIndex))
      return characters
  }
}
于 2016-06-06T12:02:08.010 回答