Entry 3292

ARC4 class

   

Submitted by kenkeiras on March 10, 2010 at 1:32 a.m.
Language: Python. Code size: 1.5 KB.

#!/usr/bin/env python
"""
 Copyright 2010 kenkeiras <kenkeiras@gmail.com>
 Bajo la licencia WTFPL

             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                    Version 2, December 2004

 Everyone is permitted to copy and distribute verbatim or modified
 copies of this license document, and changing it is allowed as long
 as the name is changed.

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. You just DO WHAT THE FUCK YOU WANT TO. 
"""

class arc4:

    # Intercambia dos valores del array 's'

    def swap(self,n,k):

        t=self.s[n]
        self.s[n]=self.s[k]
        self.s[k]=t


    def __init__(self,key):

        # generamos el array 's', normalmente se utilizaria un bucle para llenarlo
        # con los valores de 0 a 255, pero python lo hace mas facil

        self.s=range(0,256)
        k=0
        for n in range(0,256):
            k=(k+self.s[n]+ord(key[n % len(key)]))%256
            self.swap(n,k)

        self.i=0
        self.j=0


    def nextbyte(self):
        
        self.i=(self.i+ 1)% 256
        self.j=(self.j+ self.s[self.i])% 256
        self.swap(self.i,self.j)
        return self.s[(self.s[self.i]+ self.s[self.j])% 256]

    def cipherbyte(self,byte):
        return byte^self.nextbyte()


    # Aunque se usa para cifrar y descrifrar...
    def cipher(self,text):
        a=""
        for c in text:
            a+=chr(self.cipherbyte(ord(c)))
        return a

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).