Modified Audio Encryption Script
#1
Modified Audio Encryption Script
by avatarmonkeykirby
Jul 28 2008

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.


This is a modification of Behemoth's Audio Encryption as well as a merge
with SephirothSpawn's error logging script.

Credit goes to the two of them, I just made some modifications and merged so enjoy.
Code:
#==============================================================================
# ** AMK's Mod-ed Audio Encryption
#==============================================================================
# Original Scripts by Behemoth & SephirothSpawn
#
# Problem: When using Behemoth's original script it would only work when you accessed audio through $game_system.audiocommand
# keeping you from using the Audio module to actually play audio.
#
# Remedy: Instead of aliasing the Game_System class I aliased the Audio module. I found the best way to do that was to merge
# SephirothSpawn's error reporting script with Behemoth's.
#
# Notes: The original instructions for Behemoth's and Sephiroth's scripts are both below. I've also added
# the method Audio.run_all to run and encrypt all unencrypted audio files, it only runs when you are in debug mode
#==============================================================================

#############################################################################
# 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.                                                                     #
#############################################################################

#==============================================================================
# ** File Error Fix & Log
#------------------------------------------------------------------------------
# SephirothSpawn
# Version 1
# 2006-10-08
#------------------------------------------------------------------------------
# * Description
#
#   This script was designed to prevent the game from exiting when a file is
#   not found from the audio and cache modules. It creates a log file with
#   date and time the error occurs, with the file name, and what module /
#   method was looking for the file.
#------------------------------------------------------------------------------
# * Instructions
#
#   Place The Script Below the SDK and Above Main.
#==============================================================================

#------------------------------------------------------------------------------
# * SDK Log Script
#------------------------------------------------------------------------------
SDK.log('Audio Encryption', 'BEHEMOTH and SephirothSpawn; Merged and Mod-ed by AMK', 2.0, '7/20/2008')
#SDK.disable('Audio Encryption')
#------------------------------------------------------------------------------
# * Begin SDK Enable Test
#------------------------------------------------------------------------------
if SDK.state('Audio Encryption')

#==============================================================================
# ** Audio
#==============================================================================

module Audio
  class << self
    ENCRYPT_EXT        = ".bin"     #Encrypted file extenstion
    ENCRYPT_FILE       = "\Audio\\eAudio.bin"  #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        = true         #Encrypt bgs?
    ENCRYPT_ME         = true         #Encrypt me? Yes, please! lol
    ENCRYPT_SE         = true         #Encrypt se?
    #If you don't want the original audio files to be deleted, after encryption
    #change to false when you release your final version
    DELETE_ORIGINAL    = false
    #--------------------------------------------------------------------------
    # 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"
      elsif FileTest.exist?(filename + ENCRYPT_EXT)
        return ENCRYPT_EXT
      else
        return ""
      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
          if DELETE_ORIGINAL
            # Test if file is writeable
            afile = File.open(filen, "wb")
            afile.close
            # Delete File
            File.delete(filen)#Deletes the original file
          end
        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
    #------------------------------------------------------------------------
    # * Alias Listings
    #------------------------------------------------------------------------
    if @seph_fileerrorfix.nil?
      alias seph_fileerrorfix_audio_bgmplay bgm_play
      alias seph_fileerrorfix_audio_bgsplay bgs_play
      alias seph_fileerrorfix_audio_meplay  me_play
      alias seph_fileerrorfix_audio_seplay  se_play
      @seph_fileerrorfix = true
    end
    #------------------------------------------------------------------------
    # * BGM Play
    #------------------------------------------------------------------------
    def bgm_play(filename, volume = 100, pitch = 100)
      if ENCRYPT_BGM
        decrypt_file(filename) rescue write_error('BGM [decrypt]', filename)
        seph_fileerrorfix_audio_bgmplay(filename, volume, pitch) rescue write_error('BGM [decrypted play]', filename)
        encrypt_file(filename) rescue write_error('BGM [encrypt]', filename)
      else
        seph_fileerrorfix_audio_bgmplay(filename, volume, pitch) rescue write_error('BGM [regular play]', filename)
      end
    end
    #------------------------------------------------------------------------
    # * BGS Play
    #------------------------------------------------------------------------
    def bgs_play(filename, volume = 100, pitch = 100)
      if ENCRYPT_BGS
        decrypt_file(filename) rescue write_error('BGS [decrypt]', filename)
        seph_fileerrorfix_audio_bgsplay(filename, volume, pitch) rescue write_error('BGS [decrypted play]', filename)
        encrypt_file(filename) rescue write_error('BGS [encrypt]', filename)
      else
        seph_fileerrorfix_audio_bgsplay(filename, volume, pitch) rescue write_error('BGS [regular play]', filename)
      end
    end
    #------------------------------------------------------------------------
    # * ME Play
    #------------------------------------------------------------------------
    def me_play(filename, volume = 100, pitch = 100)
      if ENCRYPT_ME
        decrypt_file(filename) rescue write_error('ME [decrypt]', filename)
        seph_fileerrorfix_audio_meplay(filename, volume, pitch) rescue write_error('ME [decrypted play]', filename)
        encrypt_file(filename) rescue write_error('ME [encrypt]', filename)
      else
        seph_fileerrorfix_audio_meplay(filename, volume, pitch) rescue write_error('ME [regular play]', filename)
      end
    end
    #------------------------------------------------------------------------
    # * SE Play
    #------------------------------------------------------------------------
    def se_play(filename, volume = 100, pitch = 100)
      if ENCRYPT_SE
        decrypt_file(filename) rescue write_error('SE [decrypt]', filename)
        seph_fileerrorfix_audio_seplay(filename, volume, pitch) rescue write_error('SE [decrypted play]', filename)
        encrypt_file(filename) rescue write_error('SE [encrypt]', filename)
      else
        seph_fileerrorfix_audio_seplay(filename, volume, pitch) rescue write_error('SE [regular play]', filename)
      end
    end
    #------------------------------------------------------------------------
    # * Play all BGM
    #------------------------------------------------------------------------
    def play_all_bgm
      return if !$DEBUG
      if ENCRYPT_BGM
        @audiofiles = Dir.entries("Audio/BGM/")
        for i in 0..(@audiofiles.length-1)
          next if @audiofiles[i] == '.'
          next if @audiofiles[i] == '..'
          next if @audiofiles[i].include?(ENCRYPT_EXT)
          @encodefile="#{@audiofiles[i]}"
          @encodefile.gsub!(/.[Ww][Aa][Vv]/) { "" }
          @encodefile.gsub!(/.[Mm][Pp]3/) { "" }
          @encodefile.gsub!(/.[Ww][Mm][Aa]/) { "" }
          @encodefile.gsub!(/.[Mm][Ii][Dd]/) { "" }
          @encodefile.gsub!(/.[Ww][Mm][Aa]/) { "" }
          @encodefile.gsub!(/.[Oo][Gg][Gg]/) { "" }
          next if File.exist?("Audio/BGM/" + @encodefile + ENCRYPT_EXT)
          bgm_play("Audio/BGM/" + @encodefile)
          Graphics.transition(1)
          bgm_stop
        end
      end
      @encodefile = @audiofiles = nil
    end
    #------------------------------------------------------------------------
    # * Play all BGS
    #------------------------------------------------------------------------
    def play_all_bgs
      return if !$DEBUG
      if ENCRYPT_BGS
        @audiofiles = Dir.entries("Audio/BGS/")
        for i in 0..(@audiofiles.length-1)
          next if @audiofiles[i] == '.'
          next if @audiofiles[i] == '..'
          next if @audiofiles[i].include?(ENCRYPT_EXT)
          @encodefile="#{@audiofiles[i]}"
          @encodefile.gsub!(/.[Ww][Aa][Vv]/) { "" }
          @encodefile.gsub!(/.[Mm][Pp]3/) { "" }
          @encodefile.gsub!(/.[Ww][Mm][Aa]/) { "" }
          @encodefile.gsub!(/.[Mm][Ii][Dd]/) { "" }
          @encodefile.gsub!(/.[Ww][Mm][Aa]/) { "" }
          @encodefile.gsub!(/.[Oo][Gg][Gg]/) { "" }
          next if File.exist?("Audio/BGS/" + @encodefile + ENCRYPT_EXT)
          bgs_play("Audio/BGS/" + @encodefile)
          Graphics.transition(1)
          bgs_stop
        end
      end
      @encodefile = @audiofiles = nil
    end
    #------------------------------------------------------------------------
    # * Play all ME
    #------------------------------------------------------------------------
    def play_all_me
      return if !$DEBUG
      if ENCRYPT_ME
        @audiofiles = Dir.entries("Audio/ME/")
        for i in 0..(@audiofiles.length-1)
          next if @audiofiles[i] == '.'
          next if @audiofiles[i] == '..'
          next if @audiofiles[i].include?(ENCRYPT_EXT)
          @encodefile="#{@audiofiles[i]}"
          @encodefile.gsub!(/.[Ww][Aa][Vv]/) { "" }
          @encodefile.gsub!(/.[Mm][Pp]3/) { "" }
          @encodefile.gsub!(/.[Ww][Mm][Aa]/) { "" }
          @encodefile.gsub!(/.[Mm][Ii][Dd]/) { "" }
          @encodefile.gsub!(/.[Ww][Mm][Aa]/) { "" }
          @encodefile.gsub!(/.[Oo][Gg][Gg]/) { "" }
          next if File.exist?("Audio/ME/" + @encodefile + ENCRYPT_EXT)
          me_play("Audio/ME/" + @encodefile)
          Graphics.transition(1)
          me_stop
        end
      end
      @encodefile = @audiofiles = nil
    end
    #------------------------------------------------------------------------
    # * Play all SE
    #------------------------------------------------------------------------
    def play_all_se
      return if !$DEBUG
      if ENCRYPT_SE
        @audiofiles = Dir.entries("Audio/SE/")
        for i in 0..(@audiofiles.length-1)
          next if @audiofiles[i] == '.'
          next if @audiofiles[i] == '..'
          next if @audiofiles[i].include?(ENCRYPT_EXT)
          @encodefile="#{@audiofiles[i]}"
          @encodefile.gsub!(/.[Ww][Aa][Vv]/) { "" }
          @encodefile.gsub!(/.[Mm][Pp]3/) { "" }
          @encodefile.gsub!(/.[Ww][Mm][Aa]/) { "" }
          @encodefile.gsub!(/.[Mm][Ii][Dd]/) { "" }
          @encodefile.gsub!(/.[Ww][Mm][Aa]/) { "" }
          @encodefile.gsub!(/.[Oo][Gg][Gg]/) { "" }
          next if File.exist?("Audio/SE/" + @encodefile + ENCRYPT_EXT)
          se_play("Audio/SE/" + @encodefile)
          Graphics.transition(1)
          se_stop
        end
      end
      @encodefile = @audiofiles = nil
    end
    #------------------------------------------------------------------------
    # * Play all Audio
    #------------------------------------------------------------------------    
    def run_all
      play_all_bgm
      play_all_bgs
      play_all_me
      play_all_se
    end
    #------------------------------------------------------------------------
    # * Write Error
    #------------------------------------------------------------------------
    def write_error(type, filename)
      if $DEBUG
        File.open('File Error Report.txt',"a+") do |file|
          file.puts("#{Time.new} - Unable to find file #{type} : #{filename}\n")
        end
      end
    end
  end
end

#==============================================================================
# ** RPG::Cache
#==============================================================================
module RPG::Cache
  class << self
    #------------------------------------------------------------------------
    # * Alias Listings
    #------------------------------------------------------------------------
    if @seph_fileerrorfix.nil?
      alias seph_filereport_rpgc_loadbitmap load_bitmap
      @seph_fileerrorfix = true
    end
    #------------------------------------------------------------------------
    # * Load Bitmap
    #------------------------------------------------------------------------
    def load_bitmap(folder_name, filename, hue = 0)
      seph_filereport_rpgc_loadbitmap(folder_name, filename, hue) rescue write_error('Bitmap', "#{folder_name}#{filename}")
    end
    #------------------------------------------------------------------------
    # * Write Error
    #------------------------------------------------------------------------
    def write_error(type, filename)
      Bitmap.new(128, 128)
      # Opens File (Or Creates One)
      if $DEBUG
        File.open('File Error Report.txt',"a+") do |file|
          # Writes Error to End of File
          file.puts("#{Time.new} - Unable to find file #{type} : #{filename}.\n")
        end
      end
    end
  end
end
#--------------------------------------------------------------------------
# * End SDK Enable Test
#--------------------------------------------------------------------------
end
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rotimikid's Enemy level Script ShockWave 0 2,825 08-24-2007, 01:00 PM
Last Post: ShockWave
  Script switch management GubiD 0 2,515 08-19-2007, 01:00 PM
Last Post: GubiD
  Nice game over script Moghunter 0 2,595 07-18-2007, 01:00 PM
Last Post: Moghunter
  Animated Options Script Samo the thief 0 2,580 06-18-2007, 01:00 PM
Last Post: Samo the thief
  Teleportation Script Digi 0 2,541 05-01-2007, 01:00 PM
Last Post: Digi
  Luck Script Darklord3652 0 2,551 04-10-2007, 01:00 PM
Last Post: Darklord3652
  GameOver Menu Script sasuke89 0 2,486 06-08-2006, 01:00 PM
Last Post: sasuke89
  Weapon Levelling Script GoldenShadow 0 2,482 07-05-2005, 01:00 PM
Last Post: GoldenShadow
  Ammo Script Dubealex 0 2,496 05-27-2005, 01:00 PM
Last Post: Dubealex
  Item Detail Script jackatrades 0 2,645 05-19-2005, 01:00 PM
Last Post: jackatrades



Users browsing this thread: 1 Guest(s)