ModdersCentral

Full Version: Python Simple String Encryption
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
1) reverse encryption
Code:
def encryptstring(strhere):
    chars = str.lower('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789')
    chars_r = chars[::-1]
    tmp_str = ''
    for i in range(len(strhere)):
      pos = chars.find(strhere[i])
      tmp_str += chars_r[int(pos)]
    return tmp_str
print (encryptstring('hello'))
2)Vigenere cipher

Code:
import base64
class Vigenere_cipher():
    def __init__(self, e_key):
        self._ekey = e_key
    def encrypt(self,clear_text):
        enc = []
        for i in range(len(clear_text)):
            key_c = self._ekey[i % len(self._ekey)]
            enc_c = chr((ord(clear_text[i]) + ord(key_c)) % 256)
            enc.append(enc_c)
        return base64.urlsafe_b64encode("".join(enc))  
    def decrypt(self,enc_text):
        dec = []
        enc_text = base64.urlsafe_b64decode(enc_text)
        for i in range(len(enc_text)):
            key_c = self._ekey[i % len(self._ekey)]
            dec_c = chr((256 + ord(enc_text[i]) - ord(key_c)) % 256)
            dec.append(dec_c)
        return "".join(dec) 
 

person1 = Vigenere_cipher("john")  
#person1.setFirstname("johnny")
#print(person1.encrypt("hello"))
print(person1.decrypt(person1.encrypt("hello")))
3) rot 13
Code:
def rot13(s):
    result = "" 
    for v in s: 
        c = ord(v) 
        if c >= ord('a') and c <= ord('z'):
            if c > ord('m'):
                c -= 13
            else:
                c += 13
        elif c >= ord('A') and c <= ord('Z'):
            if c > ord('M'):
                c -= 13
            else:
                c += 13 
        result += chr(c) 
    return result
www.000webhost.com