• Categories
    • Unread
    • Recent
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Some Encryption program in Python

    Scheduled Pinned Locked Moved Development and Coding
    pythonenryption
    1 Posts 1 Posters 846 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ScuzzS Offline
      Scuzz
      last edited by Scuzz

      Encryption program for a university module

      Usage:

      python joshcrypt.py [-h] [-e] [-d] keyword inputFile outputFile
      

      Flags:

      [-h] Help. This flag will generate help from the command line.
      [-e] Encrypt. Use this flag to encrypt the inputFile.
      [-d] Decrypt. Use this flag to decrypt the inputFile.
      

      Arguments:

      Keyword - The keyword used to encrypt the file.
      inputFile - The Filename to save the encrypted/decrypted file as.
      

      This program needs to be run from the command line; cmd.exe or any terminal emulator will suffice.
      To start the encryption process a user must type:

      python joshcrypt.py -e "keyword" "inputfile" "outputfile"
      

      Keyword is the password used for the document.
      Inputfile is the file that you want to be encrypted.
      Outputfile is the file that you want the encrypted file to be saved as.

      To start the decryption process a user must type:

      python joshcrypt.py -d "keyword" "inputfile" "outputfile"
      

      Keyword is the password used for the document.
      Inputfile is the file that you want to be decrypted.
      Outputfile is the file that you want the plain text file to be saved as.

      # Requires Python 2.7 as Python 3.0+ contains bugs with the array.array() function.
      import argparse
      import array
      import sys
      
      # Opens file as a byte array.
      def getBlock(fileName):
          f = open(fileName, "rb")
          blockArray = array.array('b', f.read())
          f.close
          return blockArray
      
      # Converts bytes to string and writes it to a file.
      def writeBlocks(blocks, fileName):
          f = open(fileName, "wb")
          string = blocks.tostring()
          f.write(blocks)
          f.close()
      
      # Generates a key. 
      # Converts the key into a byte array, creates another byte array of the key but reversed. 
      # Xor the bytes together to generate the final key.
      def genKey(key):
          a = array.array('b', key)
          b = a[::-1]
          for x in range(len(a)):
              a[x] = a[x] ^ b[x]
          return a
      
      # Xor the bytes of the key with the bytes of the file.
      # Loops through the key and adds the times it has looped to the end of the key
      def encrypt(blocks, key):
          i = 0
          r = 0
          for block in range(len(blocks)):
              blocks[block] = blocks[block] ^ key[i]
              if i == len(key) - 1:
                  i = 0
                  r += 1
                  key = genKey(key.tostring() + str(r))
              else:
                  i += 1
          return blocks
      
      def decrypt(blocks, key):
          i = 0
          r = 0
          for block in range(len(blocks)):
              blocks[block] = blocks[block] ^ key[i]
              if i == len(key) - 1:
                  i = 0
                  r += 1
                  key = genKey(key.tostring() + str(r))
              else:
                  i += 1
          return blocks
      
      # Sets up the command line arguments
      # Calls functions to encrypt the file.    
      def main():
          parser = argparse.ArgumentParser(description="Encrypt and Decrypt a file.")
          parser.add_argument("-e", action = "store_true", default = False, dest = "edFlag", help = "Encrypt File")
          parser.add_argument("-d", action = "store_false", default = False, dest = "edFlag", help = "Decrypt File")
          parser.add_argument("keyword", action = "store", help = "Keyword used to encrypt or decrypt file")
          parser.add_argument("inputFile", action = "store", help = "Filename to be encrypted/decrypted")
          parser.add_argument("outputFile", action = "store", help = "Filename to save the encrypted/decrypted file as")
          args = parser.parse_args()
          
          if len(args.keyword) < 10 or len(args.keyword) > 40:
              print "Password needs to be greater than 10 characters and less than 40"
              sys.exit(1)
      
          if args.edFlag:
              print "ENCRYPTING"
              key = genKey(args.keyword)
              blocks = getBlock(args.inputFile)
              encrypted = encrypt(blocks, key)
              writeBlocks(encrypted, args.outputFile)
          else:
              print "DECRYPTING"
              key = genKey(args.keyword)
              blocks = getBlock(args.inputFile)
              decrypted = decrypt(blocks, key)
              writeBlocks(decrypted, args.outputFile)
      
      if __name__ == '__main__':
          main()```
      1 Reply Last reply Reply Quote
      • 1 / 1
      • First post
        Last post
      Online Users