2 Replies Latest reply on Jun 19, 2008 2:14 AM by JasonFL1

    Database issue

    JasonFL1
      I am trying to be a bit clever and decrypt/encrypt my sqlite database when the program opens/closes.

      The code is below, here is the error I get:

      SecurityError: fileWriteResource
      at runtime::SecurityManager$/checkPrivilegeForCaller()
      at flash.filesystem::FileStream/open()

      Can anyone spot anything wrong? I also get an SQLError "file opened is not a database file" any ideas?


      var db = new air.SQLConnection();
      var stmt = null;
      var databaseFile = air.File.applicationDirectory.resolvePath( 'crm.db' );
      var myFileStream = new air.FileStream();

      var NONE = -1;
      var CREATE_TABLE = 0;
      var INSERT_CONTACT = 1;
      var SELECT_CONTACTS = 2;

      var state = NONE;

      function decryptDatabase(event) {
      str = myFileStream.readMultiByte(myFileStream.bytesAvailable, "iso-8859-1");
      if (str !== "") {
      str = base64_decode(TEAdecrypt(str));
      myFileStream.open(databaseFile, air.FileMode.WRITE);
      myFileStream.writeMultiByte(str, "iso-8859-1");
      }
      myFileStream.close();
      }

      function openDatabase() {
      if (databaseFile.exists) {
      myFileStream.addEventListener(air.Event.COMPLETE, decryptDatabase);
      myFileStream.openAsync(databaseFile, air.FileMode.READ);
      }
      db.addEventListener( air.SQLEvent.OPEN, doDbOpen );
      db.open(databaseFile, air.SQLMode.CREATE );
      }

      function doDbOpen( event )
      {
      stmt = new air.SQLStatement();

      stmt.addEventListener( air.SQLErrorEvent.ERROR, doStmtError );
      stmt.addEventListener( air.SQLEvent.RESULT, doStmtResult );

      stmt.sqlConnection = db;
      stmt.text = 'CREATE TABLE IF NOT EXISTS contact ( ' +
      'id INTEGER PRIMARY KEY AUTOINCREMENT, ' +
      'name TEXT )';

      state = CREATE_TABLE;
      stmt.execute();
      }

      function doStmtResult( event )
      {
      var elem = null;
      var result = null;

      switch( state )
      {
      case CREATE_TABLE:
      break;
      default:
      state = NONE;
      }
      }

      function doStmtError( event )
      {
      alert( 'Problem executing statement: ' + event.error.message );
      }


      openDatabase();
        • 1. Re: Database issue
          Oliver Goldman Adobe Employee
          The error occurs because you are trying to write to a file inside File.applicationDirectory, but that's a read-only location. Choose another location, such as File.applicationStorageDirectory.
          • 2. Re: Database issue
            JasonFL1 Level 1
            Thanks for that Oliver! My next issue is that it is not decrypting properly, I am using the TEA encryption algorithm and when you open the program it decrypts the .db file and then base64 decodes it (to encrypt it first base64 encodes the sqlite database and then encrypts that base64). I think that it is an issue with binary (TEA encrypts into those weird ascii characters and so is the format of sqlite non-encrypted) is this true? Do I need to open the file as a byte array and write it as bytes or something (and how the hell do I do that lol)? Thanks :-)