Skip to content

Instantly share code, notes, and snippets.

@dougvann
Last active October 14, 2020 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougvann/56d367ae1fa94b6bb6e9b60731fc2987 to your computer and use it in GitHub Desktop.
Save dougvann/56d367ae1fa94b6bb6e9b60731fc2987 to your computer and use it in GitHub Desktop.
<?php
function encryptData($data) {
$plainText = json_encode($data);
print '$Plaintext = json_encode($data)' . " \nVAL:$plainText \n \n";
$encryptionKey = 'p@SSword';
print "\$encryptionKey = 'p@SSword' \nVAL:$encryptionKey \n \n";
$bytesToBeEncrypted = $plainText;
print "\$bytesToBeEncrypted = utf8_encode($plainText) \nVAL:$bytesToBeEncrypted \n \n";
$passwordBytes = utf8_encode($encryptionKey);
print '$passwordBytes = utf8_encode($encryptionKey)';
print "\nVAL:$passwordBytes \n \n";
$saltBytes = array(1,2,3,4,5,6,7,8);
print '$saltBytes = array(1,2,3,4,5,6,7,8);' . "\n \n";
$saltBytesstring = "";
print "VAL:";
print_r($saltBytes);
print '$saltBytesstring = "";' . "\n \n";
for($i=0;$i<count($saltBytes);$i++){
$saltBytesstring .= chr($saltBytes[$i]);
}
print 'for($i=0;$i<count($saltBytes);$i++){ echo $i;
$saltBytesstring .= chr($saltBytes[$i]);
}' . "\n \n";
print "\$saltBytes = \nVAL: $saltBytesstring \n";
print "note: the value of \$saltBytesstring is not a visible ascii value \n \n";
$dev = hash_pbkdf2('sha256', $passwordBytes, $saltBytesstring, 10000, 48, true);
print '$dev = hash_pbkdf2(\'sha256\', $passwordBytes, $saltBytesstring, 10000, 48, true);';
print "\n VAL:$dev \n \n";
$key = substr($dev, 0, 32); //Keylength: 32
print '$key = substr($dev, 0, 32); //Keylength: 32';
print "\n VAL:$key \n \n";
$iv = substr($dev, 32, 16); // IV-length: 16
print '$iv = substr($dev, 32, 16); // IV-length: 16';
print "\n VAL:$iv \n \n";
$method = 'aes-256-cbc';
print '$method = \'aes-256-cbc\';';
print "\n VAL:$method \n \n";
$encrypted = base64_encode(openssl_encrypt($bytesToBeEncrypted, $method, $key, OPENSSL_RAW_DATA, $iv));
print '$encrypted = base64_encode(openssl_encrypt($bytesToBeEncrypted, $method, $key, OPENSSL_RAW_DATA, $iv));';
print "\nVAL:$encrypted \n \n";
$encrypted = UrlEncodeBase64($encrypted);
print '$encrypted = UrlEncodeBase64($encrypted);';
return $encrypted;
}
function UrlEncodeBase64($str) {
// $str = str_replace('+', '.', $str);
// $str = str_replace('/', '_', $str);
// $str = str_replace('=', '-', $str);
$str = str_replace('+', '-', $str);
$str = str_replace('/', '_', $str);
$str = str_replace('=', '.', $str);
return $str;
}
$submission = array('Year' => 2020, 'PlanId' => 'P00000000001', 'Zipcode' => "06443", 'Expires' => '2020-10-13T23:59:59.303');
print "\n \nVAL:\n" . encryptData($submission) . "\n \n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment