Advertisement
gregwa

FCM 83 - How to program in Python #53

Apr 1st, 2014
390
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import randint, seed
  2. import sys
  3.  
  4. localvalid1 = "abcdefghijklmnopqrstuvwxyz"
  5. localvalid2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  6. localvalid3 = "!#$%&'*+-/=?^_`{|}~."
  7. Offset = 0
  8.  
  9. def IsValidEmail(s,debug=0):
  10.     email = s
  11.     pos = email.rfind("@")
  12.     local = email[:pos]
  13.     domain = email[pos+1:]
  14.     if debug == 1:
  15.         print local
  16.         print domain
  17.     isgood = False
  18.     localvalid = localvalid1 + localvalid2 + localvalid3
  19.     # Check Local Part
  20.     for cntr in range(0,len(local)):
  21.         if local[cntr] in localvalid:
  22.             if debug == 1:
  23.                 print local[cntr],ord(local[cntr]),"True"
  24.             isgood = True
  25.         else:
  26.             if debug == 1:
  27.                 print local[cntr],ord(local[cntr]),"False"
  28.             isgood = False
  29.             break
  30.     r = email.find("..")
  31.     if r > -1:
  32.         isgood = False
  33.     return isgood
  34.    
  35. def CheckSum(s,debug = 0):
  36.     sum = 0
  37.     email = s.upper()
  38.     for cntr in range(0,len(email)):
  39.         if debug == 1:
  40.             print email[cntr],ord(email[cntr])
  41.         sum += ord(email[cntr])
  42.     cs = sum/len(email)
  43.     if debug == 1:
  44.         print('Sum = %d' % sum)
  45.         print('ChkSum = %d' % cs)
  46.         print('ChkSum = %s' % chr(cs))
  47.     return cs,chr(cs)
  48.  
  49. def EncodeKey(s, csum, debug = 0):
  50.     global Offset
  51.     email = s
  52.     Offset = csum - 68
  53.     if debug == 1:
  54.         print("Offset is %d" % Offset)
  55.     NewEmail = ""
  56.     for cntr in range(0,len(email)):
  57.         ch = ord(email[cntr]) + Offset
  58.         if cntr == 1:
  59.             NewEmail = NewEmail + (chr(len(email)+68)) + chr(ch)
  60.         elif cntr == 2:
  61.             NewEmail = NewEmail + chr(csum) + chr(ch)
  62.         else:
  63.             NewEmail = NewEmail + chr(ch)
  64.     if debug == 1:
  65.         print cntr, NewEmail
  66.     return NewEmail
  67.    
  68. def DecodeKey(s,debug = 0):
  69.     global Offset
  70.     eml = ""
  71.     for cntr in range(0,len(s)):
  72.         if debug != 0:
  73.             print cntr,s[cntr],ord(s[cntr])-Offset,chr(ord(s[cntr])-Offset)
  74.         if cntr == 0:
  75.             eml = eml + chr(ord(s[cntr])-Offset)
  76.         elif cntr == 1:
  77.             emllen = ord(s[cntr])-Offset
  78.         elif cntr == 3:
  79.             csumchr=s[cntr]
  80.         else:
  81.             eml = eml + chr(ord(s[cntr])-Offset)
  82.     if debug == 1:
  83.         print eml          
  84.     return eml
  85.    
  86. def DoIt():
  87.     email = raw_input("Please enter email address -> ")
  88.     isok = IsValidEmail(email,0)
  89.     if isok == True:
  90.         csum,csumchr = CheckSum(email)
  91.         ke = EncodeKey(email,csum,0)
  92.         print("License Key    = %s" % ke)
  93.         print("Original email = %s" % DecodeKey(ke,0))
  94.    
  95. if __name__ == "__main__":
  96.     DoIt()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement