Created
February 6, 2024 14:31
-
-
Save quyendang/5f1ae4146f80da206deae679fa6e6711 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let setKey = "matkhau" | |
func xorEncryption(clearText: String) -> String { | |
if clearText.isEmpty { return "" } | |
var encrypted = [UInt8]() | |
let text = [UInt8](clearText.utf8) | |
let key = [UInt8](setKey.utf8) | |
let length = key.count | |
// encrypt bytes | |
for t in text.enumerated() { | |
encrypted.append(t.element ^ key[t.offset % length]) | |
} | |
return bytesToHexString(encrypted) | |
} | |
func xorDecryption(cypherText: String) -> String { | |
if cypherText.count == 0 { return "" } | |
var decrypted = [UInt8]() | |
let cypher = hexStringToBytes(cypherText) | |
let key = [UInt8](setKey.utf8) | |
let length = key.count | |
// decrypt bytes | |
for c in cypher!.enumerated() { | |
decrypted.append(c.element ^ key[c.offset % length]) | |
} | |
return String(bytes: decrypted, encoding: .utf8)! | |
} | |
func bytesToHexString(_ bytes: [UInt8]) -> String { | |
return bytes.map { String(format: "%02X", $0) }.joined() | |
} | |
// Chuyển đổi chuỗi hex thành [UInt8] | |
func hexStringToBytes(_ hexString: String) -> [UInt8]? { | |
var hex = hexString | |
if hex.count % 2 != 0 { | |
hex = "0" + hex | |
} | |
var bytes = [UInt8]() | |
var index = hex.startIndex | |
while index < hex.endIndex { | |
let nextIndex = hex.index(index, offsetBy: 2) | |
if let byte = UInt8(hex[index..<nextIndex], radix: 16) { | |
bytes.append(byte) | |
} else { | |
return nil // Invalid hex string | |
} | |
index = nextIndex | |
} | |
return bytes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.