CoCalc Public FilesASCII_Pad.sagews
Authors: Liljana Babinkostova, William Unger
Views : 171
Compute Environment: Ubuntu 20.04 (Default)

# ASCII Pad Functions

Description. This is a procedure for converting ordinary text (plaintext) to padded ASCII number. It takes as input a sentence written between " and ", it pads each character one after another, and returns the padded characters as a list. It 'pads' by adding 100 to each list item.

########################################################################################
# The ASCIIPad takes a string (plaintext) as an input and gives a number as an output. #
# The number is represented in a ASCII like format that is padded.                     #
########################################################################################

def ASCIIPad(Message):
newList = []
messLen = len(Message)
tempIter = messLen - 1
while tempIter >= 0:
newList.append(Message[tempIter])
tempIter -= 1
x = [100+ord(newList[i]) for i in range(messLen)];
x = ZZ(x,1000);
return(x);

#################################################################################################
# The input is a number and the output is the original message. If the input is not padded ASCII#
# version of a message it returns the value: "This is not a padded ASCII string"                #
#################################################################################################
def ASCIIDepad(Number):
n = Number.ndigits() % 3;
if (n > 0):
print("This is not a padded ASCII string\n");
else:
L = [((Number - (Number % (1000^i)))/1000^i)%1000 - 100 for i in range(Number.ndigits()/3)];
N = "";
for i in range(Number.ndigits()/3):
N = chr(L[i]) + N;
return(N)


Example. Convert the plaintext "Bob, your public key is not secure!" into an ASCII number. Then depad the resulting number.



M = "Bob, your  public  key is not secure!";
Padded = ASCIIPad(M);
print ("The padded ASCII version of the plaitext: Bob, your public key is not secure! is" , Padded,  "\n")
print ("The function ASCIIDepad returns the original plaintext" , ASCIIDepad(Padded))

Bob, your public key is not secure! B The padded ASCII version of the plaitext: Bob, your public key is not secure! is 166211198144132221211217214132132212217198208205199132132207201221132205215132210211216132215201199217214201133 The function ASCIIDepad returns the original plaintext Bob, your public key is not secure!

The function isASCIIPadded returns a true false value on whether a given number represents a plaintext.

def isASCIIPadded(Number):
N = ""
n = Number.ndigits() % 3;
if (n > 0):
return False;
L = [((Number - (Number % (1000^i)))/1000^i)%1000 - 100 for i in range(Number.ndigits()/3)];
for i in range(Number.ndigits()/3):
if L[i] < 0:
return False
if L[i] > 255:
return False
return True


Example. The function isASCIIPadded is applied to the number 177201215215197203201. It returns "true" value. Next, function isASCIIPadded is applied to the number 177201215215197203701. It returns "false" value.

M = ASCIIPad("Plaintext")
M
isASCIIPadded(M)
P=489357392874032984032984011
P
isASCIIPadded(P)

Plaintext P 180208197205210216201220216 True 489357392874032984032984011 False