Author Topic: Encrypt/Decrypt v0.1  (Read 4867 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Encrypt/Decrypt v0.1
« on: June 14, 2018, 05:56:49 pm »
Hi Guys

here a ASCII program that let to play just a little with cryptography. It shows several methods that are based on the single unique relation between  character before and after traslation...
Enjoy to crypt your message
* encrypt1 prova.bas (Filesize: 13.9 KB, Downloads: 305)
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Encrypt/Decrypt v0.1
« Reply #1 on: June 16, 2018, 10:40:04 am »
Looks like simple substitution BUT! you change the substitution codes as you progress through the message. Correct?

Like ye ol Enigma machine?

Got to say, I don't understand menu and nothing stays up long enough for me to read it (I am slow with bad eyes, it's true.) and where is my coded message going?
« Last Edit: June 16, 2018, 10:52:09 am by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Encrypt/Decrypt v0.1
« Reply #2 on: June 16, 2018, 02:25:57 pm »
Hi Bplus
 Thank's to take a look at my encrypt/decrypt

Yes you are right,
 in this very early version of the program it has been coded the procedure that I have called Traslation, because getting together the avaible symbols to use for the message, we can build a ring (a linear sequence that restart itself) and we can code and decode shifting from original position to coded position and then backward .
The first and the second methods are a variable version of the Giulius Ceaser's cipher as can see at this webpage
 https://en.wikipedia.org/wiki/Caesar_cipher,  while the other methods also if they make a substitution one to one they use different aritmetic method to choose the final coded symbol.
So on other hand if we want call this procedure Substitution, we must say also one to one  in fact substitution one to one because we can also make a substitution one to many in so many manners....

Yes you are right also about time elapsed to read results of coding and decoding....
To give more time to read and let go on to the user that don't want spend all those seconds I must substituite _DELAY with SLEEP
and so I can rise the number of seconds from 2 to 6.
Moreover the output shows original message, result message both in coding both in decoding. And there is also a timestamp... (it is the same because QB64 is very fast!) and the number of characters of the message as checksum.

Here the new version 0.2 of Encrypt/Decrypt

Code: QB64: [Select]
  1. _TITLE "QB64 encrypt/decrypt 0.1"
  2. CONST MessageConst = "QB64 is the best evolution of QB4.5 and PDS 7.1 for multiplatform BASIC programming. Moreover Inform is a powerful tool to use hardly for our projects."
  3. CONST StepConst = 1, JumpConst = 0
  4. CONST ALPHAbeta = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" '26 maiuscole + 26 minuscole + 10 cifre
  5.  
  6. DIM SHARED DecrMessage$
  7.  
  8. '-----------starting  help
  9. COLOR 31, 2
  10. _PRINTSTRING (6, 1), "CRYPTING/DECRYPTING METHODS"
  11. COLOR 15, 1
  12. _PRINTSTRING (6, 2), "Using linear correlation between character at start time and at end time"
  13. _PRINTSTRING (6, 3), "Each message has the same number of characters at the start and at the end"
  14. _PRINTSTRING (6, 5), "Press ESC to end, from 0 to 8 for choosing method, M S R J to set variables"
  15. COLOR 15, 0
  16. help
  17.  
  18. '-----------           setting to default the variables of crypting
  19. mesg$ = MessageConst
  20. steps% = 1 'how calculate next encrypted value
  21. seed% = 1 'starting value to encrytp
  22. jump% = 0 ' how many characters jump the coding
  23.  
  24. ' main loop
  25.     ' here user select method of encrypting to show
  26.     choice$ = INKEY$
  27.     IF LEN(choice$) > 0 AND choice$ <> CHR$(27) THEN ' do nothing but fix bug
  28.         CLS
  29.         SELECT CASE choice$
  30.             CASE "M", "m"
  31.                 ' modifying message to crypting
  32.                 INPUT "Please type the new message and then press Enter, hit only Enter to cancel:", mesg$
  33.                 IF LEN(mesg$) = 0 THEN mesg$ = MessageConst
  34.                 PRINT " you set this message "; mesg$
  35.             CASE "S", "s"
  36.                 'modifying step of codifing
  37.                 INPUT "Please type the number to encode the message: ", steps%
  38.                 IF steps% <= 0 THEN steps% = 1
  39.                 PRINT " you set this STEP"; steps%
  40.             CASE "R", "r"
  41.                 'radix or seed of starting to calculate
  42.                 INPUT "please type the number to start to coding: ", seed%
  43.                 IF seed% <= 0 THEN seed% = 1
  44.                 PRINT " you set this SEED/RADIX "; seed%
  45.             CASE "J", "j"
  46.                 ' jump the number of character of the message to code
  47.                 INPUT "Please type how many characters to jump (no code):", jump%
  48.                 PRINT " you set this JUMP (characters no coded) "; jump%
  49.             CASE "0"
  50.                 ' encryptfixForward
  51.                 encryptFixF mesg$, steps%, jump%
  52.                 encryptFixB DecrMessage$, steps%, jump%
  53.             CASE "1"
  54.                 'encryptfixBackward
  55.                 encryptFixB mesg$, steps%, jump%
  56.                 encryptFixF DecrMessage$, steps%, jump%
  57.             CASE "2"
  58.                 ' progressive linear method increasing (+) by 1-2-3-4 starting from seed%
  59.                 encryptProg mesg$, 1, steps%, seed%, jump%
  60.                 encryptProg DecrMessage$, 2, steps%, seed%, jump%
  61.             CASE "3"
  62.                 ' progressive linear method decreasing (-) by 1-2-3-4 from seed%
  63.                 encryptProg mesg$, 2, steps%, seed%, jump%
  64.                 encryptProg DecrMessage$, 1, steps%, seed%, jump%
  65.             CASE "4"
  66.                 ' progressive linear method  alternating +/- vs -/+ by 1-2-3-4 from seed%
  67.                 encryptProg mesg$, 3, steps%, seed%, jump%
  68.                 encryptProg DecrMessage$, 4, steps%, seed%, jump%
  69.             CASE "5"
  70.                 ' progressive linear method  alternating -/+ vs +/- by 1-2-3-4 from seed%
  71.                 encryptProg mesg$, 4, steps%, seed%, jump%
  72.                 encryptProg DecrMessage$, 3, steps%, seed%, jump%
  73.             CASE "6"
  74.                 ' progressive quadratic method increasing by 1^2 +seed%, 2^2 + seed%, 3^2+ seed%
  75.                 encryptProg mesg$, 5, steps%, seed%, jump%
  76.                 encryptProg DecrMessage$, 6, steps%, seed%, jump%
  77.             CASE "7"
  78.                 ' progressive quadratic method decreasing by 1^2 +seed%, 2^2 + seed%, 3^2+ seed%
  79.                 encryptProg mesg$, 6, steps%, seed%, jump%
  80.                 encryptProg DecrMessage$, 5, steps%, seed%, jump%
  81.             CASE "8"
  82.                 ' progressive quadratic method alternating +/- vs -/+ by 1^2 +seed%, 2^2 + seed%, 3^2+ seed%
  83.                 encryptProg mesg$, 7, steps%, seed%, jump%
  84.                 encryptProg DecrMessage$, 8, steps%, seed%, jump%
  85.             CASE "9"
  86.                 ' progressive quadratic method alternating -/+ vs +/- by 1^2 +seed%, 2^2 + seed%, 3^2+ seed%
  87.                 encryptProg mesg$, 8, steps%, seed%, jump%
  88.                 encryptProg DecrMessage$, 7, steps%, seed%, jump%
  89.  
  90.  
  91.             CASE ELSE
  92.                 ' here we manage exceptions
  93.                 PRINT "THIS CASE IS NOT YET CODED"
  94.         END SELECT
  95.         ' _DELAY 2
  96.         SLEEP 6
  97.         CLS
  98.         help
  99.     END IF
  100. LOOP UNTIL choice$ = CHR$(27)
  101.  
  102. END ' logic end of program
  103.  
  104. '-------------------area of SUBroutines and Functions
  105. SUB help
  106.     PRINT " 0 encryptfixForward"
  107.     PRINT " 1 encryptfixBackward"
  108.     PRINT " 2 progressive linear method increasing "
  109.     PRINT " 3 progressive linear method decreasing "
  110.     PRINT " 4 progressive linear method  alternating +/-"
  111.     PRINT " 5 progressive linear method  alternating  -/+"
  112.     PRINT " 6 progressive quadratic method increasing by n^2"
  113.     PRINT " 7 progressive quadratic method decreasing by n^2"
  114.     PRINT " 8 progressive quadratic method alternating +/- vs -/+ by n^2 "
  115.     PRINT " M to set message to crypting"
  116.     PRINT " S to set step of crypting"
  117.     PRINT " R to set radix of crypting"
  118.     PRINT " J to set how characters to jump"
  119.  
  120. SUB encryptProg (Messag$, step1%, steps%, seed%, jump%)
  121.     ' step1% chooses the method, step2% chooses the step of sequence to add to seed  , seed% is the starting value
  122.     msg$ = Messag$
  123.     MsgLen% = LEN(msg$)
  124.     DecrMessage$ = ""
  125.     step2% = steps%
  126.     b% = 0
  127.     c% = LEN(ALPHAbeta)
  128.     st1% = seed%
  129.     sign% = 1
  130.  
  131.     PRINT msg$, TIME$, MsgLen% 'before traslation
  132.  
  133.     SELECT CASE step1%
  134.         CASE 1 ' progressive linear method increasing (+) by 1-2-3-4 starting from seed%
  135.             FOR a% = 1 TO MsgLen% STEP 1
  136.                 ch$ = MID$(msg$, a%, 1)
  137.                 IF b% < jump% THEN
  138.                     b% = b% + 1
  139.                 ELSE
  140.                     IF INSTR(ALPHAbeta, ch$) > 0 THEN
  141.                         pos1% = INSTR(ALPHAbeta, ch$)
  142.                         st1% = st1% + step2% 'increasing the added factor
  143.                         step2% = step2% + 1 ' linear increasing sequence
  144.                         IF st1% > c% THEN st1% = st1% MOD c%
  145.                         IF (pos1% + st1%) > c% THEN pos1% = (pos1% + st1%) - c% ELSE pos1% = pos1% + st1%
  146.                         ch$ = MID$(ALPHAbeta, pos1%, 1)
  147.                     END IF
  148.                     b% = 0
  149.  
  150.                 END IF
  151.                 DecrMessage$ = DecrMessage$ + ch$
  152.             NEXT
  153.  
  154.             PRINT DecrMessage$, TIME$, LEN(DecrMessage$) 'after traslation
  155.  
  156.         CASE 2 ' progressive linear method decreasing (-) by 1-2-3-4 from seed%
  157.  
  158.             FOR a% = 1 TO MsgLen% STEP 1
  159.                 ch$ = MID$(msg$, a%, 1)
  160.                 IF b% < jump% THEN
  161.                     b% = b% + 1
  162.                 ELSE
  163.                     IF INSTR(ALPHAbeta, ch$) > 0 THEN
  164.                         pos1% = INSTR(ALPHAbeta, ch$)
  165.                         st1% = st1% + step2%
  166.                         step2% = step2% + 1 'increasing the added factor
  167.                         IF st1% > c% THEN st1% = st1% MOD c%
  168.                         IF (pos1% - st1%) < 1 THEN pos1% = (pos1% - st1%) + c% ELSE pos1% = pos1% - st1%
  169.                         ch$ = MID$(ALPHAbeta, pos1%, 1)
  170.                     END IF
  171.                     b% = 0
  172.                 END IF
  173.                 DecrMessage$ = DecrMessage$ + ch$
  174.             NEXT
  175.  
  176.             PRINT DecrMessage$, TIME$ 'after traslation
  177.  
  178.         CASE 3, 4 ' progressive linear method  alternating +/- vs -/+ by 1-2-3-4 from seed%
  179.             IF step1% = 4 THEN sign% = -1
  180.             FOR a% = 1 TO MsgLen% STEP 1
  181.                 ch$ = MID$(msg$, a%, 1)
  182.                 IF b% < jump% THEN
  183.                     b% = b% + 1
  184.                 ELSE
  185.                     IF INSTR(ALPHAbeta, ch$) > 0 THEN
  186.                         pos1% = INSTR(ALPHAbeta, ch$)
  187.                         st1% = st1% + step2%
  188.                         step2% = step2% + 1 'increasing the added factor
  189.                         IF st1% > c% THEN st1% = st1% MOD c%
  190.                         IF sign% = 1 THEN
  191.                             IF (pos1% + st1%) > c% THEN pos1% = (pos1% + st1%) - c% ELSE pos1% = pos1% + st1%
  192.                         ELSE
  193.                             IF (pos1% - st1%) < 1 THEN pos1% = (pos1% - st1%) + c% ELSE pos1% = pos1% - st1%
  194.                         END IF
  195.                         sign% = sign% * -1
  196.                         ch$ = MID$(ALPHAbeta, pos1%, 1)
  197.                     END IF
  198.                     b% = 0
  199.                 END IF
  200.                 DecrMessage$ = DecrMessage$ + ch$
  201.             NEXT
  202.  
  203.             PRINT DecrMessage$, TIME$ 'after traslation
  204.  
  205.         CASE 5 ' progressive quadratic method increasing by 1^2 +seed%, 2^2 + seed%, 3^2+ seed%
  206.             FOR a% = 1 TO MsgLen% STEP 1
  207.                 ch$ = MID$(msg$, a%, 1)
  208.                 IF b% < jump% THEN
  209.                     b% = b% + 1
  210.                 ELSE
  211.                     IF INSTR(ALPHAbeta, ch$) > 0 THEN
  212.                         pos1% = INSTR(ALPHAbeta, ch$)
  213.                         st1% = st1% + (step2% ^ 2) 'increasing the added factor
  214.                         step2% = step2% + 1 ' linear increasing sequence
  215.                         IF st1% > c% THEN st1% = st1% MOD c%
  216.                         IF (pos1% + st1%) > c% THEN pos1% = (pos1% + st1%) - c% ELSE pos1% = pos1% + st1%
  217.                         ch$ = MID$(ALPHAbeta, pos1%, 1)
  218.                     END IF
  219.                     b% = 0
  220.  
  221.                 END IF
  222.                 DecrMessage$ = DecrMessage$ + ch$
  223.             NEXT
  224.  
  225.             PRINT DecrMessage$, TIME$, LEN(DecrMessage$) 'after traslation
  226.  
  227.         CASE 6 ' progressive quadratic method decreasing by 1^2 +seed%, 2^2 + seed%, 3^2+ seed%
  228.  
  229.             FOR a% = 1 TO MsgLen% STEP 1
  230.                 ch$ = MID$(msg$, a%, 1)
  231.                 IF b% < jump% THEN
  232.                     b% = b% + 1
  233.                 ELSE
  234.                     IF INSTR(ALPHAbeta, ch$) > 0 THEN
  235.                         pos1% = INSTR(ALPHAbeta, ch$)
  236.                         st1% = st1% + step2% ^ 2
  237.                         step2% = step2% + 1 'increasing the added factor
  238.                         IF st1% > c% THEN st1% = st1% MOD c%
  239.                         IF (pos1% - st1%) < 1 THEN pos1% = (pos1% - st1%) + c% ELSE pos1% = pos1% - st1%
  240.                         ch$ = MID$(ALPHAbeta, pos1%, 1)
  241.                     END IF
  242.                     b% = 0
  243.                 END IF
  244.                 DecrMessage$ = DecrMessage$ + ch$
  245.             NEXT
  246.  
  247.             PRINT DecrMessage$, TIME$, LEN(DecrMessage$) 'after traslation
  248.  
  249.         CASE 7, 8 ' progressive quadratic method alternating +/- vs -/+ by 1^2 +seed%, 2^2 + seed%, 3^2+ seed%
  250.  
  251.             IF step1% = 8 THEN sign% = -1
  252.             FOR a% = 1 TO MsgLen% STEP 1
  253.                 ch$ = MID$(msg$, a%, 1)
  254.                 IF b% < jump% THEN
  255.                     b% = b% + 1
  256.                 ELSE
  257.                     IF INSTR(ALPHAbeta, ch$) > 0 THEN
  258.                         pos1% = INSTR(ALPHAbeta, ch$)
  259.                         st1% = st1% + step2%
  260.                         step2% = step2% + 1 'increasing the added factor
  261.                         IF st1% > c% THEN st1% = st1% MOD c%
  262.                         IF sign% = 1 THEN
  263.                             IF (pos1% + st1%) > c% THEN pos1% = (pos1% + st1%) - c% ELSE pos1% = pos1% + st1%
  264.                         ELSE
  265.                             IF (pos1% - st1%) < 1 THEN pos1% = (pos1% - st1%) + c% ELSE pos1% = pos1% - st1%
  266.                         END IF
  267.                         sign% = sign% * -1
  268.                         ch$ = MID$(ALPHAbeta, pos1%, 1)
  269.                     END IF
  270.                     b% = 0
  271.                 END IF
  272.                 DecrMessage$ = DecrMessage$ + ch$
  273.             NEXT
  274.  
  275.             PRINT DecrMessage$, TIME$, LEN(DecrMessage$) 'after traslation
  276.  
  277.  
  278.         CASE ELSE
  279.     END SELECT
  280.  
  281.  
  282. SUB encryptFixF (Messag$, step2%, jump%)
  283.     ' crypting fixed forward
  284.     msg$ = Messag$
  285.     IF step2% < 0 THEN step1% = -step2% ELSE step1% = step2%
  286.     MsgLen% = LEN(msg$)
  287.     DecrMessage$ = ""
  288.  
  289.     b% = 0
  290.     c% = LEN(ALPHAbeta)
  291.     PRINT msg$, TIME$, MsgLen% 'before traslation
  292.     FOR a% = 1 TO MsgLen% STEP 1
  293.         ch$ = MID$(msg$, a%, 1)
  294.         IF b% < jump% THEN
  295.             b% = b% + 1
  296.         ELSE
  297.             IF INSTR(ALPHAbeta, ch$) > 0 THEN
  298.                 pos1% = INSTR(ALPHAbeta, ch$)
  299.                 IF (pos1% + step1%) > c% THEN pos1% = (pos1% + step1%) - c% ELSE pos1% = pos1% + step1%
  300.                 ch$ = MID$(ALPHAbeta, pos1%, 1)
  301.             END IF
  302.             b% = 0
  303.         END IF
  304.         DecrMessage$ = DecrMessage$ + ch$
  305.     NEXT
  306.  
  307.     PRINT DecrMessage$, TIME$, MsgLen% 'after traslation
  308.  
  309. SUB encryptFixB (Messag$, step2%, jump%)
  310.     ' crypting fixed backward
  311.     msg$ = Messag$
  312.     IF step2% > 0 THEN step1% = -step2% ELSE step1% = step2%
  313.     MsgLen% = LEN(msg$)
  314.     DecrMessage$ = ""
  315.  
  316.     b% = 0
  317.     c% = LEN(ALPHAbeta)
  318.     PRINT msg$, TIME$, MsgLen% 'before traslation
  319.     FOR a% = 1 TO MsgLen% STEP 1
  320.         ch$ = MID$(msg$, a%, 1)
  321.         IF b% < jump% THEN
  322.             b% = b% + 1
  323.         ELSE
  324.  
  325.             IF INSTR(ALPHAbeta, ch$) > 0 THEN
  326.                 pos1% = INSTR(ALPHAbeta, ch$)
  327.                 IF (pos1% + step1%) < 1 THEN pos1% = (pos1% + step1%) + c% ELSE pos1% = pos1% + step1%
  328.                 ch$ = MID$(ALPHAbeta, pos1%, 1)
  329.             END IF
  330.             b% = 0
  331.         END IF
  332.         DecrMessage$ = DecrMessage$ + ch$
  333.     NEXT
  334.  
  335.     PRINT DecrMessage$, TIME$, MsgLen% 'after traslation
  336.  

STEP and RADIX/SEED affect the result of calculation, JUMP affect what character in the message will be encrypted, MESSAGE let choose a different message to translate.

It maybe interesting to code an engine to attack with brute force this encoder and to get original message without decoding...

Thank's for read and try
Programming isn't difficult, only it's  consuming time and coffee