Encryption with DarkBasic: (2 of 2) Encrypting With DBP by nonZero24th Nov 2012 9:50
|
---|
Summary [PART 2 OF 2] This demonstrates the concepts of Part 1 in action. Description This code requires you to place a valid filename and path in the variable called "myFile$". Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com //////////////////////////////////////////////////////////////// // Encryption with DarkBasic: (2 of 2) Encrypting With DBP // //////////////////////////////////////////////////////////////// // * 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 // //////////////////////////////////////////////////////////////// `` In the first module, if you read it, you were given some baby-steps so you could understand the `` principles. Encryption can be done in many ways and simply changing byte values is but one. This `` is the most pragmatic method under DBP so we'll be using this but don't forget about other ways `` such as inserting random bytes at non-regular intervals (causes bloating) or rearanging blocks of `` data based on a specific pattern (vulnerable on its own). Combining these methods and your own `` will allow you an endless amount of flexibility, even within DBP. // Globals GLOBAL myFilee$: myFile$ = "C:\Test.bmp" GLOBAL newFile$: newFile$ = myFile$ + ".nonZeroEncrypted." + RIGHT$(myFile$, 3) GLOBAL fnlFile$: fnlFile$ = myFile$ + ".nonZeroDECRYPTED." + RIGHT$(myFile$, 3) GLOBAL myFil: myFil = 1 GLOBAL myBlock: myBlock = 1 GLOBAL myImage: myImage = 999 GLOBAL mbPTR AS DWORD GLOBAL myPTR AS DWORD // Video Setup SET WINDOW ON: SYNC RATE 30 // We're going to use a table for this one. We can use it for encrypting or decrypting (but the values must be the same for both). DIM eTable(360) // We will use index 0 in this case. Now why 360? DBP only supports WRAPVALUE FOR i = 0 TO 360 // for 360 (my function library supports more) and I'm trying to keep this native. eTable(i) = i * 27 // This first table controls the pattern of adding. It's basic NEXT i // Next example uses pointers. If you don't know what a pointer is, check the DBP help. We will // be encrypting a large-size file at a decent speed using a complex algorithm. // We'll begin with an encryption. For that we'll need a file in memory. File_To_Memory: OPEN TO READ myFil, myFile$ // Same as before, we open a file and load MAKE MEMBLOCK FROM FILE myBlock, myFil // into a memblock...` CLOSE FILE myFil mbPTR = GET MEMBLOCK PTR(myBlock) // We'll use this tempSize = GET MEMBLOCK SIZE(myBlock) myPTR = MAKE MEMORY(tempSize+1) // We'll be working directly with memory now COPY MEMORY myPTR, mbPTR, tempSize // Copy the memblock into our memory DELETE MEMBLOCK myBlock // Free memblock, it's not needed mbPTR = 0 // Set pointer to 0, Google NULL-Pointers for more info Encrypt: tempLastIt = tempSize - 1 tempAddr AS DWORD tempByte AS BYTE tempTableIndex AS INTEGER // This will be used in a cunning way FOR i = 0 TO tempLastIt // Btw precalculating this value saves it calculating each iteration tempAddr = myPTR + i // Now think of memory as a giant single-byte array. tempByte = *tempAddr // Grab byte tempTableIndex = WRAPVALUE(i) // Assign a wrapvalue of i to the index variable for the lookup DEC tempByte, eTable(tempTableIndex) // Now modify the byte accordingly FILL MEMORY tempAddr, tempByte, 1 // Write the new byte value. NEXT i MAKE MEMBLOCK myBlock, tempSize // We'll place our memory back in a memblock to make it easier to write to hdd mbPTR = GET MEMBLOCK PTR(myBlock) COPY MEMORY mbPTR, myPTR, tempSize IF FILE EXIST(newFile$): DELETE FILE newFile$: ENDIF OPEN TO WRITE myFil, newFile$ MAKE FILE FROM MEMBLOCK myFil, myBlock CLOSE FILE myFil // Cleanup operations DELETE MEMBLOCK myBlock DELETE MEMORY myPTR myPTR = 0: mbPTR = 0: tempAddr = 0 // Now we'll decrypt the file. We'll be processing it in the same way we did to encrypt it but obviously in reverse. File_To_Memory_Again: OPEN TO READ myFil, newFile$ // Same as before, we open a file and load MAKE MEMBLOCK FROM FILE myBlock, myFil // into a memblock...` CLOSE FILE myFil mbPTR = GET MEMBLOCK PTR(myBlock) // We'll use this tempSize = GET MEMBLOCK SIZE(myBlock) myPTR = MAKE MEMORY(tempSize+1) // We'll be working directly with memory now COPY MEMORY myPTR, mbPTR, tempSize // Copy the memblock into our memory DELETE MEMBLOCK myBlock // Free memblock, it's not needed mbPTR = 0 // Set pointer to 0, Google NULL-Pointers for more info Decrypt: tempLastIt = tempSize - 1 // So similar I copy-pated it and then changed a few things? tempAddr = 0 // Can you spot them? tempByte = 0 tempTableIndex = 0 FOR i = 0 TO tempLastIt tempAddr = myPTR + i tempByte = *tempAddr tempTableIndex = WRAPVALUE(i) INC tempByte, eTable(tempTableIndex) // I'll point this one out as it's important. We're now subtracting, not adding FILL MEMORY tempAddr, tempByte, 1 // Place tempByte at memory adress tempAddr 1 times (2 would fill (tempAddr) and (tempAddr+1)) NEXT i MAKE MEMBLOCK myBlock, tempSize // We'll place our memory back in a memblock to make it easier to write to hdd mbPTR = GET MEMBLOCK PTR(myBlock) COPY MEMORY mbPTR, myPTR, tempSize // Now we're going to use the decrypted image :) IF FILE EXIST(fnlFile$): DELETE FILE fnlFile$: ENDIF // We're gonna save it first coz DBP can't load images from files directly in OPEN TO WRITE myFil, fnlFile$ // memblocks (there is a way though but we'll cover that in my File Containers MAKE FILE FROM MEMBLOCK myFil, myBlock // code tutorial-ish thing). CLOSE FILE myFil WHILE FILE EXIST(fnlFile$) = 0: ENDWHILE // Wait for the file to be available. LOAD IMAGE fnlFile$, myImage // And the rest is straightforward. MAKE OBJECT SPHERE 1, 5 TEXTURE OBJECT 1, myImage i = 0 WHILE SCANCODE() = 0 i = WRAPVALUE(i+1) YROTATE OBJECT 1, i TEXT 1, 20, "Press any key" ENDWHILE DELETE MEMBLOCK myBlock // Cleanup. DBP will do this at program end but it's a good habbit as you'll DELETE MEMORY myPTR // be working with functions and certain things don't die when going out of DELETE IMAGE myImage // scope. Example: Memory you create remains but pointer is destroyed. Images myPTR = 0: mbPTR = 0: tempAddr = 0 // are global as are memblocks. END `` Post notes: You will find the following files in the same directory as the file you specified to `` use for the excercise: `` [filename].nonZeroEncrypted.[file-extension] `` [filename].nonZeroDECRYPTED.[file-extension] `` You can examine them with a hex editor if you like and compare the DECRYPTED to the original. I used the following comparison `` on a BMP image run through this test using hashes: `` Algorithm Check `` MD5 Same `` SHA-1 [160] Same `` CRC32 Same `` 11 SizeHash-32 Same |