Friday, July 24, 2020

Simple Encryption with MMBasic

The program presented here is a very simple encryption program, one I have written several times over the years to relearn Basic. This program takes advantage of two built in functions, RND and XOR.

RND is a pseudo random number generator, I say pseudo because it is not a true random number generator. The function needs to be seeded with a number, which it then uses to generate other numbers. If you seed the function with the same number, it will generate the same numbers. To make this appear more random, Basic programmers often seed the function with the Timer function, which seeds RND with the number of seconds past midnight. In this program, I take the password entered, convert each letter to its ASCII number and add them all together and then seed the RND function with this number. The result of this is, each time you enter the same password, the RND function will use the same set of numbers to encrypt or decrypt the message.

XOR is a simple formula that always produces the same 3 numbers. For instance, 1 XOR 2 = 3, 2 XOR 3 = 1, 3 XOR 1 = 2. In this program, I am generating a number using the RND function, then I am taking the letters from the file I want to encrypt, one at a time and converting it to its ASCII number. I am then XOR'ing the two numbers, converting that number into text and writing it to a file. For instance say the RND function generates 35 and the letter from my file a small case "a". The ASCII values of small case "a" is 97. 35 XOR 97 = 33, I then convert 33 to its ASCII text equivalent, which is "!" and I write that to the encrypted file. Re-running the program, this time using the encrypted file as the file you want to encrypt, the process reverses itself. The RND function will produce the same number, 35, and it will read the "!", convert it to its ASCII value of 33, then XOR the two numbers (35 XOR 33 = 97), to produce 97 and convert this number to its ASCII text value of small case "a" and writes that to the decrypted file.



' encrypt.bas
' Uses the RND and XOR functions to encrypt text files
Input "Password: ", Password$
Print

For Count = 1 To Len(Password$)
  MyNumber = MyNumber + Asc(Left$(Password$,Count))
Next

Randomize MyNumber
Input "Enter File to be encrypted: ", FileName$
Print
Open FileName$ For Input As #1
Input "Enter the name to save the encryted file as: ", EncryptedFile$
Print
Open EncryptedFile$ For OutPut As #2
Print "Encrypting the file, please wait..."
Print
 
For Count = 1 To Lof(#1)
  Print ".";
  RealPassword = Int(Rnd * 126) + 1
  Temp$ = Input$(1,#1)
  Encrypt = RealPassword Xor Asc(Temp$)
  If Encrypt < 1 Then Encrypt = Encrypt + 126
  If Encrypt > 126 Then Encrypt = Encrypt - 126
  Encrypt$ = Chr$(Encrypt)
  Print #2, Encrypt$;
Next Count

Close #1
Close #2
Print "Done"
Print
End

Okay, let me make this very clear, this is not a strong encryption by any means, in fact by today's standards, it is barely encryption at all. A low level NSA mook could break this on his coffee break, and even a scrub hacker could break this in an hour using his grandma's computer. The only person this will protect you from is your 12 year old sister, and I would not even bet on that.

The point of this program is simply to teach the basics of encryption, using functions readily available in the Basic programming language. The security can be increased by building your own equivalent functions to RND and XOR using more mathematically complex and security sound formulas. You could also use the RND function to find a location within a key file, say a jpeg image, and use the bit at that location to XOR with the letters needing encrypting and save the file as a binary file rather than a text file. The point of course is, this is just a starting point.

_______________________________________________________________
Edit:
The reason this is not considered secure encryption is because the possible combination of numbers is low by the standards of modern computing. We have three numbers, the psuedo random number generated by RND, the ASCII number of the letter to be encrypted and the ASCII number of the encrypted letter. In theory, this gives us 126x126x126=2,000,376 possible combinations, this is a pretty big number, but not a huge number when you consider that an old 1.8Ghz desktop computer can execute 1,800,000,000 computations per second, that means it takes about .001 seconds to go through all of them for a single letter, that is not much. Now, the reality is, we already know 1 of the three numbers, the encrypted letter, so all we have to do is find the other two, now we are only looking at 128x128=15,876 combinations, now we are talking .000009 seconds for each letter. So the file you are trying to protect would have to be gargantuan to take any significant amount of time to break.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Mastodon