Skip to content

Instantly share code, notes, and snippets.

@quyendang
Created February 6, 2024 14:31
Show Gist options
  • Save quyendang/5f1ae4146f80da206deae679fa6e6711 to your computer and use it in GitHub Desktop.
Save quyendang/5f1ae4146f80da206deae679fa6e6711 to your computer and use it in GitHub Desktop.
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
}
@quyendang
Copy link
Author

quyendang commented Feb 6, 2024

var point = 1993
var encryptedText = xorEncryption(clearText: "\(point)")
print(encryptedText)
var decryptedText = xorDecryption(cypherText: encryptedText)
var decryptedPoint = Int(decryptedText) ?? 0
print(decryptedPoint)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment