The RND() function of VISUALBASIC has an important feature: when the parameter of RND() (we can call it a seed here) is a negative value, the same seed (negative value) generates the same random number sequence. At the same time, VISUALBASIC also has powerful binary technology functions, so that we can achieve file content encryption according to the following ideas:
X=RND(-KEY)
'KEY is a positive number
VAULE=INT(256*RND)'Generate a random number (use this as password)
OpenFILENAME$ForBinaryAs#FILENUM'Open file
Get#FILENUM,I,A'Get file content
B=AXORVAULE'get encrypted file
combine
C=BXORVAULE' gets the decrypted file (B is the content of the encrypted file)
Note: A here is not the entire file content, it can be a very small part, a few bytes or even a single byte. If it is a single byte, each byte in the file will be XORed with a different number, making it difficult to decipher. How big. Specific process:
SubENDECODE(FILENAME$,MA,FILE2$)' parameters are: source file, password, target file
DimFILENUMAsInteger,XAsSingle,IAsSingle
DimCHARNUMAsInteger,RANDOMINTEGERAsInteger
DimSINGLECHARAsString*1,filen2AsInteger' takes a single byte
IfMA<0Them
MA=MA*(-1)
EndIf
X=Rnd(-MA)'parameter is negative
FILENUM=FreeFile
OpenFILENAME$ForBinaryAs#FILENUM'open source file in binary mode
filen2=FreeFile
OpenFILE2$ForOutputAs#filen2'Open the target file in a sequential file
ForI=1ToLOF(FILENUM)'LOF() file length in bytes
Get#FILENUM,I,SINGLECHAR'Get single-byte content
CHARNUM=Asc(SINGLECHAR)
RANDOMINTEGER=Int(256*Rnd)'Get the alphabet
CHARNUM=CHARNUMXorRANDOMINTEGER'XOR
PRint#filen2,Chr$(CHARNUM);'Write to target file
NextI
CloseFILENUM
Closefilen2
ok' calls the success dialog box
EndSub
Call format: ENDECODE source file name, password, target file name
The above process can encrypt and decrypt any EXE, COM, text and other files (odd number of encryption times, even number of decryption times), with excellent reproducibility and excellent confidentiality. If the above process is further processed, such as multiple random processing, Then it will go to a higher level, which I won’t repeat here.
By the way, if the above process also opens and writes the target file as a binary file, then only pure Western text can be encrypted and decrypted. For pure Chinese text, it is modified to take double bytes and the step size of I is 2. Implementation, other (Chinese and Western combined text, EXE, COM and other files) will not get the expected results. The reason may be that characters with ASCII greater than 127 cannot be displayed normally, and the file cannot be written normally with the put statement (only the space), those who are interested may wish to give it a try.
->