encryption-class

Gesponsorde koppelingen

PHP script bestanden

  1. encryption-class

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php

class secure_data
{

// this function is the constructor for our class.
function open_module(){
    
    $this->td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
    $this->iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_RAND);
    $this->iv = substr(md5($this->iv), 0, mcrypt_enc_get_iv_size($this->td));
    $this->ks = mcrypt_enc_get_key_size($this->td);
    
}



// this function encrypt's only input data
function encrypt($key,$data){
    
    $this->ori_data = $data;
    
    $this->key = substr(md5($key), 0, $this->ks);
    
    mcrypt_generic_init($this->td, $this->key, $this->iv);
    
    $this->data = mcrypt_generic($this->td, $data);
    
}



// this function encrypt's only files
function encrypt_file($key,$filename){
    
    $this->fp = fopen($filename,"r");
    
    $file_content ="";
    while (!feof($this->fp)){
        $file_content .= fread($this->fp, 8192);
    }

    
    $this->ori_data = $file_content;
        
    $this->key = substr(md5($key), 0, $this->ks);
    
    mcrypt_generic_init($this->td, $this->key, $this->iv);
    
    $this->data = mcrypt_generic($this->td, $file_content);
    
    
    $this->fpn = fopen($filename.".safe","w");
    
    fwrite($this->fpn,$this->data);
    
    
}



// decript data
function decrypt($key,$data,$iv){
    
    mcrypt_generic_init($this->td, $key, $iv);
    
    $this->data = mdecrypt_generic($this->td, $data);
    
}



// decrypt encrypted file
function decrypt_file($key,$filename,$iv){
    
    $this->fp = fopen($filename,"r");
    
    $file_content ="";
    while (!feof($this->fp)){
        $file_content .= fread($this->fp, 8192);
    }

    
    mcrypt_generic_init($this->td, $key, $iv);
    
    $this->data = mdecrypt_generic($this->td, $file_content);
    
    
    $filename = str_replace(".safe","",$filename);
    $this->fpn = fopen($filename,"w");
    
    fwrite($this->fpn,$this->data);
    
}



// verify's if the data original data is different from the encrypted data
function verify(){
    
    if($this->ori_data != $this->data){
        return true;
    }
else{
        return false;
    }
    
}

    
function
base64_encode(){
    
    $this->data = base64_encode($this->data);
    
}


function
base64_decode(){
    
    $this->data = base64_decode($this->data);
    
}



// returns the key used for encryption
function get_key(){
    
    return $this->key;
    
}



// returns intilization vector
function get_iv(){
    
    return $this->iv;
    
}



// this returns current data
function get_data(){
    
    return $this->data;
    
}



// We dont need to close the module
function close_module(){
    
    mcrypt_generic_deinit($this->td);
    mcrypt_module_close($this->td);
    
}


// end class
}

/* this is just a example
$secure = new secure_data;
$secure->open_module();
$secure->encrypt_file("dunno","test.png");
$secure->decrypt_file($secure->get_key(),"test.png.safe",$secure->get_iv());
echo $secure->get_key();
echo $secure->get_iv();
$secure->close_module();
*/

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.