Encrypt – Decrypt Class
I have come across a few petitions from some of you readers, and I will try to accomplish all of your requests as soon as possible. The first here is a class that I have built based on some ecnryption and decryption functions that I have used in the past. These functions can be found here: http://www.webmasterworld.com/php/3086138.htm?highlight=msg3086437
I took the liberty to create a class so that it can be downloaded by you guys. The use is simple and I can explain how to use it. Just include the class file and create a new object from it:
Usage
In order to encrypt or decrypt, we will need to send in the the string to process, and a passphrase called the key. This key is to make your encryption/decryption process unique to your website. The way to do it is as follows:
Before Using!!
Make sure you initialize the the variables within the class before you encrypt or decrypt. This is to prevent errors.
Below I have the full code for the class. I will soon put it up on github so that it will be available fordownloaded.
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
|
<?php
/**
* Author: Jorge Torres - jotorres1@gmail.com
* Website: http://www.jotorres.com
* Source Website: http://www.webmasterworld.com/php/3086138.htm?highlight=msg3086437
* Date: 10/17/2012
* Version: 1.0
* Description: An encryption and decryption class for small projects
* Documentation: http://www.jotorres.com/
*/
class jtcrypt{
/**
* @access private
* @var string
*/
private $result;
/**
* @access private
* @var int
*/
private $count;
public function __construct(){
// Initial values
$this->init_values();
}
/**
* Function to encrypt
*/
public function encrypt($string, $key) {
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)+ord($keychar));
$this->result.=$char;
}
return base64_encode($this->result);
}
/**
* Function to decrypt
*/
public function decrypt($string, $key) {
$string = base64_decode($string);
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$this->result.=$char;
}
return $this->result;
}
/**
* Initialize values
*/
public function init_values(){
$this->result = '';
$this->count = 0;
}
}
?>
|
0 Komentar
Berikan Komentar yang bermanfaat dan sehat.