daily pastebin goal
30%
SHARE
TWEET

xor

a guest Mar 25th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2. import CommonCrypto
  3.  
  4. extension String {
  5.  
  6.     var md5: String {
  7.         let str = self.cString(using: .utf8)
  8.         let strLen = CC_LONG(self.lengthOfBytes(using: String.Encoding.utf8))
  9.         let digestLen = Int(CC_MD5_DIGEST_LENGTH)
  10.         let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
  11.  
  12.         CC_MD5(str!, strLen, result)
  13.  
  14.         let hash = NSMutableString()
  15.         for i in 0..<digestLen {
  16.             hash.appendFormat("%02x", result[i])
  17.         }
  18.  
  19.         return String(format: hash as String)
  20.     }
  21.  
  22.     var hexadecimal: Data? {
  23.         var data = Data(capacity: self.count / 2)
  24.  
  25.         let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
  26.         regex.enumerateMatches(in: self, range: NSRange(startIndex..., in: self)) { match, _, _ in
  27.             let byteString = (self as NSString).substring(with: match!.range)
  28.             let num = UInt8(byteString, radix: 16)!
  29.             data.append(num)
  30.         }
  31.  
  32.         guard data.count > 0 else { return nil }
  33.  
  34.         return data
  35.     }
  36.  
  37. }
  38.  
  39. class ViewController: UIViewController {
  40.  
  41.     let currentTimeInMillis: Int = 1553265944
  42.  
  43.     override func viewDidLoad() {
  44.         super.viewDidLoad()
  45.         // Do any additional setup after loading the view, typically from a nib.
  46.  
  47.         let password = self.getEncryptedPass(password: "utd3mz9k", currentTimeInMillis: self.currentTimeInMillis)
  48.         print(password)
  49.         //oqp\/INLVt9A=
  50.     }
  51.  
  52.     func getEncryptedPass(password: String, currentTimeInMillis: Int) -> String {
  53.  
  54.         let currentTimeReversed = String("\(currentTimeInMillis)".reversed())
  55.         let currentTimeReversedMd5 = currentTimeReversed.md5
  56.  
  57.         if let keyData = currentTimeReversedMd5.hexadecimal {
  58.  
  59.             let dataBytes = password.data(using: .utf8)!
  60.             let xorResult = self.xor(data: dataBytes, with: keyData)
  61.  
  62.             return xorResult.base64EncodedString()
  63.         }
  64.  
  65.         return ""
  66.     }
  67.  
  68.     func xor(data: Data, with key: Data) -> Data {
  69.         var xorData = data
  70.         var tempData = xorData
  71.  
  72.         tempData.withUnsafeMutableBytes { (start: UnsafeMutablePointer<UInt8>) -> Void in
  73.             key.withUnsafeBytes { (keyStart: UnsafePointer<UInt8>) -> Void in
  74.                 let b = UnsafeMutableBufferPointer<UInt8>(start: start, count: xorData.count)
  75.  
  76.                 let k = UnsafeBufferPointer<UInt8>(start: keyStart, count: data.count)
  77.                 let length = data.count
  78.  
  79.                 for i in 0..<xorData.count {
  80.                     b[i] ^= k[i % length]
  81.                 }
  82.             }
  83.         }
  84.  
  85.         return tempData
  86.     }
  87.  
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top