2017-12-13 23 views
0

Swift文字列を[UInt8]バイト配列として取得し、Cコードから同じバイト配列を返して、オリジナルスウィフトストリング。私はユニコードを維持しようとしています(変換/操作に損失がないので)。私は、エラーが「復号化機能で変換ラインに期待される引数の型 『UINT』にタイプ 『の値String.Encoding』を変換できません取得しています。任意の助けを事前に感謝を!Swift文字列をCバイト配列に変換してSwift文字列に戻す

// function to encrypt a string with custom "C" code and encode the result into a hex string 
func EncryptString(password: String, stringToEncrypt: String) ->String { 

    var hexStr = "" 

    // convert the String password into utf8 bytes 
    let pw = password.data(using: String.Encoding.utf8, allowLossyConversion:false) 
    var passwordBytes : [UInt8] = Array(pw!) 

    // convert the string to encrypt into utf8 bytes 
    let bytes = stringToEncrypt.data(using: String.Encoding.utf8, allowLossyConversion:false) 
    var buf : [UInt8] = Array(bytes!) 

    // encrypt the string to encrypt with the Unicode password (both are Unicode in Swift) 
    encryptData(&passwordBytes, Int32(password.count), &buf, Int32(stringToEncrypt.count)) 

    // turn the now encrypted "stringToEncrypt" into two character hex values in a string 
    for byte in buf { 
     hexStr.append(String(format:"%2X", byte)) 
    } 

    // return the encrypted hex encoded string to the caller... 
    return hexStr 
} 


func DecryptString(password: String, hexStringToDecrypt: String) ->String { 

    var decryptedStr = "" 

    // convert the String password into utf8 bytes 
    let pw = password.data(using: String.Encoding.utf8, allowLossyConversion:false) 
    var passwordBytes : [UInt8] = Array(pw!) 

    // convert the string to encrypt into utf8 bytes 
    let bytes = hexStringToDecrypt.data(using: String.Encoding.utf8, allowLossyConversion:false) 
    var buf : [UInt8] = Array(bytes!) 

    // encrypt the string to encrypt with the Unicode password (both are Unicode in Swift) 

    let bytecount = password.count 
    decryptData(&passwordBytes, Int32(password.count), &buf, Int32(hexStringToDecrypt.count)) 
      // turn the now encrypted "hexStringToDecrypt" into int values here is where I get error: Cannot convert value of type 'String.Encoding' to expected argument type 'UInt' 
    var unicode_str = NSString(bytes: buf, length: bytecount, encoding: NSUTF32LittleEndianStringEncoding) 

    // return the encrypted hex encoded string to the caller... 
    return unicode_str 
} 

}

をスウィフト3/4で
+1

エラーを引き起こす正確な行を指摘してください。 'utf8'を使用しようとする行がいくつかあります。 Swift 3以降を使用していることを確認します。 – rmaddy

+0

ありがとうございます!私はSwift 4を使用していますが、現在のコード行は次のようになります:var unicode_str = NSString(buf、length:bytecount、encoding:NSUTF32LittleEndianStringEncoding) – user6096790

+0

「Cバイト配列」は正確には何ですか?バイトが言語関連を持たないと言うことができるので、単純に8ビット(最近)です。スウィフト "C"もありません。 – zaph

答えて

1

UInt8の代わりにDataタイプを使用することをお勧めし

DataStringを変換します。

let dat = string.data(using:.utf8) 

StringDataを変換します。ここでは

let str = String(data:dat, encoding:.utf8) 

は、例えば、暗号化はUInt8の代わりにDataを使用している、これは最低でエラー処理が欠落している、生産コードではありません。

func aesCBCEncrypt(data:Data, keyData:Data, ivData:Data) -> Data { 
     let cryptLength = size_t(kCCBlockSizeAES128 + data.count + kCCBlockSizeAES128) 
     var cryptData = Data(count:cryptLength) 
     var numBytesEncrypted :size_t = 0 

     let cryptStatus = cryptData.withUnsafeMutableBytes {cryptBytes in 
      data.withUnsafeBytes {dataBytes in 
       keyData.withUnsafeBytes {keyBytes in 
        ivData.withUnsafeBytes {ivBytes in 
         CCCrypt(CCOperation(kCCEncrypt), 
           CCAlgorithm(kCCAlgorithmAES), 
           CCOptions(kCCOptionPKCS7Padding), 
           keyBytes, keyData.count, 
           ivBytes, 
           dataBytes, data.count, 
           cryptBytes, cryptLength, 
           &numBytesEncrypted) 
        }}}} 

    cryptData.count = (cryptStatus == kCCSuccess) ? numBytesEncrypted : 0 

    return cryptData; 
} 
+0

ご協力いただきありがとうございます! :-) – user6096790

関連する問題