1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
protected function decryptInfo($toDecrypt) {
    	/* Open the cipher */
    	$td = mcrypt_module_open('rijndael-128', '', 'ecb', '');
    	
    	/* Create the IV and determine the keysize length, use MCRYPT_RAND
		 * on Windows instead */
		$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
		$ks = mcrypt_enc_get_key_size($td);
		
		/* Create key */
		$key = substr(md5('very secret key'), 0, $ks);
    	        
    	/* Intialize encryption */
	    mcrypt_generic_init($td, $key, $iv);
	    
	    //$sendToDec = base64_decode($toDecrypt2);
	    
	    /* Decrypt encrypted string */
	    if(!$toDecrypt){
	    	return "";
	    }
		$decrypted = mdecrypt_generic($td, base64_decode($toDecrypt));
		
		/* Terminate decryption handle and close module */
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		
		return $decrypted;
    
    }
    
    
    function onBeforeWrite() {
    	/* Open the cipher */
    	$td = mcrypt_module_open('rijndael-128', '', 'ecb', '');
    	
    	/* Create the IV and determine the keysize length, use MCRYPT_RAND
		 * on Windows instead */
		$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
		$ks = mcrypt_enc_get_key_size($td);
		
		/* Create key */
		$key = substr(md5('very secret key'), 0, $ks);
    	
    	/* Intialize encryption */
	    mcrypt_generic_init($td, $key, $iv);
	    
	    //$toEncrypt = $this->getField("FirstName");
	    $toEncrypt = array (
	    	$this->getField("FirstName"),
	    	$this->getField("LastName"),
	    	$this->getField("Email"),
	    	$this->getField("Address1"),
	    	$this->getField("Address2"),
	    	$this->getField("City"),
	    	$this->getField("PostalCode"),
	    	$this->getField("PhoneNumber")
	    );
	    
	    $encrypted = array();
	    
	    //$encrypted = mcrypt_generic($td, $toEncrypt);
	    
	    foreach ($toEncrypt as $toBeEncrypted) {
	    	if(!$toBeEncrypted){
	    		array_push($encrypted, $toBeEncrypted);
	    	} else {
	    		$encryptedValue = mcrypt_generic($td, $toBeEncrypted);
		    	array_push($encrypted, $encryptedValue);
	    	}
	    	
	    }
	    
	    
	    //$toWrite = base64_encode($encrypted);	    
	    $this->FirstName = base64_encode($encrypted[0]);
	    $this->LastName = base64_encode($encrypted[1]);
	    $this->Email = base64_encode($encrypted[2]);
	    $this->Address1 = base64_encode($encrypted[3]);
	    $this->Address2 = base64_encode($encrypted[4]);
	    $this->City = base64_encode($encrypted[5]);
	    $this->PostalCode = base64_encode($encrypted[6]);
	    $this->PhoneNumber = base64_encode($encrypted[7]);
	    
	    /* Terminate encryption handler */
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		
		
		
		parent::onBeforeWrite();
    
    }