encrypting sound files in pure basic, can it be done?

Advanced game related topics
Omar1993
New User
New User
Posts: 7
Joined: Sat Dec 31, 2016 11:18 am

encrypting sound files in pure basic, can it be done?

Post by Omar1993 »

Hi everyone,
I am an audio game developer for the blind. I am myself blind, but that's not really what I've come to talk about today.
Feel free to pm me or email me if you want more info about that, I will gladly take any questions you may have.
So anyway, audio games rely purely on sound alone. And I'm interested in making one in pure basic.
I've got basic knowledge of the language though one thing that I can't seem to get information on is how one would secure files in general.
These files include sound files, and I would like to encrypt them so people have a harder time trying to rip them out the game, preferably with a encryption passphrase.
I would also if at all possible like to play them back as my game called them, and decrypt them on the fly.
By the way, yes, these are of course already recorded or created sounds most likely in ogg format.
Is it possible to encrypt sound files or even files in general at all in pure basic?
Thanks everyone! :D
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: encrypting sound files in pure basic, can it be done?

Post by Keya »

You certainly can encrypt sound or any other files, very easily too, but you'll forever be up against the insurmountable analog loophole allowing anyone to simply press Record when your decrypted audio is playing, but anyway encryption obviously still adds that extra layer of work that they need to do, and it doesn't take much work for the programmer, so maybe it's worth it, i don't know :)
Purebasic makes encryption very easy, especially as it has native support for the Advanced Encryption Standard (which always makes customers feel better compared with "we roll our own encryption, it looks pretty good under a hex editor!"), just search your helpfile for AESEncoder to find an example, and there's plenty more examples here in the forum, again AESEncoder is probably a good keyword to use to find them.
And congratulations on choosing Purebasic, 2017 is going to be awesome for you! The next month is going to suck - but that's the case with any new language, but after that the freedom and simplicity and power will blow your freaking mind :D
Omar1993
New User
New User
Posts: 7
Joined: Sat Dec 31, 2016 11:18 am

Re: encrypting sound files in pure basic, can it be done?

Post by Omar1993 »

Keya wrote:You certainly can encrypt sound or any other files, very easily too, but you'll forever be up against the insurmountable analog loophole allowing anyone to simply press Record when your decrypted audio is playing, but anyway encryption obviously still adds that extra layer of work that they need to do, and it doesn't take much work for the programmer, so maybe it's worth it, i don't know :)
Purebasic makes encryption very easy, especially as it has native support for the Advanced Encryption Standard (which always makes customers feel better compared with "we roll our own encryption, it looks pretty good under a hex editor!"), just search your helpfile for AESEncoder to find an example, and there's plenty more examples here in the forum, again AESEncoder is probably a good keyword to use to find them.
And congratulations on choosing Purebasic, 2017 is going to be awesome for you! The next month is going to suck - but that's the case with any new language, but after that the freedom and simplicity and power will blow your freaking mind :D
Pure basic is already blowing my freaking mind xd. I come from an object oriented scripting language designed from the blind for the blind, but for me it wasn't cutting it.
I'll be sure to search the various help resources, thanks keya!
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: encrypting sound files in pure basic, can it be done?

Post by Keya »

Here's a simple but full example of AES file encrypt and decrypt to kick off your New Years in secure style

Code: Select all

sFile.s = ProgramFilename()  ;file to encrypt, for this demo ill just use our own executable

;// Read file to encrypt
hFile = ReadFile(#PB_Any, sFile)
If hFile = 0
  MessageRequester("Error", "Couldnt open file"): End
EndIf
lenbuf = Lof(hFile)
*original = AllocateMemory(lenbuf)
ReadData(hFile, *original, lenbuf)
CloseFile(hFile)

;// Encrypt data (from *original to *encrypted buffer)
*encrypted = AllocateMemory(lenbuf)
AESEncoder(*original, *encrypted, lenbuf, ?cipherkey, 128, ?IV, #PB_Cipher_CBC)

;// Save encrypted data
hFile = CreateFile(#PB_Any, sFile+".encrypted")
WriteData(hFile, *encrypted, lenbuf)
CloseFile(hFile)

;// Decrypt encrypted data (from *encrypted to *decrypted buffer)
*decrypted = AllocateMemory(lenbuf)
AESDecoder(*encrypted, *decrypted, lenbuf, ?cipherkey, 128, ?IV, #PB_Cipher_CBC)

;// Save decrypted data
hFile = CreateFile(#PB_Any, sFile+".decrypted")
WriteData(hFile, *decrypted, lenbuf)
CloseFile(hFile)

;// Verify that decrypted data matches original
UseCRC32Fingerprint()
If Fingerprint(*decrypted, lenbuf, #PB_Cipher_CRC32) = Fingerprint(*original, lenbuf, #PB_Cipher_CRC32)
  Debug "Decrypt matches original"
Else
  Debug "Error, decrypt doesnt match original"
EndIf

;// Free resources
FreeMemory(*original)
FreeMemory(*decrypted)
FreeMemory(*encrypted)

           
DataSection  ;forgive my weak keys, you should use something like PB's CryptRandomData() to easily & securely generate these
  cipherkey:
  Data.a 23,53,1,5,89,32,49,39,50,20,50,1,4,5,2,5  ;16 bytes for 128-bit AES (use 32 for 256)
  IV:
  Data.a 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0           ;IV is always 16 bytes regardless of 128/192/256
EndDataSection
Last edited by Keya on Sat Dec 31, 2016 12:20 pm, edited 2 times in total.
Omar1993
New User
New User
Posts: 7
Joined: Sat Dec 31, 2016 11:18 am

Re: encrypting sound files in pure basic, can it be done?

Post by Omar1993 »

Holycrap. Thanks so much for your help to start off my new year! I'll be sure to look around what all of that does, since as of now you just completely blew my mind. :)
Thanks so much!
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: encrypting sound files in pure basic, can it be done?

Post by Keya »

you're welcome. btw grab another copy of that as i had to make a quick modification, sorry for that. btw that compiles to only a 28kb completely-standalone exe in Win32 (make sure you turn Debugger off before compiling a 'Release mode' build), I have a feeling that's a lot less bloat than whatever object-oriented language you're coming from :) And forgive my lousy pun but you ain't seen nothing yet :D have a great NYE
Omar1993
New User
New User
Posts: 7
Joined: Sat Dec 31, 2016 11:18 am

Re: encrypting sound files in pure basic, can it be done?

Post by Omar1993 »

I will do of course. And I forgive you for your pun xd. Throw all the vision jokes you can throw at me, I've heard em all. Hahaha.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: encrypting sound files in pure basic, can it be done?

Post by Keya »

Omar1993 wrote:Throw all the vision jokes you can throw at me, I've heard em all. Hahaha.
oh ok, great! in that case haha ... so a blind programmer named Omar walks into a bar ... the bartender says "so you can't see?" the programmer says "no I don't like any object-oriented language" *cringe* :D ok Fred i think i should be suspended for 24hours lol
Omar1993
New User
New User
Posts: 7
Joined: Sat Dec 31, 2016 11:18 am

Re: encrypting sound files in pure basic, can it be done?

Post by Omar1993 »

Omg! Ahahaha! That's amusing as heck rofl. :) :) LOL extremely amused this very moment.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: encrypting sound files in pure basic, can it be done?

Post by Keya »

it wouldve been better if C actually was an object-oriented language but i didnt have time to workshop it haha, thankyou everyone for turning a bli..... uh thanks to everyone for not mentioning that! actually Fred can we make my suspension 48hrs instead of 24 lol

btw Omar when youve encrypted a file you can easily embed them directly in your executable, which i guess adds yet another step to make it harder for people to get at as opposed to having them as external files (trickier for them to work out where in your executable the audio file starts and ends):

Code: Select all

MessageRequester("Demo", "The first 2 bytes of the embedded file are: " + PeekS(?MyFile, 2, #PB_Ascii)) ;"MZ" for exes

DataSection
  MyFile:
  IncludeBinary("c:\windows\notepad.exe")  ;ie. your encrypted audio file
EndDataSection
And you're probably aware that PB makes it ridiculously easy to play audio files with LoadSound() and PlaySound(), but you might not yet be aware that PB also has a very nice set of Catch____() API's that are basically like the Load___() ones but work from a memory buffer instead of a file buffer ... this means that you can embed your encrypted audio file directly in your executable with IncludeBinary(), then decrypt it in memory using AESDecoder(), then play the decrypted one from memory using CatchSound(), (and then re-encrypt with AESEncoder() or simply overwrite with nulls if you want to re-protect it) ... all of this without ever writing or otherwise using a file, just the memory buffer of your program, so your audio file is only ever decrypted in memory and never on disk :)

Also im guessing you'll want your program to continue (respond to GUI events etc) while the audio is playing, to do this you'll of course need to play the audio in a separate thread (create three threads if you want three audios playing simultaneously etc), but again you're in luck because it doesn't get any easier than in PB:

Code: Select all

Procedure MyPlayAudioThread(threadparam)
  PlaySound(threadparam)  ;threadparam = hSound
EndProcedure
 
InitSound()  ;<-- must call once first to open access to PB's ___Sound() api's
hSound = LoadSound(....)  ;(or CatchSound())
CreateThread(@MyPlayAudioThread(), hSound)
;main thread immediately continues...
When using threads its generally recommended to select "Create threadsafe executable" in Compiler -> Options
Post Reply