Wednesday, 7 August 2019

Wed 7th Aug 2019 Deeply interested in studying learning more about the art of: Cyptography

The art of Cryptography is something I've always been very deeply interested in learning more about...; namely, how to make 'uncrackable' code...?!

Of course, not being mathematically skilled myself...; therefore, I have to start out doing really 'simple' learning, first. So, This is my own very 'weak' attempt at studying Cryptography.

------------------------------------------------------------------------------------------------------------------------
NOTE: In order to become a really good Cryptographer; then, you would have to know maths brilliantly well. (Otherwise, you might as well just completely forget it?! On the other hand, you can use other programs which will do both the Maths/Cryptography for you.)

On the other hand, if all you wish to learn is just merely' beginners' Cryptography...; then, even a schoolchild can learn it...; that is anybody who can read/write English; and, also, can count up from 1 to 100; will be able to fully 'master' learning beginner's Cryptographical techniques.
------------------------------------------------------------------------------------------------------------------------

The below 5 x 5 grid represents the letters: A-Z.

NOTE(1): 5 x 5 = 25/however, there are 26 letters in the English alphabet;
         so, we now have the problem of how to make the 26th letter fit into a 5x5: 26 letter grid?
         One possible solution is to use the 25th letter position: EE
         to represent 'two' letters at at time; instead, of just merely 'one': Y;
         thus, the 25th letter position now maybe interpreted as being letters, either: Y/or, Z.

NOTE(2): Here the alphabet letters are quite logically arranged using a specific order going from: A up to Z;
         however, it would also be possible to use a purely 'random' alphabet letter arrangement, as well.

      A B C D E
       -  -  -  -   -
A - A B C D E
B - F  G H  I  J
C - K L M N O
D - P Q  R S T
E - U V W X Y/Z

What we need to note here is...the only significant letters of note are: ABCDE...everything else...meaning, any other letter apart from these: FGHIJKLMNOPQRSTUVW, is referred to as being just purely 'random noise'/or, in other words, meaningless.

Each letter can be read off using a combination of two letters.
Read top row letter, first(T)/side column letter next(S).
For example, the word THIS can be encoded as...

TS
ED = T
CB = H
DB = I
DD = S
...or, EDCBDBDD
...which might also be re-written as...
!b7aq6ef1omE4rgp2.'X"Y0,O"D/9?9rop5e£lit8C$ishR34QG^^%BF&*LTDO_NQ-BL+UFdddDratI@ND#~
...so, in the random text string above; the only letters of significant meaning are: 'B/C/E/D';
and, all of the rest of stuff is just purely 'random noise', instead.
NOTE: Only 'UPPER CASE' letters are significant; 'lower case' letters are completely ignored;
      so, lower case: d, means absolutely nothing/and, only upper case: D, actually means something.
------------------------------------------------------------------------------------------------------------------------
ALTERNATE CASING

You can, of course, use alternate UPPER CASE/lower case...so,...
 the 1st letter is significant only if it's UPPERCASE: ABCDE
 the 2nd letter is significant only if it's lower case: abcde
 the 3rd letter is significant only if its UPPERCASE: ABCDE
 the 4th leter is significant only if its lower case: abcde
-etc.

This alternate switching could be based on a keyword: DIM...that repeats...DIMDIMDIM/-etc.
DIM = 04-09-13/EVEN-ODD-ODD
 EVEN = UPPERCASE
  ODD = lowercase
 the 1st letter is UPPERCASE: ABCDE (D/EVEN)
 the 2nd letter is lowercase: abcde (I/ODD)
 the 3rd letter is lowercase: abcde (M/ODD)
 the 4th letter is UPPERCASE: ABCDE (D/EVEN)
-etc.
------------------------------------------------------------------------------------------------------------------------
We could also use a 'rotating' alphabet, so:

T/S: ABCDE->BCDEF->CDEFG->-etc.

...and, so on...until when we reach the end of the alpahabet letter: Z...then, the alphabet is made to wrap around itself starting from the beginning.

VWXYZ->WXYZA->XYZAB->-etc.

Or, if the keyword letter is ODD, then, rotate backwards/if the keyword letter is EVEN, then, rotate forwards.
------------------------------------------------------------------------------------------------------------------------
Another way to do this 'alternate' stuff would be to based it on the length of a keyword/or, name; so if the length of the keyword/or, name was 5 letters long...; then, rotate forwards 5 letters/next, rotate backwards 5 letters/and, so on.
------------------------------------------------------------------------------------------------------------------------
You could also have one side rotating forwards through the alphabet...; and, another side rotating backwards though the alphabet. So,...

   Top side: ABCDE->BCDEF->CDEFG->-etc.
Bottom side: ABCDE->ZABCD->YZABC->-etc.
------------------------------------------------------------------------------------------------------------------------
Or, whether or not the alphabet is made to go forwards/backwards...could be controlled instead by a key...; so,...
 if that key letter is Even(alphanumeric number)/then, go forwards;
 if that key letter is Odd(alphanumeric number/then, go backwards, instead.
(However, I'm not sure if this method would make decoding still possible...?! Or, just too difficult...)
------------------------------------------------------------------------------------------------------------------------
In fact, we can either choose to rotate the: rows/column letters...or, rotate, the alphabet position itself/or, indeed, you could choose to do both. Such as...

Rotate the alphabet letters going forwards;
Rotate both the row/column letters going backwards.
-Etc.
------------------------------------------------------------------------------------------------------------------------
A slightly more advanced theory.

My idea is...for each letter of the alphabet...create a random sequence of Alphabet letters: A-Z; eg.

A: jcqxkwdpvlyounfzbtimesarhg
...and, then, do the same thing for each of the other alphabet letters: B through to Z.

Now, when you've selected a letter...as you would normally do up above...EE = T/AA= A...then, use the 1st letter of that random string to replace it; thus, A = J; next, rotate that random string around by +1 to become...

A: cqxkwdpvlyounfzbtimesarhgj

The letter A is now ready for re-use...only this time it shows up as being 'C' instead of the previous 'J'.

Now, let me carefully trace back through this last argument...to see if it acutally makes any real sense? If you can encode any letter of the alphabet as being any other letter of the alphabet...then, that means you could get back something that reads like this...
JJJJJ
...now, how in the hell are your going to decrypt that to return back plain English...the answer surely is, no, you can't! So, that's the big problem one can run into when trying to design Cryptocodes...namely, if you can 'do' it.../then, can you also, 'undo' it...?! Because if all you can do is 'encrypt'...and, not also do it's exact opposite which is to 'decrypt'...then, what you've gone and created is an impossible mess, I'm afraid!
------------------------------------------------------------------------------------------------------------------------
CONCLUSION

Cryptography, is to some degree purely 'theoretical'...that is to say based on mere 'ideas', alone.

However, when we choose to combine it together with 'computer programming'; then, all of a sudden those mere theories can be  put to sound 'practical' usage, instead.

This is to say that we as human beings would have a hard time; and, take really quite long to both 'encode/decode' Ciphertext; however, the computer...because it can do millions/or, even billions of operations per second...therefore, it can usually 'encode/decode', right away, instantly; which, otherwise, such a task might take us human beings: minutes/hours/even, days...(having to go check/and, then, re-check). So, in this sense, for the purpose of dealing with sheer complexity/and, also, for speed...computers are ideally suited to solving this type of task.
------------------------------------------------------------------------------------------------------------------------
COMPUTER PROGRAM

DESIGNING AN ALGORITHM

Get the user to type in their chosen column letter: (A-E)
Get the user to type in their chosen row number: (A-E)
Next, do the calculation
Finally, output the calculated result

((RowNumber-1)*5) + ColumnNumber

in the case of if the user entered: D,D...
    row number is: ( (4 - 1 = 3) * 5 ) = 15 + column number: 4 = 19
    alphabet letter number 19 is: S

...now, let's go create the program...

The code is stored at the following GitHub URL  address:

https://github.com/pramnora/qb64/blob/master/5x5-decrypt-01.bas

--------CODE START...

'-------------------------------------
'***  PROGRAM: CRYPTOGRAPHY/DECRYPT...
'    LANGUAGE: QBASIC: Version: QB64
'    COMPUTER: PC/WINDOWS 10
'-------------------------------------
'      AUTHOR: Mr. Paul Ramnora
'       EMAIL: pramnora@yahoo.com
'-------------------------------------
'     CREATED: 070819 22:16 PM GMT
'     UPDATED: 070819 22:16 PM GMT
'-------------------------------------

'*** This program selects a single character from a 5 x 5 alphabet letters grid: A-Y;
'    the user has to enter in both the : Column letter: A-E; plus, the Row letter: A-E.

'----------------------------
'*** Variable declarations...
'=---------------------------

'---string: str/($)...

strColumnNo$ = ""
strRowNo$ = ""

'--- integer: int/(%)...

intColNo% = 0
intRowNo% = 0
intTotal% = 0

'-------------------
'*** Main program...
'--------------

GOSUB clearScreen

GOSUB getColumnLetter
intColNo% = ASC(UCASE$(strColNo$)) - 64

GOSUB getRowLetter
intRowNo% = ASC(UCASE$(strRowNo$)) - 64

GOSUB doCalculation
PRINT CHR$(intTotal% + 64)

END

'-------------------
'*** Sub-routines...
'-------------------

clearScreen:
CLS
RETURN

getColumnLetter:
INPUT "Column letter (A/B/B/D/E)"; strColNo$
RETURN

getRowLetter:
INPUT "Row letter: (A/B/C/D/E)"; strRowNo$
RETURN

doCalculation:
intTotal% = ((intRowNo% - 1) * 5) + intColNo%
RETURN


-------CODE END.

The minimalist version of this program is just 9 lines long(with no code comments/sub-routines):

strColumnNo$ = "": strRowNo$ = ""
intColNo% = 0: intRowNo% = 0: intTotal% = 0
CLS
INPUT "Column letter (A/B/B/D/E)"; strColNo$
intColNo% = ASC(UCASE$(strColNo$)) - 64
INPUT "Row letter: (A/B/C/D/E)"; strRowNo$
intRowNo% = ASC(UCASE$(strRowNo$)) - 64
intTotal% = ((intRowNo% - 1) * 5) + intColNo%
PRINT CHR$(intTotal% + 64)

In fact, it's still possible to minimalise the program even further still down to 8 lines...(taking out long file names/doing calculations and then printing all on one single line/-etc.):

cn$ = "": rn$ = ""
cn% = 0: rn% = 0: t% = 0
CLS
INPUT "Column letter (A/B/B/D/E)"; cn$
cn% = ASC(UCASE$(cn$)) - 64
INPUT "Row letter: (A/B/C/D/E)"; rn$
rn% = ASC(UCASE$(rn$)) - 64
PRINT CHR$((((rn% - 1) * 5) + cn%) + 64)

Of course, what one gains in terms of speed/and, also, sheer brevity of code...; one loses in terms of 'readability'/and, therefore, ease of understanding/changing and updating the code, later on.

Because, QB64 doesn't demand programmers declare variables before using them; you could even reduce it down to just merely 6 lines of code, instead.

CLS
INPUT "Column letter (A/B/B/D/E)"; cn$
cn% = ASC(UCASE$(cn$)) - 64
INPUT "Row letter: (A/B/C/D/E)"; rn$
rn% = ASC(UCASE$(rn$)) - 64
PRINT CHR$((((rn% - 1) * 5) + cn%) + 64)

Now, compare 6 lines of code...; with 67 lines of more fully commented code!

In fact, if you use the code compression technique of putting 2 lines on one line; then, we get...just 3 lines of code, instead:-

CLS: INPUT "Column letter (A/B/B/D/E)"; cn$
cn% = ASC(UCASE$(cn$)) - 64: INPUT "Row letter: (A/B/C/D/E)"; rn$
rn% = ASC(UCASE$(rn$)) - 64: PRINT CHR$((((rn% - 1) * 5) + cn%) + 64)

...which when I tested it...still works, actually. ;-)

No comments:

Post a Comment