corbo950
02-22-2009, 05:07 PM
I'm trying to put a "Set Up" in an AIR application of mine so what I'm having it do is check for the Activation code which should be saved in the Application Directory if the set up has already be run. My class is supposed to check for the key and if it finds it get the users application information from a database I host. If the key is not there then it is supposed 2 run the set up which among other things asks for and saves the key into Application Directory. The Key is AES encrypted using an AES javascript. The rest of my application works fine but the key part keeps giving me errors what am i doing wrong?
The Key Class looks like this:
package com.Key
{
import com.adobe.crypto.*;
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.filesystem.*;
import flash.html.HTMLLoader;
import flash.net.URLRequest;
import mx.core.Application;
import mx.managers.CursorManager;
public class Activate
{
private var password:String = "";//removed for security resons
private var loadedFunction:Function;
private var html:HTMLLoader = new HTMLLoader();
public function loadEncryption(doneFunction:Function):void {
loadedFunction = doneFunction;
CursorManager.setBusyCursor();
var JSlocation:String = mx.core.Application.application.PHPurl + "?method=AES";
var urlReq:URLRequest = new URLRequest(JSlocation);
html.load(urlReq);
html.addEventListener(Event.COMPLETE, loaded);
}
private function loaded(event:Event):void {
loadedFunction();
CursorManager.removeBusyCursor();
}
public function checkKey():Boolean {
var activationFile:File = File.applicationDirectory.resolvePath("Team.xml");
var fileStream:FileStream = new FileStream();
fileStream.open(activationFile, FileMode.WRITE);
var fileData:String = fileStream.readUTFBytes(fileStream.bytesAvailable) ;
if(fileData == null)
{
return false;
}
else
{
return true
}
fileStream.close();
}
public function getKey():String {
var activationFile:File = File.applicationDirectory.resolvePath("Team.xml");
var fileStream:FileStream = new FileStream();
fileStream.open(activationFile, FileMode.READ);
var fileData:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvaila ble));
var key:String = AESdecrypt(fileData.key);
var keyMD5:String = hashMD5(key);
var keySHA:String = hashSHA(key);
if(keyMD5 == fileData.security_code_1 && keySHA == fileData.security_code_2)
{
return(key);
}
else
{
return ("Could not verify the key");
}
}
public function insertKey(key:String):void {
var xml:String = '<?xml version="1.0" encoding="utf-8"?><activation><key>' + AESencrypt(key) + "</key><security_code_1>" + hashMD5(key) + "</security_code_1><security_code_2>" + hashMD5(key) + "</security_code_2></activation>";
}
private function AESencrypt(inputString:String):String {
var outputString:String = html.window.AESEncryptCtr(inputString, password, 256);
return outputString;
}
private function AESdecrypt(inputString:String):String {
var outputString:String = html.window.AESDecryptCtr(inputString, password, 256);
return outputString;
}
private function hashSHA(hashString:String):String {
var hashedString:String = SHA256.hash(hashString);
return hashedString;
}
private function hashMD5(hashString:String):String {
var hashedString:String = MD5.hash(hashString);
return hashedString;
}
}
}
Im running it in my application like this:
private function checkKey():void {
if(key.checkKey() == true)
{
if(key.getKey() != "Could not verify the key")
{
dataService.sendEncryptedData({"method": "getUUID", "key": String(key.getKey())}, PHPurl, recieveUUID);
}
else
{
Alert.show("Fatal Error: 1\nThe application must now close", "Error", 4, null, mx.core.Application.application.close);
}
}
else
{
appViews.selectedIndex = 3;
}
}
private function recieveUUID(response:Object):void {
uuid = String(response);
if(uuid == "UUID not found")
{
Alert.show("Error: Could not find team", "Error");
}
else if(uuid == "Team Unactive")
{
Alert.show("This team has been deactivated", "Deactivated");
}
else if(uuid == "")
{
Alert.show("Error selecting team", "Error");
}
else
{
appViews.selectedIndex = 1;
}
}
The Key Class looks like this:
package com.Key
{
import com.adobe.crypto.*;
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.filesystem.*;
import flash.html.HTMLLoader;
import flash.net.URLRequest;
import mx.core.Application;
import mx.managers.CursorManager;
public class Activate
{
private var password:String = "";//removed for security resons
private var loadedFunction:Function;
private var html:HTMLLoader = new HTMLLoader();
public function loadEncryption(doneFunction:Function):void {
loadedFunction = doneFunction;
CursorManager.setBusyCursor();
var JSlocation:String = mx.core.Application.application.PHPurl + "?method=AES";
var urlReq:URLRequest = new URLRequest(JSlocation);
html.load(urlReq);
html.addEventListener(Event.COMPLETE, loaded);
}
private function loaded(event:Event):void {
loadedFunction();
CursorManager.removeBusyCursor();
}
public function checkKey():Boolean {
var activationFile:File = File.applicationDirectory.resolvePath("Team.xml");
var fileStream:FileStream = new FileStream();
fileStream.open(activationFile, FileMode.WRITE);
var fileData:String = fileStream.readUTFBytes(fileStream.bytesAvailable) ;
if(fileData == null)
{
return false;
}
else
{
return true
}
fileStream.close();
}
public function getKey():String {
var activationFile:File = File.applicationDirectory.resolvePath("Team.xml");
var fileStream:FileStream = new FileStream();
fileStream.open(activationFile, FileMode.READ);
var fileData:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvaila ble));
var key:String = AESdecrypt(fileData.key);
var keyMD5:String = hashMD5(key);
var keySHA:String = hashSHA(key);
if(keyMD5 == fileData.security_code_1 && keySHA == fileData.security_code_2)
{
return(key);
}
else
{
return ("Could not verify the key");
}
}
public function insertKey(key:String):void {
var xml:String = '<?xml version="1.0" encoding="utf-8"?><activation><key>' + AESencrypt(key) + "</key><security_code_1>" + hashMD5(key) + "</security_code_1><security_code_2>" + hashMD5(key) + "</security_code_2></activation>";
}
private function AESencrypt(inputString:String):String {
var outputString:String = html.window.AESEncryptCtr(inputString, password, 256);
return outputString;
}
private function AESdecrypt(inputString:String):String {
var outputString:String = html.window.AESDecryptCtr(inputString, password, 256);
return outputString;
}
private function hashSHA(hashString:String):String {
var hashedString:String = SHA256.hash(hashString);
return hashedString;
}
private function hashMD5(hashString:String):String {
var hashedString:String = MD5.hash(hashString);
return hashedString;
}
}
}
Im running it in my application like this:
private function checkKey():void {
if(key.checkKey() == true)
{
if(key.getKey() != "Could not verify the key")
{
dataService.sendEncryptedData({"method": "getUUID", "key": String(key.getKey())}, PHPurl, recieveUUID);
}
else
{
Alert.show("Fatal Error: 1\nThe application must now close", "Error", 4, null, mx.core.Application.application.close);
}
}
else
{
appViews.selectedIndex = 3;
}
}
private function recieveUUID(response:Object):void {
uuid = String(response);
if(uuid == "UUID not found")
{
Alert.show("Error: Could not find team", "Error");
}
else if(uuid == "Team Unactive")
{
Alert.show("This team has been deactivated", "Deactivated");
}
else if(uuid == "")
{
Alert.show("Error selecting team", "Error");
}
else
{
appViews.selectedIndex = 1;
}
}