Straw's B1og.

swift-re常见的加密解密

字数统计: 608阅读时长: 3 min
2024/10/09

swift-re常见的加密解密

在xcode里用swift写了点加密解密,以后都放着这里了。。

RC4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import Foundation
class RC4 {
var state = [UInt8](repeating: 0, count: 256)
init(key: [UInt8]) {
var j:Int = 0
for i in 0..<256 {
state[i] = UInt8(i)
}
for i in 0..<256 {
j = (j + Int(state[i]) + Int(key[i % key.count])) % 256
state.swapAt(i, j)
}
}
func encrypt(data: inout [UInt8]){
var x: Int = 0
var y: Int = 0
for i in 0..<data.count {
x = (x + 1) % 256
y = (y + Int(state[x])) % 256
let temp = state[x]
state[x]=state[y]
state[y]=temp
let t = state[(Int(state[x]) + Int(state[y])) % 256]
data[i] = data[i] ^ t

}
}
}
let key: [UInt8] = Array("qwq".utf8)
let rc4=RC4(key: key)
var text: [UInt8] = Array("straw".utf8)
rc4.encrypt(data: &text)
for i in 0..<text.count{
var texthex=String(format: "0x%02x", text[i])
print("\(texthex)")
}

TEA

加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Foundation
var flag: String = "liufeiyu"
var hexArray:[UInt8?]=[]
for ch in flag{
hexArray.append(ch.asciiValue)
}
var v0:UInt32
var v1:UInt32
var sum:UInt32 = 0
//print(hexArray)
var I32Array:[UInt32]=[]
for i in stride(from: 0, to: hexArray.count, by: 4) {
let byte1 = UInt32(hexArray[i]!)
let byte2 = UInt32(hexArray[i + 1]!) << 8
let byte3 = UInt32(hexArray[i + 2]!) << 16
let byte4 = UInt32(hexArray[i + 3]!) << 24
I32Array.append(byte1 | byte2 | byte3 | byte4)
}
print(I32Array)
let key: [UInt32] = [0x1,0x1,0x4,0x5]
for i in 0..<I32Array.count/2{
v0=I32Array[2*i]
v1=I32Array[2*i+1];
for _ in 0..<32 {
sum = UInt32((UInt64(sum) + UInt64(0x9e3779b9)) & 0xFFFFFFFF)
v0 &+= UInt32((((UInt64(v1) << 4) + UInt64(key[0])) ^ (UInt64(v1) + UInt64(sum)) ^ ((UInt64(v1) >> 5) + UInt64(key[1])))&0xFFFFFFFF)
v1 &+= UInt32((((UInt64(v0) << 4) + UInt64(key[2])) ^ (UInt64(v0) + UInt64(sum)) ^ ((UInt64(v0) >> 5) + UInt64(key[3])))&0xFFFFFFFF)
}
I32Array[2*i]=v0
I32Array[2*i+1]=v1
}
print(I32Array)

还没包装,等后面有时间包装一下

解密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Foundation
let key: [UInt32] = [0x1,0x1,0x4,0x5]
var pw: [UInt32] = [0x5f031f51,0xae732304,0xa6bdef67,0x15c0218b,0x4ec46f64,0x8bfccb46,0x90eebc4a,0xa9aa0c03 ]
var v0:UInt32
var v1:UInt32
var sum:UInt32 = 0
for i in 0..<pw.count/2{
v0=pw[2*i]
v1=pw[2*i+1];
sum=UInt32(UInt64(32*0x9E3779B9)&0xFFFFFFFF)
for _ in 0..<32 {
v1 &-= UInt32((((UInt64(v0) << 4) + UInt64(key[2])) ^ (UInt64(v0) + UInt64(sum)) ^ ((UInt64(v0) >> 5) + UInt64(key[3])))&0xFFFFFFFF)
// print(v1)
v0 &-= UInt32((((UInt64(v1) << 4) + UInt64(key[0])) ^ (UInt64(v1) + UInt64(sum)) ^ ((UInt64(v1) >> 5) + UInt64(key[1])))&0xFFFFFFFF)
// print(v0)
sum = UInt32((UInt64(sum) + UInt64(0x61C88647)) & 0xFFFFFFFF)
// print(sum)
}
for j in 0...3{
var v0hex=String(format: "%c", (v0>>(j*8))&0xff)
print(v0hex,terminator: "")
}
for j in 0...3{
var v1hex=String(format: "%c", (v1>>(j*8))&0xff)
print(v1hex,terminator: "")
}
}

被swift的检测恶心了,数据溢出或者类型不匹配直接报错,必须全加一遍类型修正。

CATALOG
  1. 1. swift-re常见的加密解密
    1. 1.1. RC4
    2. 1.2. TEA
      1. 1.2.1. 加密
      2. 1.2.2. 解密