Here is a variation to the opening post.
One of the problems with using a FreeBASIC random number generator is that the secret seed is limited to 2^32 values. In the code below I am using
RndMT where the seed is limited to 2^19937 - 1 values.
So that the encrypted message gets displayed without truncation, nulls in the encrypted string, I have used Paul Squires's FF_Remove function.
That 'Iif(RndMTR(0,-1), flag, -flag)' sees RndMTR() either added or subtracted, randomly.
To make life difficult for an attacker we could use seed = "m!/b6=rO?]4N{apju|srfN#>" which has about 157 bits of entropy and problematic for a brute force attack. We won't find "m!/b6=rO?]4N{apju|srfN#>" in a dictionary either.<laugh>
Home brewed encryption can be fun but we should never take it seriously. Encryption is the domain of cryptographers and to be reviewed by cryptanalysis.
Added: I don't need to say this, do I? Seed is to be input by the user and not hard wired as in the code below.
' Compile GUI application
Code: Select all
#include once "windows.bi"
#Include "RndMT.bi"
Declare Sub EncDec( As String, As String, As Long )
Declare Function FF_Remove( As String, As String ) As String
Dim As String seed, s, displayS
Dim As Long i
seed = "I am not young enough to know everything. Oscar Wilde"
s = "1234567890-=!@#$%^&*()_+{}|[]\:;'<>?,./~`qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
Messagebox( null, s, "Simple encryption", mb_ok )
' Encrypt
EncDec( s, seed, 1 ) ' +1 is for encryption
displayS = FF_Remove( s, Chr(0) )
Messagebox( null, displayS, "Simple encryption", mb_ok )
' Decrypt
EncDec( s, seed, -1 ) ' -1 is for decryption
Messagebox( null, s, "Simple encryption", mb_ok )
Sub EncDec( message As String, seed As String, flag As Long )
Dim As Long i, j
Dim As Byte Dummy
Dim ptrs as zString Ptr
RandomizeRndMT("seed")
For j = 1 To 100 ' number Of rounds
ptrs = StrPtr(message)
For i = 1 To Len(message)
Dummy = Asc( message, i ) + RndMTR(0,255)*Iif(RndMTR(0,-1), flag, -flag)
*ptrs = Dummy
ptrs += 1
Next
Next
End Sub
' By Paul Squires
Function FF_Remove( ByRef sMainString As String, _
ByRef sMatchPattern As String _
) As String
Dim i As Integer
Dim s As String
If Len(sMainString) = 0 Then Return sMainString
s = sMainString
Do
i = Instr(s, sMatchPattern)
If i > 0 Then
s = Left(s, i - 1) & Mid(s, i + Len(sMatchPattern))
End If
Loop Until i = 0
Return s
End Function