Encryption with DarkBasic: (1 of 2) The Basics by nonZero24th Nov 2012 9:32
|
---|
Summary [PART 1 OF 2] Basic introductory to encryption concepts. Description This code requires you to place a valid filename and path in the variable called "myFile$". This file should NOT be very large as speed is not considered in this demo. You can use a larger image in Part 2. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com //////////////////////////////////////////////////////////////// // Encryption with DarkBasic: (1 of 2) The Basics // //////////////////////////////////////////////////////////////// // * This is free to use, however you see fit. // // * I accept no liability for loss or damages related to the // // use of this code. // // * I provide no warranty or guarantee of any sort regarding // // the code. // // * A credit to the author is not required, but it is always // // appreciated. // // * Make derivs and improvements on this code as much as you // // like but please don't just copy-paste it and claim it as // // your own. How will you face your ancestors then? // // * THE FORMATTING MAY BE OFF due to the codebase's limited // // number of columns and enforced word-wrapping. As such, // // it is recomended you copy and paste it to your editor // // to view it properly. Notepad will even be sufficient. // // // // Cheers, nonZero // //////////////////////////////////////////////////////////////// `` Encryption appeared back in ancient Rome in the the form of crude cyphers, iirc. It was about the only secure `` way of sending a message and being sure that if it fell into the wrong hands, the attatched eyes couldn't make `` any sense of it. For more info on the theory, you can search for some tutorials. We'll be dealing with more `` practical implementation. In otherwords, I'll be giving you t3h cod3z. `` This part (1) is primarily focussed on understanding the basic concept of encryption. The second part will be `` focussed on more complicated mechanics and speed-critical operation. // Add the path to an image of yours to myFile$. Fear not, the file will not be deleted/altered. GLOBAL myFile$: myFile$ = "C:\TestIMG.bmp" GLOBAL newFile$: newFile$ = myFile$ + ".nonZeroMOD" + RIGHT$(myFile$, 4) GLOBAL myMemBlock: myMemBlock = 1 GLOBAL myFileNo: myFileNo = 1 GLOBAL newFileNo: newFileNo = 2 GLOBAL myText$: myText$ = "The Fuhrer is an homonculus" GLOBAL myImage: myImage = 999 // Quick display setup: SET WINDOW ON: SYNC RATE 30 // The following functions illustrate encryption starting with simple things and end with slightly more complex // mechanics. Read through each function carefully, following the printed out info, then run the code for the demo. BasicTextEncryption() BasicAssetEncryption() BasicAssetDecryption() // -------------------------------------------------------------------------------------------------------------- // END // -------------------------------------------------------------------------------------------------------------- // ` _________________________________________________________________________________________________________________ ENCRYPT TEXT Function BasicTextEncryption() // To do this we need to be able to access the text byte by byte. We'll load the text into an array for // illustrative purposes. Note that in a real-life situation, this would be a slow and inefficient method // in DBP. We'll address speed issues later. TheSetup: DIM msgChar(LEN(myText$)) AS BYTE // Create an array to store the message FOR i = 1 TO ARRAY COUNT(msgChar()) msgChar(i) = ASC(MID$(myText$, i)) // Insert ASCII values (byte values) of text into array NEXT i PRINT "The unencrypted message:" // We'll now print out the array as it stands FOR i = 1 TO ARRAY COUNT(msgChar()) PRINT CHR$(msgChar(i)); // Print the character associated with the value NEXT i PRINT "": PRINT "" TheEncryption: PRINT "Now we'll encrypt it..." FOR i = 1 TO ARRAY COUNT(msgChar()) // Time to encrypt it INC msgChar(i), 2 // Increases each character value by 2: a=c, b=d, etc. NEXT i PRINT "": PRINT "" PRINT "The encrypted message:" // We'll now print out the array as it stands FOR i = 1 TO ARRAY COUNT(msgChar()) PRINT CHR$(msgChar(i)); // Print the character associated with the value NEXT i PRINT "": PRINT "" TheDecryption: PRINT "": PRINT "Now we'll decrypt it..." FOR i = 1 TO ARRAY COUNT(msgChar()) // Time to encrypt it DEC msgChar(i), 2 // Decreases each value by the same number it was increased by NEXT i PRINT "": PRINT "" PRINT "The decrypted message:" // We'll now print out the array as it stands FOR i = 1 TO ARRAY COUNT(msgChar()) PRINT CHR$(msgChar(i)); // Print the character associated with the value NEXT i PRINT "": PRINT "" TheCleanUp: DIM msgChar(0) // Erase array WAIT KEY EndFunction ` _________________________________________________________________________________________________________________ ENCRYPT ASSET Function BasicAssetEncryption() // This concerns asset protection - images in this case. It's useful because calls to externals are not needed // which makes the key less exposed. CLS PRINT "Will open the source file and save it" PRINT "(Won't overwrite the original though" PRINT: PRINT "Press any key" WAIT KEY tempByte AS BYTE // Temporary byte to store stuff in OPEN TO READ myFileNo, myFile$ // Open the file MAKE MEMBLOCK FROM FILE myMemBlock, MyFileNo // Make a memblock with the file CLOSE FILE myFileNo myPTR = GET MEMBLOCK PTR(myMemBlock) // It's easier to work with the pointer mbSize = GET MEMBLOCK SIZE(myMemBlock) // We need the size too tmpAddr AS DWORD // Need a temp var for the address. See later IF FILE EXIST(newFile$) = 1 DELETE FILE newFile$ ENDIF OPEN TO WRITE newFileNo, newFile$ // Open the new file that'll be the encrypted version FOR i = 0 TO mbSize - 1 // We go 0 TO size - 1 instead of 1 TO size tempAddr = myPTR + i // We're iterating from the start address tempByte = *tempAddr // Get the byte at address tempAddr and write it to tempByte INC tempByte, 125 // Add 125 to each byte IF i MOD 2 = 0 INC tempByte, 53 // Add another 53 every second byte ENDIF WRITE BYTE newFileNo, tempByte // Write the new byte to the output file NEXT i CLOSE FILE newFileNo CLS PRINT "All done. Will now open the file:" PRINT newFile$ PRINT "in your default program." PRINT "Press any key to proceed..." WAIT KEY EXECUTE FILE newFile$, "", "" CLS PRINT "That prolly caused an error or showed garbage." PRINT "So we'll be decrypting it in the next step." PRINT "Press any key..." WAIT KEY DELETE MEMBLOCK myMemBlock // Delete the memblock holding the original file myPTR = 0 // Set the pointer to null. A good habbit after destroying memory mbSize = 0 // Set mbSize to 0 too // We cannot cheat now EndFunction ` _________________________________________________________________________________________________________________ DECRYPT ASSET Function BasicAssetDecryption() // We'll be decrypting the file we encrypted now. This is the reverse-process. Note that all info about the // original file has been wiped so this would simulate loading an asset. CLS PRINT "Will load the encrypted file into memory" PRINT "Will then decrypt and overwrite it" PRINT "Will then load the image and paste it." PRINT: PRINT "Press any key" WAIT KEY tempByte AS BYTE // We'll be using a temporary byte here too. tmpAddr AS DWORD // Need a temp var for the address. IF FILE EXIST(newFile$) = 0: END: ENDIF // Terminate if the file didn't write/was deleted/etc OPEN TO READ myFileNo, newFile$ MAKE MEMBLOCK FROM FILE myMemBlock, myFileNo // Load into memory CLOSE FILE myFileNo DELETE FILE newFile$ // Now we'll delete the encrypted file (we have it in memory) OPEN TO WRITE myFileNo, newFile$ // And make a new one to write the decrypted data to myPTR = GET MEMBLOCK PTR(myMemBlock) // Once again we'll use the pointer mbSize = GET MEMBLOCK SIZE(myMemBlock) // And the size FOR i = 0 TO mbSize - 1 // We repeat the encryption procedure in reverse tempAddr = myPTR + i tempByte = *tempAddr DEC tempByte, 125 IF i MOD 2 = 0 DEC tempByte, 53 ENDIF WRITE BYTE myFileNo, tempByte NEXT i CLOSE FILE myFileNo EXECUTE FILE newFile$, "", "" // Should now open correctly in the default program LOAD IMAGE newFile$, myImage // Load the decrypted file and hope it all works out PRINT "Done! Press any key..." WAIT KEY DELETE FILE newFile$ EndFunction |