Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text encryption
#2
Cool.  I've always had a fascination with crypto, although I am certainly no expert.  I know just enough about the subject to NEVER trust my own work.  Actually, after testing the program below to make sure it worked, I filed it away and it's been my shameful little secret for years, so YOU, dear reader, are among the only people in the world, besides me, who know about this program.

This was written, as a proof-of-concept, for GWBasic back in the bronze age.

It uses a combination of substitution (by adding a "random" number to each byte), and transposition (rearrangement of bytes).

Due to the repeatable nature of the linear congruential random number generator used by GWB (and QB and QB64PE), the sequence of "random" numbers used for encryption may be easily recreated for decryption.

The program asks for the name of an Input File (which is the file you want to encrypt or decrypt), an Output File (which will contain the encrypted or decrypted data), and a Key (which will be used as the "seed" for Basic's built-in pseudo-random number generator).

Exclamation WARNING: Exclamation Be careful with your filename typing; this program does not ask for confirmation if a file with your Output name already exists!

Code: (Select All)
5 'File Security Series, Part 1.
6 'Due to the nature of GWBASIC's random number generator,
7 'this program must be rerun *EVERY* time it is used.
10 DefInt A-Z: Dim A1(255), A2(255), A3(1, 127)
50 Cls: Print "Pseudo-random file cypher utility.": Print "Written by Jim Race.": Print: Print
51 Input "(E)ncypher, (D)ecypher, or (Q)uit"; I$: I = Int(InStr("EeDdQq", I$) / 2 + .5): If I = 0 Then 51
52 Print
53 If I = 3 Then End

60 FI$ = "": Line Input "Input filename : "; FI$
70 FO$ = "": Line Input "Output filename: "; FO$
80 Print: Input "Key (numeric): ", K

100 Cls: Print "wait...";: Randomize (K)

130 Open "R", 1, FI$, 1: Field 1, 1 As Z1$
140 Open "R", 2, FO$, 1: Field 2, 1 As Z2$

150 On I GOSUB 200, 500
160 Close: Print "done!": Run

199 'Encypher....
200 GoSub 1000: If L = -1 Then Return
210 For A = 0 To I: A1(A) = (A1(A) + Int(Rnd * 9472)) And 255: Next '  Addition of pseudo-random number
220 J = Int(I / 2): For A = 0 To J '  Transposition
230 R = Int(Rnd * (I + 1)): S = Int(Rnd * (I + 1)): Swap A1(R), A1(S): Next
240 GoSub 2000: GoTo 200

499 'Decypher....
500 GoSub 1000: If L = -1 Then Return
510 For A = 0 To I: A2(A) = Int(Rnd * 9472): Next
520 J = Int(I / 2): For A = 0 To J
530 A3(0, A) = Int(Rnd * (I + 1)): A3(1, A) = Int(Rnd * (I + 1)): Next
540 For A = J To 0 Step -1: Swap A1(A3(0, A)), A1(A3(1, A)): Next '  Transposition
550 For A = 0 To I: A1(A) = (A1(A) - A2(A)) And 255: Next '  Subtraction of pseudo-random number
560 GoSub 2000: GoTo 500

999 'Read a random number of bytes from the input file....
1000 L = Int(Rnd * 256): I = 0
1010 If Loc(1) = LOF(1) Then Close: L = -1: Return
1020 Get 1: A1(I) = Asc(Z1$)
1030 If L > 0 And (Loc(1) < LOF(1)) Then I = I + 1: L = L - 1: GoTo 1020
1040 Return

1999 'Write A1(0)...A1(I) to the output file....
2000 For A = 0 To I
2010 LSet Z2$ = Chr$(A1(A)): Put 2: Next
2020 Return


The code above is unchanged except for the added blank lines to break things into functional blocks.  I've always written somewhat "dense" code, ESPECIALLY in olden times.

This is what I've always called a "kid brother" cipher, due to it's reliance on a linear congruential generator.
LCGs are fast, but simple, and NOT cryptographically secure.
This might protect your teenage poetry and sappy love letters from your snot-nosed little sibling, but it WILL NOT stop a knowledgeable code breaker.

Lightly tested under QB64PE.  Compiles & seems to run fine.  Just posting it here in case anyone wants to tinker with it.
(My few simple tests seem to indicate that you can ignore the comments in lines 6 & 7 if compiling with QB64PE.)


(For any would-be employers reading this, my coding style and level of sophistication have greatly improved in the years since this program was written.)
Reply


Messages In This Thread
Text encryption - by AtomicSlaughter - 11-17-2022, 10:59 AM
RE: Text encryption - by JRace - 11-17-2022, 07:59 PM
RE: Text encryption - by mnrvovrfc - 11-17-2022, 09:03 PM
RE: Text encryption - by bplus - 11-17-2022, 09:25 PM
RE: Text encryption - by JRace - 11-17-2022, 09:57 PM
RE: Text encryption - by bplus - 11-17-2022, 10:03 PM
RE: Text encryption - by SpriggsySpriggs - 11-17-2022, 10:03 PM
RE: Text encryption - by JRace - 11-17-2022, 10:22 PM
RE: Text encryption - by Jack - 11-17-2022, 10:58 PM



Users browsing this thread: 5 Guest(s)