如何在swift中实现此步骤
- 以此格式 e077b34665f66eb5 获取加密的 TWK 。
- 使用DES作为解密方法,当TMK abcdef0123456789 时,使用存储的 TMK 解密TWK,以获得清除TWK。
- 使用ANSI X9.8(ISO格式0)格式准备PIN块,如下所示:
- 计算P1:[0] [pin_length] [pin]然后右边填充'F',例如PIN =“0000”,P1:“040000FFFFFFFFFF”
- 计算P2:从PAN获取帐户,取PAN的最后12位不包括Luhn代码,左边填充零,例如PAN =“1234567890123456”,P2:“0000456789012345”
- 清除PIN Block = p1 XOR p2,上面的P1和P2 = 0400459876fedcba
- 使用clear TWK密钥和DES作为加密方法加密PIN块。
ii尝试使用CommonCrypto
class Handeller{
var pinBlock:String = "";
func Handeller(tmk:String , workingKey:String ,pan:String ,pin:String){
var p1:String = ""
var p2:String = ""
p1.append("0")
p1.append(String(pin.count))
p1.append(pin)
var i:Int = 0
for i in 0...15-p1.count{
p1.append("F")
}
p2.append("0000")
var y:Int = 0
for i in 0...11{
let panChar = pan.characters.map { (Character) -> Character in
return Character
}
p2.append(panChar[pan.count-13+i])
}
print(p1)
print(p2)
// to get clear workingKey
print(myDecrypt(mykey: tmk, myString: workingKey))
let text = [UInt8](p1.utf8)
let cipher = [UInt8](p2.utf8)
var encrypted = [UInt8]()
print("text \(text)")
// encrypt bytes
//for t in enumerate(text) {
for (index, t) in text.enumerated(){
encrypted.append(t ^ cipher[index])
}
var str = String(bytes: encrypted, encoding: String.Encoding.utf8)
print(str)
for (index, t) in encrypted.enumerated(){
print(t)
}
print("\\\\\\\\\\\\\\\\\\\\\\\\\\")
for (index, t) in encrypted.enumerated(){
print(String(t, radix: 16))
}
String(bytes: encrypted, encoding: String.Encoding.utf8)
print(toByteArray(String(bytes: encrypted, encoding: String.Encoding.utf8)!))
}
func myDecrypt (mykey:String , myString:String) -> String{
//let tmkByte = String(mykey).utf8.map{ UInt8($0) }
print("key is \(mykey)")
print("data is \(myString)")
if
let key = mykey.data(using: .utf8),
let data = myString.data(using: .utf8)
//let data = Data(Hex: myString, options: .ignoreUnknownCharacters) //<-#1
{
print("key byte num is \(key)")
print("data byte num is \(data)")
var numBytesDecrypted: size_t = 0
var result = Data(count: data.count) //<-#2
print("result length \(result)")
let err = result.withUnsafeMutableBytes {resultBytes in
data.withUnsafeBytes {dataBytes in
key.withUnsafeBytes {keyBytes in
CCCrypt(CCOperation(kCCDecrypt), CCAlgorithm(kCCAlgorithmDES), CCOptions(kCCOptionPKCS7Padding|kCCModeECB), keyBytes, kCCKeySizeDES, nil, dataBytes, data.count, resultBytes, result.count, &numBytesDecrypted) //<-#4
}
}
}
if err != CCCryptorStatus(kCCSuccess) {
NSLog("Decryption failed! Error: \(err.description)")
}
print(numBytesDecrypted)
result.count = numBytesDecrypted //<-#3
print(result as NSData) //`as NSData` is good for debugging.
//let NSDATA = result as NSData
//init?(data: Data, encoding: String.Encoding)
//print ("string is \(result.hexEncodedString())")
return String(data: result , encoding: .utf8) ?? "???1"
//return result.hexEncodedString()
}
return "???2"
}
func toByteArray( _ hex:String ) -> [UInt8] {
// remove "-" from Hexadecimal
var hexString = hex.removeWord( "-" )
let size = hexString.characters.count / 2
var result:[UInt8] = [UInt8]( repeating: 0, count: size ) // array with length = size
// for ( int i = 0; i < hexString.length; i += 2 )
for i in stride( from: 0, to: hexString.characters.count, by: 2 ) {
let subHexStr = hexString.subString( i, length: 2 )
result[ i / 2 ] = UInt8( subHexStr, radix: 16 )! // ! - because could be null
}
return result
}}
extension Data {
func hexEncodedString() -> String {
return map { String(format: "%02hhx", $0) }.joined()
}}
extension String {
func subString( _ from: Int, length: Int ) -> String {
let size = self.characters.count
let to = length + from
if from < 0 || to > size {
return ""
}
var result = ""
for ( idx, char ) in self.characters.enumerated() {
if idx >= from && idx < to {
result.append( char )
}
}
return result
}
func removeWord( _ word:String ) -> String {
var result = ""
let textCharArr = Array( self.characters )
let wordCharArr = Array( word.characters )
var possibleMatch = ""
var i = 0, j = 0
while i < textCharArr.count {
if textCharArr[ i ] == wordCharArr[ j ] {
if j == wordCharArr.count - 1 {
possibleMatch = ""
j = 0
}
else {
possibleMatch.append( textCharArr[ i ] )
j += 1
}
}
else {
result.append( possibleMatch )
possibleMatch = ""
if j == 0 {
result.append( textCharArr[ i ] )
}
else {
j = 0
i -= 1
}
}
i += 1
}
return result
}}
我在parms中称呼它
let h = Handeller()
h.Handeller(tmk: "abcdef0123456789", workingKey: "e077b34665f66eb5", pan: "1234432109874567", pin: "0000")