Python Crypto Script: Send Secret Message

  



Berteriak untuk semua orang di Indonesia! Terima kasih!

Thanks for the support I have been getting from Indonesia!(May 2021)  

This post is dedicated to team Indonesia!

Being a person who is constantly thinking about all facets of security, I often land on privacy.  Now we have a lot of methods of protecting our identities and in this blog we highlight numerous ways to protect our identity.

I have written two scripts.  A script that encrypts a secret message (text file) and a script that decrypts that file.  As part of the fun, the user needs to input the correct numerical digit sequence in order to decrypt the file correctly.  The script can be run with a massive string of numbers to create a very complex text file.  There are also more methods that one could incorporate to add complexity. Really this is just a POC.  

Below you will find the Python scripts to encrypt/decrypt.  Before we get there I will give you the general script usage.

Mysterious User #1(MU1)  has a message they want to send too Mysterious User #2(MU2).  Now these two characters are fun and they want to avoid the "normal" methods of encrypting (which are pretty good, but stick with me here).  They like playing with fire and sending their message in a clear text file.

What now!? That means that anyone could intercept this file and see it's contents!

Well yes, but if you look below this is what the output looks like.


Somewhere in there is MU1's message to MU2.

MU2 receives the text file from their friend, MU1 runs the decryption script and is able to read the secret message.

Below you will find the code to which you can copy and paste.  I will explain the functionality of the code and how to use it.

Usage:

#python crypto.py YOUR_SECRET_FILE.txt <string of numbers>


To decrypt

#python decrypto.py YOUR_RECEIVED_FILE.txt <the secret combo #>



Code Explained:

I have written the code in a very straight forward and logical way (I think at least).  This is the basic function.

-secret file is fed to a function

-the second argument form terminal is saved and is used to create a "padding"

-the padding is a random assortment of numbers

-padding is placed on either side of the secret message

-secret message is split up by characters and hard coded string of 5 characters is inserted between each character.

-all characters are converted to decimal and saved in an output file.

fast forward to the decryption phase

-received file is sent to function and work done on it

-second argument is used to strip off padding from beginning and end of file

-decimal is converted to ascii

-message is joined from list to a string

-iterate length of the string until we reach the known length of the 5 hard coded characters, at which point we add that character to a growing string.

-print string


Here is the code for "crypto.py"

#crypto.py

import sys
import random

decMessage=[]
decCode=[]
encoded=[]
randBegin=[]
randEnd=[]
newTotal=[]


def outSecret(sList):
	f=open("output.txt","w+")
	
	for x in sList:
		f.write(str(x)+" ")
	f.close()

def splitMessage(sMessage):
	return [char for char in sMessage]
def mySecret(sFile,sCode):
	
	f = open(sFile,"r")
	message=f.read()
	charMessage=splitMessage(message)
	
	for x in charMessage:
		decMessage.append(ord(x))

	charCode = splitMessage(sCode)
	for y in charCode:
		decCode.append(ord(y))
	
	for num in decMessage:
		encoded.append(num)
		encoded.append(random.randint(33,122))
		encoded.append(random.randint(33,122))
		encoded.append(random.randint(33,122))
		encoded.append(random.randint(33,122))
		encoded.append(random.randint(33,122))

	decPadding=int(sCode)

	for z1 in range(0,decPadding):
		
		randBegin.append(random.randint(33,122))
		randEnd.append(random.randint(33,122))

	f.close()

mySecret(sys.argv[1],sys.argv[2])
newTotal = randBegin+encoded+randEnd

outSecret(newTotal)

Below is the code for "decrypto.py"

#decypto.py

import sys

secretMessage=[]

revealed=''
sep = ', ,'
def openSecret(sFile,sCode):
	f=open(sFile,"r")
	data=f.read()
	breakUp=data.split(" ")

	padding=int(sCode)
	
	
	message = breakUp[padding:-padding]
	for x in message:
		
		val = chr(int(int(x)))
		
		secretMessage.append(val)
	


openSecret(sys.argv[1],sys.argv[2])
joined = sep.join(secretMessage)

for item in range(0,len(joined)):
	
	if item % 24 == 0:
		revealed+=str(joined[item])
print(revealed)



Popular Posts