Author Topic: Encryption/Decryption Function  (Read 2626 times)

0 Members and 1 Guest are viewing this topic.

Offline loudar

  • Newbie
  • Posts: 73
  • improve it bit by bit.
    • View Profile
Encryption/Decryption Function
« on: July 30, 2019, 07:44:22 am »
Hey everyone! Wrote two little functions that will encrypt/decrypt any given string with the two keys rawkey$ (it will be redefined after completing the first encryption). The two encryptions on top of each other also make bruteforcing pretty much impossible.
The code generated is different each time, even with the same input and the same keys.

Use this for your programs when saving passwords or other data you don't want to be read out ;)

I also wrote a program that will do the same, but you'll have a little menu to navigate around and can easily copy/paste data to/from clipboard if you don't want to automatically write files but e.g. send messages or similar:
https://loudar.myportfolio.com/cara


Code: QB64: [Select]
  1. 'variables for encryption
  2. length = 1000
  3. DIM rndlet(length) AS INTEGER
  4. DIM addlet(length) AS STRING
  5. DIM finalASC(length) AS INTEGER
  6. DIM toadd(length) AS STRING
  7. DIM keyasc(length) AS INTEGER
  8. DIM rawlet(length) AS STRING
  9.  
  10. 'variables for decryption
  11. DIM findpos(length) AS INTEGER
  12. DIM number(length) AS STRING
  13. DIM ascvalue(length) AS INTEGER
  14. DIM cryptletter(length) AS STRING

Code: QB64: [Select]
  1. FUNCTION encrypt$ (rawdata$)
  2.     encrun = 0
  3.     DO
  4.         encrun = encrun + 1
  5.         IF encrun = 1 THEN
  6.             rawkey$ = "enter the first key here"
  7.         ELSE
  8.             rawkey$ = "enter the second key here"
  9.         END IF
  10.         length = LEN(rawdata$)
  11.         DIM rndlet(length) AS INTEGER
  12.         DIM addlet(length) AS STRING
  13.         DIM keyasc(length) AS INTEGER
  14.         DIM rawlet(length) AS STRING
  15.         DIM finalASC(length) AS INTEGER
  16.         DIM toadd(length) AS STRING
  17.         c = 0
  18.         DO
  19.             c = c + 1
  20.  
  21.             genRNDLET:
  22.             rndlet(c) = INT(RND * 90) + 32
  23.             IF rndlet(c) > 47 THEN
  24.                 IF rndlet(c) < 68 THEN
  25.                     GOTO genRNDLET
  26.                 END IF
  27.             END IF
  28.             addlet$(c) = CHR$(rndlet(c))
  29.  
  30.             genFINALASC:
  31.             l = 0
  32.             DO
  33.                 l = l + 1
  34.                 keylet$ = MID$(rawkey$, l, 1)
  35.                 keyasc(c) = keyasc(c) + ASC(keylet$)
  36.             LOOP UNTIL l = LEN(rawkey$)
  37.             rawlet$(c) = MID$(rawdata$, c, 1)
  38.             finalASC(c) = ASC(rawlet$(c)) + keyasc(c) + rndlet(c)
  39.  
  40.             toadd$(c) = addlet$(c) + MID$(STR$(finalASC(c)), 2, LEN(STR$(finalASC(c) - 1)))
  41.             edata$ = edata$ + toadd$(c)
  42.         LOOP UNTIL c = LEN(rawdata$)
  43.         IF encrun = 1 THEN
  44.             rawdata$ = edata$
  45.         ELSE
  46.             encrypt$ = edata$
  47.         END IF
  48.         rawkey$ = ""
  49.     LOOP UNTIL encrun = 2

Code: QB64: [Select]
  1. FUNCTION decrypt$ (edata$)
  2.     decrun = 0
  3.     DO
  4.         decrun = decrun + 1
  5.         IF decrun = 1 THEN
  6.             rawkey$ = "enter the second key from encrypt$ here"
  7.         ELSE
  8.             rawkey$ = "enter the first key from encrypt$ here"
  9.         END IF
  10.         u = 0
  11.         DO
  12.             u = u + 1
  13.             number$(u) = ""
  14.         LOOP UNTIL u = LEN(edata$)
  15.         l = 0
  16.         DO
  17.             l = l + 1
  18.             keyasc(l) = 0
  19.         LOOP UNTIL l = LEN(rawkey$)
  20.  
  21.         c = 0
  22.         letpos = 0
  23.         DO
  24.             letpos = letpos + 1
  25.             IF ASC(MID$(edata$, letpos, 1)) > 32 THEN
  26.                 IF ASC(MID$(edata$, letpos, 1)) < 48 OR ASC(MID$(edata$, letpos, 1)) > 57 THEN
  27.                     c = c + 1
  28.                     rndlet(c) = ASC(MID$(edata$, letpos, 1))
  29.                     findpos(c) = letpos
  30.                     endsearch = 0
  31.                     DO
  32.                         letpos = letpos + 1
  33.                         IF letpos = LEN(edata$) + 1 THEN
  34.                             GOTO endsearch
  35.                         END IF
  36.                         IF ASC(MID$(edata$, letpos, 1)) > 47 THEN
  37.                             IF ASC(MID$(edata$, letpos, 1)) < 58 THEN
  38.                             ELSE
  39.                                 endsearch = 1
  40.                             END IF
  41.                         ELSE
  42.                             endsearch = 1
  43.                         END IF
  44.                     LOOP UNTIL endsearch = 1
  45.                     endsearch:
  46.                     letpos = letpos - 1
  47.                     numbers = letpos - findpos(c)
  48.                     u = 0
  49.                     DO
  50.                         u = u + 1
  51.                         number$(c) = number$(c) + MID$(edata$, findpos(c) + u, 1)
  52.                     LOOP UNTIL u = numbers
  53.                     ascvalue(c) = VAL(number$(c))
  54.                     l = 0
  55.                     DO
  56.                         l = l + 1
  57.                         keylet$ = MID$(rawkey$, l, 1)
  58.                         keyasc(c) = keyasc(c) + ASC(keylet$)
  59.                     LOOP UNTIL l = LEN(rawkey$)
  60.                     cryptletter$(c) = CHR$(ascvalue(c) - keyasc(c) - rndlet(c))
  61.                     rawdata$ = rawdata$ + cryptletter$(c)
  62.                 END IF
  63.             END IF
  64.             continue:
  65.         LOOP UNTIL letpos = LEN(edata$)
  66.         IF decrun = 1 THEN
  67.             edata$ = rawdata$
  68.         ELSE
  69.             decrypt$ = rawdata$
  70.         END IF
  71.         rawdata$ = ""
  72.     LOOP UNTIL encrun = 2
« Last Edit: July 30, 2019, 07:59:40 am by loudar »
Check out what I do besides coding: http://loudar.myportfolio.com/