Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. import UIKit
  2.  
  3. import Foundation
  4. import CommonCrypto
  5.  
  6.  
  7.  
  8. class SecretSpec {
  9. var algorithm: String = "AES/ECB/PKCS5padding"
  10. var key = [UInt8]()
  11.  
  12.  
  13. func SecretSpec(key: [UInt8], algorithm: String){
  14. self.key = key
  15. self.algorithm = algorithm
  16. }
  17.  
  18. func getAlgorithm() -> String {
  19. return self.algorithm
  20. }
  21.  
  22. func getFormat() -> String {
  23. return "RAW"
  24. }
  25.  
  26. func getEncoded() -> [UInt8] {
  27. return self.key
  28. }
  29.  
  30.  
  31.  
  32. func hashCode() -> Int {
  33. var retval: Int = 0
  34. for i in 1...key.count-1 {
  35. retval = retval + Int(key[i]) * Int(i)
  36. }
  37.  
  38. if (algorithm.lowercased() == "tripledes"){
  39. retval = retval ^ Int("desede".hashValue)
  40. return retval
  41. }else{
  42. retval = retval ^ Int(algorithm.lowercased().hashValue)
  43. return retval
  44. }
  45. }
  46. }
  47.  
  48.  
  49.  
  50. extension String {
  51.  
  52. func aesEncrypt(key: String, options:Int = (kCCOptionECBMode + kCCOptionPKCS7Padding)) -> String? {
  53. if let keyData = key.data(using: String.Encoding.utf8),
  54. let data = self.data(using: String.Encoding.utf8),
  55. let cryptData = NSMutableData(length: Int((data.count)) + kCCBlockSizeAES128) {
  56.  
  57. let keyLength = size_t(kCCKeySizeAES128)
  58. let operation: CCOperation = UInt32(kCCEncrypt)
  59. let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128)
  60. let options: CCOptions = UInt32(options)
  61.  
  62. var numBytesEncrypted :size_t = 0
  63.  
  64. let cryptStatus = CCCrypt(operation, algoritm, options,
  65. (keyData as NSData).bytes, keyLength,
  66. nil, (data as NSData).bytes, data.count,
  67. cryptData.mutableBytes, cryptData.length,
  68. &numBytesEncrypted)
  69.  
  70.  
  71. if UInt32(cryptStatus) == UInt32(kCCSuccess) {
  72. cryptData.length = Int(numBytesEncrypted)
  73.  
  74. var bytes = [UInt8](repeating: 0, count: cryptData.length)
  75. cryptData.getBytes(&bytes, length: cryptData.length)
  76.  
  77. var hexString = ""
  78. for byte in bytes {
  79. hexString += String(format:"%02x", UInt8(byte))
  80. }
  81.  
  82. return hexString
  83. }
  84. else {
  85. return nil
  86. }
  87. }
  88. return nil
  89. }
  90. }
  91.  
  92.  
  93.  
  94. func MD5(_ string: String) -> String? {
  95. let length = Int(CC_MD5_DIGEST_LENGTH)
  96. var digest = [UInt8](repeating: 0, count: length)
  97.  
  98. if let d = string.data(using: String.Encoding.utf8) {
  99. _ = d.withUnsafeBytes { (body: UnsafePointer<UInt8>) in
  100. CC_MD5(body, CC_LONG(d.count), &digest)
  101. }
  102. }
  103.  
  104.  
  105. return (0..<length).reduce("") {
  106. $0 + String(format: "%02x", digest[$1])
  107. }
  108. }
  109.  
  110.  
  111.  
  112. var mdT = "YourStrongKeyasdfghjklzxcvbnm"
  113. var algorithm: String = "AES/ECB/PKCS7padding"
  114.  
  115. var s = MD5(mdT)
  116. let buf: [UInt8] = Array(s!.utf8)
  117.  
  118. var skcSpec = SecretSpec()
  119. skcSpec.SecretSpec(key: buf, algorithm: algorithm)
  120.  
  121.  
  122.  
  123. print("hello: (skcSpec)")
  124.  
  125.  
  126. skcSpec.getEncoded()
  127. skcSpec.getFormat()
  128. skcSpec.getAlgorithm()
  129. skcSpec.hashCode()
  130.  
  131.  
  132. let msg: NSMutableDictionary = NSMutableDictionary()
  133.  
  134. msg.setValue("uttam kumar", forKey: "name")
  135. msg.setValue("1001", forKey: "id")
  136.  
  137. let msgData: NSData
  138. var msgStr: String = ""
  139. var requestUrl: String = ""
  140.  
  141. do {
  142. msgData = try JSONSerialization.data(withJSONObject: msg, options: JSONSerialization.WritingOptions()) as NSData
  143. msgStr = NSString(data: msgData as Data, encoding: String.Encoding.utf8.rawValue)! as String
  144.  
  145. } catch _ {
  146. print ("JSON Failure")
  147. }
  148.  
  149.  
  150. var skc = String(data: Data(skcSpec), encoding: .utf8)!
  151. var encoded = msgStr.aesEncrypt(key: String(skc))!
  152.  
  153. print("encoded: (encoded)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement