Code:
##########################################################################
# Script by: BEHEMOTH # #
####################### #
#Version: 1.0 #
#Date Created: October 26th 2006 8:42 PM (Eastern Standard Time) #
#Description: An encryption script for audio files. #
#Thanks goes to thephantom from http://www.phanxgames.com/ for pointing out #
#the whole encryption key idea which I was too blind to see. #
#############################################################################
#############################################################################
# ***IMPORTANT RESTRICTIONS*** #
#1) If the audio file is an .mp3 file, it will NOT encrypt the file and it #
#will play it normally. If you wish to have your mp3 file encrypted please #
#save it as another compatible audio file, such as .ogg, .mid #
#or .wav(compressed of course) or .wma #
# #
#2) Your audio files must NOT be: read only, or hidden, or protected in #
#anyway or it will not encrypt the file and it will play it normally. #
# #
#3) Audio files should not have the same filename with a different #
# extension. #
# BAD: "battle01.mid" "battle01.ogg" #
# GOOD: "battle01.mid" "battle02.ogg" #
# #
#4) Files from an RMXP RTP will not be encrypted and will play normally. #
#############################################################################
#############################################################################
# ***NOTES*** #
#Positives: #
# - Protects your own custom audio files #
# - Gives a point to having a BGM test inside your game #
# - Ummmm.......it's cool #
# #
#Negatives: #
# - Audio files take slightly longer to load #
# - mp3 files do not encrypt #
# - Once encrypted, audio files will not play in rmxp gui, only in the game #
# - The audio file is slightly bigger in file size then the orginal file #
# #
#If you have problems, suggestions, comments about this script then you can #
#currently find me here: http://www.phanxgames.com/forum/ #
# #
#How to Use: #
#To encrypt your audio files play the file in your game at least once. #
#The script will handle the rest. MAKE SURE TO BACK UP YOUR ORIGINAL #
#AUDIO FILES BEFORE ENCRYPTING IT, because it automatically overwrites #
#the original file with the encrypted file. To make this script encrypt #
#only certain audio files such as BGM, BGS, ME, SE then change the values #
#of the variables below named: #
#ENCRYPT_BGM, ENCRYPT_BGS, ENCRYPT_ME, ENCRYPT_SE #
# (located below class Game_System) #
#changing the value to true means those files will be encrypted and false #
#means those files will not be encrypted. Changing these values when an #
#audio file is encrypted may cause undesired results. So choose what you #
#want first then start encrypting your audio files. Deleting the Encryption #
#key file (default = "audio.edat") after encrypting your audio will also #
#result in undesired results....so don't delete or move that file once #
#you've been encrypting or else you'll have to start encrypting all your #
#audio again. This script is more for people who create their own audio #
#files. #
#############################################################################
class Game_System
ENCRYPT_EXT = ".eaudio" #Encrypted file extenstion
ENCRYPT_FILE = "audio.edat" #File containing encrypted data
ENCRYPT_KEY_LENGTH = 16 #Number of characters for encryption key
#Change encryption for certain audio files.......HERE!!!!!
ENCRYPT_BGM = true #Encrypt bgm?
ENCRYPT_BGS = false #Encrypt bgs?
ENCRYPT_ME = false #Encrypt me? Yes, please! lol
ENCRYPT_SE = false #Encrypt se?
#--------------------------------------------------------------------------
# * Play Background Music
# bgm : background music to be played
#--------------------------------------------------------------------------
def bgm_play(bgm)
@playing_bgm = bgm
if bgm != nil and bgm.name != ""
filename = "Audio/BGM/" + bgm.name
if ENCRYPT_BGM
decrypt_file(filename)
Audio.bgm_play(filename, bgm.volume, bgm.pitch)
encrypt_file(filename)
else
Audio.bgm_play(filename, bgm.volume, bgm.pitch)
end
else
Audio.bgm_stop
end
Graphics.frame_reset
end
#--------------------------------------------------------------------------
# * Play Background Sound
# bgs : background sound to be played
#--------------------------------------------------------------------------
def bgs_play(bgs)
@playing_bgs = bgs
if bgs != nil and bgs.name != ""
filename = "Audio/BGS/" + bgs.name
if ENCRYPT_BGS
decrypt_file(filename)
Audio.bgs_play(filename, bgs.volume, bgs.pitch)
encrypt_file(filename)
else
Audio.bgs_play(filename, bgs.volume, bgs.pitch)
end
else
Audio.bgs_stop
end
Graphics.frame_reset
end
#--------------------------------------------------------------------------
# * Play Music Effect
# me : music effect to be played
#--------------------------------------------------------------------------
def me_play(me)
if me != nil and me.name != ""
filename = "Audio/ME/" + me.name
if ENCRYPT_ME
decrypt_file(filename)
Audio.me_play(filename, me.volume, me.pitch)
encrypt_file(filename)
else
Audio.me_play(filename, me.volume, me.pitch)
end
else
Audio.me_stop
end
Graphics.frame_reset
end
#--------------------------------------------------------------------------
# * Play Sound Effect
# se : sound effect to be played
#--------------------------------------------------------------------------
def se_play(se)
if se != nil and se.name != ""
filename = "Audio/SE/" + se.name
if ENCRYPT_SE
decrypt_file(filename)
Audio.se_play(filename, se.volume, se.pitch)
encrypt_file(filename)
else
Audio.se_play(filename, se.volume, se.pitch)
end
end
end
#--------------------------------------------------------------------------
# Finds the extension of an audio filename with no extension
#--------------------------------------------------------------------------
def get_file_ext(filename)
if FileTest.exist?(filename + ".wav")
return ".wav"
elsif FileTest.exist?(filename + ".mp3")
return ".mp3"
elsif FileTest.exist?(filename + ".ogg")
return ".ogg"
elsif FileTest.exist?(filename + ".mid")
return ".mid"
elsif FileTest.exist?(filename + ".wma")
return ".wma"
else
return ENCRYPT_EXT
end
end
#--------------------------------------------------------------------------
# Encrypts an audio file and saves it with the encrypted extension and
# deletes the non encrypted file if it was not already encrypted.
#--------------------------------------------------------------------------
def encrypt_file(filename)
ext = get_file_ext(filename)
filen = filename + ext
# If file doesn't not exist in project/audio folder then exit.
# File either doesn't exist or is part of the RTP.
if (not FileTest.exist?(filen))
return
end
# Load File
afile = File.open(filen, "rb")
t = afile.readlines
afile.close
#If filename was not previously encrypted
if ext != ENCRYPT_EXT
begin
# Test if file is writeable
afile = File.open(filen, "wb")
afile.close
# Delete File
File.delete(filen)
rescue
#File could not be encrypted
return
end
end
get_encryption
# Save File
filen = filename + ENCRYPT_EXT
afile = File.open(filen, "wb")
for i in 0...t.size
s = @encrypt_data + t[i]
t[i] = s
end
Marshal.dump(t, afile)
afile.close
end
#--------------------------------------------------------------------------
# Decrypts a file if it is encrypted.
#--------------------------------------------------------------------------
def decrypt_file(filename)
filename += ENCRYPT_EXT
#If file is not encrypted
if (not FileTest.exist?(filename))
return
end
get_encryption
# Load File
afile = File.open(filename, "rb")
t = Marshal.load(afile)
for i in 0...t.size
s = t[i]
t[i] = s[ENCRYPT_KEY_LENGTH, t[i].size]
end
afile.close
# Save File
afile = File.open(filename, "wb")
for i in 0...t.size
afile.write(t[i])
end
afile.close
end
#--------------------------------------------------------------------------
# * Retrieve's encryption data from file. If file doesn't exist then it
# creates an encryption data file.
#--------------------------------------------------------------------------
def get_encryption
# No encryption data(method) found
if @encrypt_data == nil
# Encryption data file exists?
if FileTest.exist?(ENCRYPT_FILE)
# Load Data File
afile = File.open(ENCRYPT_FILE, "rb")
@encrypt_data = Marshal.load(afile)
afile.close
else
# Create encryption data file
afile = File.open(ENCRYPT_FILE, "wb")
Marshal.dump(create_key, afile)
afile.close
end
end
end
#--------------------------------------------------------------------------
# * Returns ENCRYPT_KEY_LENGTH character randomized string used to encrypt
# the audio file
#--------------------------------------------------------------------------
def create_key
key = ""
for i in 0...ENCRYPT_KEY_LENGTH
key_num = rand(36)
key_char = key_num.to_s(36)
upper_case = rand(2) == 1
if key_num > 9 && upper_case
key_char.upcase!
end
key += key_char
end
return key
end
#--------------------------------------------------------------------------
end