-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crypt.php
80 lines (67 loc) · 1.74 KB
/
Crypt.php
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
<?php
/*
DOCUMENTATION
To instantiate the class:
include( 'path/to/Crypt.php' );
$Crypt = new Crypt();
To encrypt a string:
$encrypted_string = $Crypt->encrypt( $string_to_encrypt, $secret_passphrase );
To decrypt a string:
$decrypted_string = $Crypt->decrypt( $encrypted_string, $secret_passphrase );
*/
class Crypt {
public function __construct() {}
/**
* @desc Decodes a string from base64.
* @param string $data
* @return string
*/
public function decodeBase64( $data ) {
$base_64 = strtr( $data, '-_', '+/' );
return base64_decode( $base_64 );
}
/**
* @desc Decrypt data string using key.
* @param string $data
* @param string $key
* @return string
*/
public function decrypt( $data, $key ) {
$result = '';
//$data = $this->decodeBase64( $data );
$data = base64_decode( $data );
for ( $i = 0; $i < strlen( $data ); $i++ ) {
$char = substr( $data, $i, 1 );
$key_char = substr( $key, ( $i % strlen( $key ) ) -1, 1 );
$char = chr( ord( $char ) - ord( $key_char ) );
$result .= $char;
}
return $result;
}
/**
* @desc Encodes a string in base64.
* @param string $data
* @return string
*/
public function encodeBase64( $data ) {
$base_64 = base64_encode( $data );
return strtr( $base_64, '+/', '-_' );
}
/**
* @desc Encrypt data string using key.
* @param string $data
* @param string $key
* @return string
*/
public function encrypt( $data, $key ) {
$result = '';
for ( $i = 0; $i < strlen( $data ); $i++ ) {
$char = substr( $data, $i, 1 );
$key_char = substr( $key, ( $i % strlen( $key ) ) -1, 1 );
$char = chr( ord( $char ) + ord( $key_char ) );
$result .= $char;
}
//return $this->encodeBase64( $result );
return base64_encode( $result );
}
}