Created
November 9, 2012 20:57
-
-
Save alonecuzzo/4048190 to your computer and use it in GitHub Desktop.
My RC4 attempt
This file contains 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
class RC4(object): | |
#initilization- take in the key and assign it | |
def __init__(self, key): | |
self._key = key | |
def _setKey(self, key): | |
self._key = key | |
def _getKey(self): | |
return self._key | |
#encryption function | |
def encrypt(self, string_to_encrypt): | |
_initialize() | |
return_string = '' | |
string_len = len(string_to_encrypt) | |
k = '' | |
t = '' | |
c = 0 | |
while(c<string_len): | |
k = math.fabs(_generatePRGA()) | |
t = string_to_encrypt[c] #stores last char taken from the array | |
return_string += hex(bool(k) ^ bool(t)) #appends the string with the bitwise_or of k and t and then converts it to hexidecimal | |
return_string += ' ' #add a space | |
return return_string | |
#initialize the array and work through the key | |
def _initialize(self): | |
self._s = [] | |
x = 0 | |
while(x<255): | |
s[x] = x | |
x = x + 1 | |
b = 0 | |
self._j = 0 | |
while(b<255): | |
self._j = (self._j + self._s[b] + key[b % len(self._key)]) % 256 | |
_swap(b,self._j) | |
self._j = 0 | |
self._i = 0 | |
#generates the psuedo random algorithm | |
def _generatePRGA(self): | |
self._i = (i + 1) % 256 | |
self._j = (self._j + self._s[self._i] % 256 | |
#swap the two | |
_swap(self._i,self._j) | |
return self._s[(self._s[self._i] + self._s[self._j]) % 256] | |
#swaps arg places in s array | |
def _swap(self, arg1, arg2): | |
tempChar = self._s[arg1] | |
self._s[arg1] = self._s[arg2] | |
self._s[arg2] = tempChar | |
#properties | |
key = property(_getKey, _setKey) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment