Skip to content

Instantly share code, notes, and snippets.

@sjbodzo
Created January 8, 2016 16:44
Show Gist options
  • Save sjbodzo/a0e8c5956c176bf1c54d to your computer and use it in GitHub Desktop.
Save sjbodzo/a0e8c5956c176bf1c54d to your computer and use it in GitHub Desktop.
WEP 64 Bit Key Generation
// Created by: Jess Bodzo
// The code below is a simple WEP key generator based off a given passphrase
import UIKit
import Foundation
var psk = "password"
let m = UInt(1 << 32)
let c = UInt(0x269ec3)
let a = UInt(0x343fd)
func wep_encrypt_64(passphrase: String) -> [String] {
/** Convert the passphrase into a unicode value representation **/
var arr: [UInt] = [0,0,0,0]
let unicode_scalars = passphrase.utf16
let unicode_byte_values: [UInt] = unicode_scalars.map({UInt($0 as UInt16)})
/** Generate array values based off cycling through the array and performing XORs **/
for (var i = 0; i < passphrase.characters.count; i++) {
let arr_index = i & 3 // Cycles through indices of arr {0,1,2,3,0,1,...}
arr[arr_index] ^= unicode_byte_values[unicode_byte_values.startIndex.advancedBy(i)]
}
// Linearly shift the values by 8 times the index position
var p = 0
let shifted_arr = arr.map({UInt($0 << UInt(8*p++))})
// Reduce the linear shift result by successively OR'ing with each index
let x = shifted_arr.reduce(0) { (reduction, operand) -> UInt in reduction | operand }
// Generate each part of the key using the recursive relationship defined for WEP
let i_keys = linear_congruential_gen(x, 20)
// Final resulting keys generated by combining the key parts
var keys: [String] = []
// The loop below reduces each cluster of 4 unsigned integers into their hexadecimal representation in a String
for (var j = 0; j < 20; j+=5) {
keys.append(i_keys[j...j+4].reduce(String("")) { (var key, piece) -> String in
return key + String(format: "%2X", piece)
})
}
return keys
}
func linear_congruential_gen(var x: UInt, _ iter: Int) -> [UInt] {
var i_keys: [UInt] = []
for (var i = 0; i < iter; i++) {
x = (a * x + c) % m
i_keys.append((x >> 16) & 0xff)
}
return i_keys
}
var psk_64 = wep_encrypt_64(psk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment