- A Basic Encryption Program
- 07 Dec 2024 06:45:46 pm
- Last edited by Michael2_3B on 04 Jan 2025 01:15:09 pm; edited 4 times in total
A few years back, I wrote an encryption program that I intended to incorporate into BasicNote CE. While this still sounds like a fun idea, it would be best to avoid bloating the program more than it already is. Nonetheless, I wanted to share this simple program on its own and see what you all thought.
The core principle of the encryption is that it individually shifts each character by a random amount, with rand having been repeatedly seeded by itself (in a sense) multiplied by index values from the password (see the code for a more accurate idea). As you might expect, this works for both encryption and decryption, by changing the shift direction. If the password is incorrect, the decrypted message would be a nonsensical mess.
Let's look at the seeding code. Str8 represents the attempted/created password, and Str9 is the character dictionary. D is the length of Str9.
Code:
rand first gets seeded by a known value. Then, we get a random value, multiply it by the location in the dictionary of each character in Str8, and multiply that by the size of the dictionary put to the power of (1-I+length(Str8)). This value is then used as the new seed for rand. Therefore rand gets seeded multiple times by a value derived from every previous iteration.
Now I'll briefly talk about the encryption/decryption. Since rand has been seeded using the above algorithm, we can now randomly shift each character forwards or backwards (depending on if we are encrypting or decrypting) and build the converted string from there. Str0 represents the string you are encrypting or decrypting, and it is the only meaningful permanent variable that remains outside of the program (you don't want to lose it).
Code:
This is a fairly simple process, and we only have to make sure that the shifting process wraps to keep each character within the range of the established dictionary; thanks to kg583 for helping me fit it on one line.
On a similar note, Str9 represents a dictionary of every possible character that the program/password can use. The program will not allow you to put a token in your password that does not occur in the dictionary. You can, however, put any token you want in your message, but it will get lost/corrupted upon encryption/decryption if it does not occur in Str9.
For some eye candy, here's a full run-through of the program:
And here is the full code:
Code:
Program Download: https://www.mediafire.com/file/qnyrtvpxecb0l2d/APASSWRD.8xp/file
To conclude, this is likely still a terribly insecure encryption method, but from a TI-Basic standpoint, your average user will only be able to decrypt Str0 if they know the original password. Please keep in mind that I wrote this a few years back at this point, but I've done my best to update and optimize it. If you can optimize it further, or have any comments to leave, it is welcome!
EDIT: I've now updated the code to include password verification; this is done by encrypting the original password as part of the message, and seeing if an attempted password successfully decrypts the message and gets the same password back. If the password is incorrect, it will not continue decrypting the rest of the string.
The very presence of password verification does substantially decrease the security of the program, but this can still be somewhat made up for with a long enough password.
The core principle of the encryption is that it individually shifts each character by a random amount, with rand having been repeatedly seeded by itself (in a sense) multiplied by index values from the password (see the code for a more accurate idea). As you might expect, this works for both encryption and decryption, by changing the shift direction. If the password is incorrect, the decrypted message would be a nonsensical mess.
Let's look at the seeding code. Str8 represents the attempted/created password, and Str9 is the character dictionary. D is the length of Str9.
Code:
length(Str8->rand
For(I,1,Ans
randinString(Str9,sub(Str8,I,1))D^(1-I+length(Str8
Ans->rand
End
rand first gets seeded by a known value. Then, we get a random value, multiply it by the location in the dictionary of each character in Str8, and multiply that by the size of the dictionary put to the power of (1-I+length(Str8)). This value is then used as the new seed for rand. Therefore rand gets seeded multiple times by a value derived from every previous iteration.
Now I'll briefly talk about the encryption/decryption. Since rand has been seeded using the above algorithm, we can now randomly shift each character forwards or backwards (depending on if we are encrypting or decrypting) and build the converted string from there. Str0 represents the string you are encrypting or decrypting, and it is the only meaningful permanent variable that remains outside of the program (you don't want to lose it).
Code:
"
For(I,1,length(Str0
Ans+sub(Str9,1+remainder(ErandInt(1,D)+inString(Str9,sub(Str0,I,1))+D-1,D),1
End
This is a fairly simple process, and we only have to make sure that the shifting process wraps to keep each character within the range of the established dictionary; thanks to kg583 for helping me fit it on one line.
On a similar note, Str9 represents a dictionary of every possible character that the program/password can use. The program will not allow you to put a token in your password that does not occur in the dictionary. You can, however, put any token you want in your message, but it will get lost/corrupted upon encryption/decryption if it does not occur in Str9.
For some eye candy, here's a full run-through of the program:
And here is the full code:
Code:
ClrHome
"^^-1sin(cos(tan(^^^2,()/log(789*ln(456-123+0.~ABCDEFGHIJKLMNOPQRSTUVWXYZtheta' :?abcdefghijklmnopqrstuvwxyzsin^-1(cos^-1(tan^-1(pisqrt(|E{}[e]10^(|u|v|w[e^(L4L5L6]L1L2L3[i]Ans!@#$%&=!=><>=<=_|^^3->Str9
length(Ans->D
Disp "Encrypt:1 Decrypt:0
Prompt E
not(not(real(E->E // in case the user is an idiot
Repeat I=M+1
"Password:
If E
"Create "+Ans
Repeat not(sum(seq(not(inString(Str9,sub(Str8,I,1))),I,1,L
Input Ans,Str8
length(Str8->L
End
Ans->rand
For(I,1,Ans
randinString(Str9,sub(Str8,I,1))D^(1-I+L
Ans->rand
End
"De
If E
Then
Input "Message:",Str0
Str8+Str0->Str0
"En
End
Disp Ans+"crypting...
E-not(E->F
length(Str0->M
"
For(I,1,M
Ans+sub(Str9,1+remainder(FrandInt(1,D)+inString(Str9,sub(Str0,I,1))+D-1,D),1
If not(E)I=L and Str8!=sub(Ans,2,I
M+1->I
End
If I=M+2
Disp "Incorrect.
End
sub(Ans,2+Lnot(E),length(Ans)-E-(L+1)not(E
If E
Ans->Str0
DelVar Str8DelVar Str9Ans
Program Download: https://www.mediafire.com/file/qnyrtvpxecb0l2d/APASSWRD.8xp/file
To conclude, this is likely still a terribly insecure encryption method, but from a TI-Basic standpoint, your average user will only be able to decrypt Str0 if they know the original password. Please keep in mind that I wrote this a few years back at this point, but I've done my best to update and optimize it. If you can optimize it further, or have any comments to leave, it is welcome!
EDIT: I've now updated the code to include password verification; this is done by encrypting the original password as part of the message, and seeing if an attempted password successfully decrypts the message and gets the same password back. If the password is incorrect, it will not continue decrypting the rest of the string.
The very presence of password verification does substantially decrease the security of the program, but this can still be somewhat made up for with a long enough password.