<?php
class CCProtect {
private function pekkaEncode($s) {
$out = '';
for ($i=0;$i<strlen($s); $i++) {
$out .= sprintf("%03d", ord($s[$i]));
}
return $out;
}
private function pekkaDecode($s) {
$out = '';
for ($i=0;$i<strlen($s);$i+=3) {
$out .= chr($s[$i].$s[$i+1].$s[$i+2]);
}
return $out;
}
private function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
private function mCrypt($text, $salt) {
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($salt), $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
private function mDecrypt($text, $salt) {
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($salt), base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
public function encrypt($content) {
$response = array(
0 => rand(1000, 9999),
1 => rand(1000, 9999),
2 => rand(1000, 9999),
3 => rand(1000, 9999),
4 => rand(1000, 9999),
5 => rand(1000, 9999),
6 => rand(1000, 9999),
7 => rand(1000, 9999),
8 => rand(1000, 9999),
9 => rand(1000, 9999),
10 => rand(0, 9),
11 => rand(0, 9),
12 => rand(0, 9),
13 => rand(0, 9),
14 => rand(5, 9),
15 => '',
16 => '',
17 => '',
18 => '',
19 => '',
20 => '',
21 => '',
22 => '',
23 => '',
24 => '',
25 => '',
);
$rand = rand(10, 14);
$combination = str_split($response[$response[$rand]], 1);
$password = $response[$combination[0]] . $response[$combination[1]] . $response[$combination[2]] . $response[$combination[3]];
if(is_array($content)) {
$content = json_encode($content);
}
$content_length = strlen($content);
$content_field = rand(20, 25);
$store_content_field = 10 + $response[14];
for($i=15; $i <= 19; $i++) {
if($i == $store_content_field) {
$response[$i] = $this->mCrypt($content_field, $password);
} else {
$response[$i] = $this->mCrypt(rand(10, 99), $password);
}
}
for($i=20; $i <= 25; $i++) {
if($i == $content_field) {
$response[$i] = $this->mCrypt($content, $password);
} else {
$response[$i] = $this->mCrypt($this->generateRandomString($content_length), $password);
}
}
$json = json_encode($response);
$string = $password . $this->pekkaEncode(str_rot13(convert_uuencode($json)));
$string = str_split($string, 4);
$string = implode(' ', $string);
return trim($string);
}
public function decrypt($string) {
$string = str_replace(' ', '', $string);
$password = substr($string, 0, 16);
$decoded = json_decode(convert_uudecode(str_rot13($this->pekkaDecode(substr($string, 16)))));
return trim($this->mDecrypt($decoded[$this->mDecrypt($decoded[10 + $decoded[14]], $password)], $password));
}
}
$protection = new CCProtect;
$cc = array(
'firstname' => 'Krista',
'lastname' => 'Sheppard',
'dob' => 'August 11, 1932',
'cardnumber' => '4916 5210 7061 9044',
'cvv2' => '769',
'expire' => array(
'month' => '6',
'year' => '2015',
),
);
?>
<html>
<head>
</head>
<body>
<h1>CCProtect example</h1>
<?php
$action = (isset($_GET['action']))?$_GET['action']:'encrypt';
switch($action) {
case 'decrypt':
echo '
<form action="?action=showdecrypt" method="POST">
Enter your encrypted message:<br />
<textarea style="height: 680px; width: 660px" name="message"></textarea><br>
<input type="submit" value="Decrypt!">
</form>
';
break;
case 'showdecrypt':
if(!isset($_POST['message'])) {
header("Location: ?action=error");
exit;
}
$message = $_POST['message'];
echo '
Here is the decrypted result of your message:<br />
<textarea style="height: 680px; width: 660px">' . $protection->decrypt($message) . '</textarea><br>
<a href="?action=encrypt">Encrypt another message</a>
';
break;
case 'showencrypt':
if(!isset($_POST['message'])) {
header("Location: ?action=error");
exit;
}
$message = $_POST['message'];
echo '
Here is your message encrypted!<br />
<textarea style="height: 680px; width: 660px">' . $protection->encrypt($message) . '</textarea><br>
<a href="?action=decrypt">Decrypt this message</a> (copy & paste) - <a href="?action=encrypt">Encrypt another message</a>
';
break;
case 'encrypt':
echo '
<form action="?action=showencrypt" method="POST">
Encrypt a message:<br />
<textarea style="height: 680px; width: 660px" name="message"></textarea><br>
<input type="submit" value="Encrypt!">
</form>
';
break;
case 'error':
echo 'Something went wrong when trying to complete your action. Please try agian.<br><a href="?action=encrypt">Go back</b>';
break;
default:
echo '';
}
?>
</body>
</html>