Welcome Guest [Log In] [Register]
Welcome to Crypto. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
A utility for replacing multi-grams of texts with extra symbols
Topic Started: Apr 7 2014, 09:21 PM (115 Views)
mok-kong shen
NSA worthy
[ *  *  *  *  *  * ]
The following code is certainly very trivial in logic. Since however what is not easily available tends IMHO often not to be employed at all due to inconvenince of doing one's own programming, I post it nonetheless so that hopefully more people would do such evidently simple yet advantageous pre-processing of their plaintexts before performing certain proper encryptions. The motivation of my coding came from a recent thread of fiziwig, hence my thanks to him.

Code:
 
#[space]A[space]utility[space]for[space]replacing[space]multi-grams[space]of[space]texts[space]with[space]extra[space]symbols.

#[space]07.04.2014.[space]M.[space]K.[space]Shen.[space]mok-kong.shen@t-online.de

#[space]Available[space]at:[space]http://s13.zetaboards.com/Crypto/topic/7178957/1/


#[space]This[space]function[space]serves[space]in[space]relevant[space]contexts[space]the[space]purpose[space]of[space]a[space]feasible[space]simple[space]yet
#[space]advantageous[space]pre-processing[space]of[space]plaintext[space]before[space]its[space]being[space]treated[space]with[space]a
#[space]proper[space]encryption[space]scheme.[space]It[space]may[space]be[space]noted[space]that[space]the[space]2nd[space]and[space]3rd[space]parameters
#[space]could/would[space]in[space]practice[space]be[space]kept[space]secret[space]and[space]variable,[space]including[space]e.g.[space]the
#[space]ordering[space]of[space]the[space]symbols[space]in[space]replacingsymbolstring.

#[space]kn=0:[space]Replacement.[space]kn=1:[space]Restoration.

def[space]replace(alphabet,replacingsymbolstring,replacedstringlist,textstring,kn):
[space][space]replacingsymbollist=list(replacingsymbolstring)
[space][space]leng=len(replacingsymbollist)
[space][space]for[space]i[space]in[space]range(leng):
[space][space][space][space]assert[space]leng==len(replacedstringlist)
[space][space]for[space]c[space]in[space]alphabet:
[space][space][space][space]assert[space]alphabet.count(c)==1[space]and[space]c[space]not[space]in[space]replacingsymbollist
[space][space]for[space]c[space]in[space]replacingsymbollist:
[space][space][space][space]assert[space]replacingsymbollist.count(c)==1
[space][space]u=100
[space][space]for[space]g[space]in[space]replacedstringlist:
[space][space][space][space]assert[space]replacedstringlist.count(g)==1
[space][space][space][space]s=len(g)
[space][space][space][space]assert(s<=u)
[space][space][space][space]u=s
[space][space][space][space]for[space]h[space]in[space]g:
[space][space][space][space][space][space]assert[space]h[space]in[space]alphabet
[space][space]if[space]kn==0:
[space][space][space][space]for[space]c[space]in[space]textstring:
[space][space][space][space][space][space][space]assert[space]c[space]in[space]alphabet
[space][space][space][space]for[space]i[space]in[space]range(leng):
[space][space][space][space][space][space]textstring=textstring.replace(replacedstringlist[i],
[space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space]replacingsymbollist[i])
[space][space]else:
[space][space][space][space]ss=alphabet+replacingsymbolstring
[space][space][space][space]for[space]c[space]in[space]textstring:
[space][space][space][space][space][space]assert[space]c[space]in[space]ss
[space][space][space][space]for[space]i[space]in[space]range(leng):
[space][space][space][space][space][space]textstring=textstring.replace(replacingsymbollist[i],
[space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space]replacedstringlist[i])
[space][space]return(textstring)


#[space]An[space]illustrative[space]example.

#[space]Alphabet[space]must[space]include[space]all[space]characters[space]appearing[space]in[space]textstring[space](including[space]space,
#[space]if[space]any).
alphabet="abcdefghijklmnopqrstuvwxyz"

#[space]No[space]symbol[space]in[space]replacingsymbolstring[space]should[space]be[space]in[space]alphabet.
replacingsymbolstring="RSTUVXY+-?3456789()/&%#*$CDEFGHI"

#[space]All[space]characters[space]in[space]the[space]strings[space]must[space]be[space]in[space]alphabet.[space]Length[space]of[space]strings[space]in[space]the
#[space]list[space]must[space]be[space]non-increasing.[space]Number[space]of[space][space]elements[space]of[space]the[space]list[space]must[space]be[space]equal[space]to
#[space]length[space]of[space]replacingsymbolstring.[space]Note[space]that[space]the[space]elements[space]could[space]be[space]phrases[space]or
#[space]even[space]sentences.
replacedstringlist=["this","ment","tion","the","and","tha","ent","ion","tio",
[space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space]"th","he","an","in","er","re","es","on","ea","ti","at",
[space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space]"st","en","nd","or","to","nt","ed","is","ar","ou","te","of"]

#[space]Given[space]text[space]string.
textstring="thefourthequationthispageiswrong"

#[space]Replacement.
newtextstring=replace(alphabet,replacingsymbolstring,replacedstringlist,
[space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space]textstring,0)

#[space]Restoration.
recoveredtextstring=replace(alphabet,replacingsymbolstring,replacedstringlist,
[space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space][space]newtextstring,1)

print(textstring)
print(newtextstring)
print(recoveredtextstring)
Edited by mok-kong shen, Apr 8 2014, 11:00 PM.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · General · Next Topic »
Add Reply