我有一个3DES加密的例子,我必须遵循它才能在NFC卡上进行身份验证。以下是示例:
因此51E764602678DF2B变为577293FD2F34CA51,密钥= 49454D4B41455242214E4143554F5946且IV = 0000000000000000 我成功地在这个网站上得到了正确的结果: http://tripledes.online-domain-tools.com/
我尝试使用https://github.com/sgl0v/SCrypto进行快速扫描,如下所示:
func testScrypto() {
let plaintext = "51E764602678DF2B".data(using: String.Encoding.utf8)!
let sharedSecretKey = "49454D4B41455242214E4143".data(using: String.Encoding.utf8)!
let IV = "0000000000000000".data(using: String.Encoding.utf8)!
let ciphertext = try! plaintext.encrypt(.tripleDES, options: .PKCS7Padding, key: sharedSecretKey, iv: IV)
let plaintext2 = try! ciphertext.decrypt(.tripleDES, options: .PKCS7Padding, key: sharedSecretKey, iv: IV)
print("cipher = \(ciphertext.hexString())")
print("plaintext2 = \(plaintext2.hexString())")
}
public extension Data {
func bytesArray<T: ExpressibleByIntegerLiteral>() -> [T] {
var bytes = Array<T>(repeating: 0, count: self.count)
(self as NSData).getBytes(&bytes, length:self.count * MemoryLayout<T>.size)
return bytes
}
func hexString() -> String {
let hexString = NSMutableString()
let bytes: [UInt8] = self.bytesArray()
for byte in bytes {
hexString.appendFormat("%02x", UInt(byte))
}
return hexString as String
}
}
结果是:
cipher = d4c4a9637bcb4a435982330a42d1357b9e4539886a983535
plaintext2 = 35314537363436303236373844463242
35314537363436303236373844463242是51E764602678DF2B,如果我从hexstring转换为明文但其他字符串根本不是577293FD2F34CA51
我也试过这个lib https://www.example-code.com/swift/crypt2_3des.asp 但结果仍然是错误的
我不知道是否有人知道如何在swift上执行此操作或者是加密专家?
谢谢!
答案 0 :(得分:0)
已提供的明文数据和密钥以十六进制编码。然而,您将它们编码为UTF-8文本,这不是一回事。
答案 1 :(得分:0)
我成功解决了这个问题 问题是密钥只有16个字节,需要24个,所以我猜它是随机填充的,但是为了做24个,它预计是在16个末尾放回的前8个字节? / p> 像这样工作:
func fillKey(keyLength: size_t, key: Data) -> Data {
let missingBytes = keyLength - key.count
if missingBytes > 0 {
let keyBytes = (key as NSData).bytes
var bytes = [UInt8](repeating: UInt8(0), count: keyLength)
memccpy(&bytes[0], keyBytes.advanced(by: 0), Int32(key.count), key.count)
memccpy(&bytes[key.count], keyBytes.advanced(by: 0), Int32(missingBytes), missingBytes)
return Data(bytes: bytes)
} else {
return key
}
}
func my3DESEncrypt(encryptData: String, key: String, iv: String) -> Data? {
var myKeyData : Data = key.hexadecimal()!
let myIvData : Data = iv.hexadecimal()!
var myRawData : Data = encryptData.hexadecimal()!
let buffer_size : size_t = myRawData.count + kCCBlockSize3DES
var buffer = [UInt8](repeating: UInt8(0), count: buffer_size)
var num_bytes_encrypted : size_t = 0
let operation: CCOperation = UInt32(kCCEncrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithm3DES)
let options: CCOptions = 0
let keyLength = size_t(kCCKeySize3DES)
myKeyData = self.fillKey(keyLength: keyLength, key: myKeyData)
let Crypto_status: CCCryptorStatus = CCCrypt(operation, algoritm, options, (myKeyData as NSData).bytes, keyLength, (myIvData as NSData).bytes, (myRawData as NSData).bytes, myRawData.count, &buffer, buffer_size, &num_bytes_encrypted)
if UInt32(Crypto_status) == UInt32(kCCSuccess) {
let data = Data(bytes: buffer, count: num_bytes_encrypted)
return data
} else{
return nil
}
}
func my3DESDecrypt(decryptData : Data, key: String, iv: String) -> Data? {
let mydata_len : Int = decryptData.count
var myKeyData : Data = key.hexadecimal()!
let myIvData : Data = iv.hexadecimal()!
let buffer_size : size_t = mydata_len+kCCBlockSize3DES
var buffer = [UInt8](repeating: UInt8(0), count: buffer_size)
var num_bytes_encrypted : size_t = 0
let operation: CCOperation = UInt32(kCCDecrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithm3DES)
let options: CCOptions = UInt32(kCCOptionPKCS7Padding)
let keyLength = size_t(kCCKeySize3DES)
myKeyData = self.fillKey(keyLength: keyLength, key: myKeyData)
let decrypt_status : CCCryptorStatus = CCCrypt(operation, algoritm, options, (myKeyData as NSData).bytes, keyLength, (myIvData as NSData).bytes, (decryptData as NSData).bytes, mydata_len, &buffer, buffer_size, &num_bytes_encrypted)
if UInt32(decrypt_status) == UInt32(kCCSuccess){
let data = Data(bytes: buffer, count: num_bytes_encrypted)
return data
} else{
return nil
}
}