39 define(
"TICKET_CIPHERMODE",
'des-ede3-cbc');
40 define(
"EZCARD_CIPHERMODE",
'des-ede3-cbc');
41 define(
"CERTEGY_CIPHERMODE",
'des-ede3');
42 define(
"DIGITAL_CIPHERMODE",
'des-ede3');
43 define(
"BILLPAY_CIPHERMODE",
'cast5-ecb');
44 define(
"VSOFT_CIPHERMODE",
'aes-256-ecb');
46 define(
"MVI_CIPHERMODE",
'aes-256-cbc');
47 define(
"SAVVYMO_CIPHERMODE",
'aes-256-cbc');
50 define(
"THIRDPARTY_DEFAULT_AUTH_ALGO",
"sha256");
56 function check_mcrypt_exists() {
57 if (!function_exists(
"mdecrypt_generic") && !function_exists(
"mcrypt_generic"))
58 throw new exception(
"MCRYPT library does not exist.");
68 function encrypt_ticket_mcrypt($ticket, $key, $iv) {
69 check_mcrypt_exists();
70 # pad so length of ticket is even multiple of 8 71 $ticket .= str_repeat(
' ', 8 - (strlen($ticket) % 8));
72 $td = mcrypt_module_open(MCRYPT_3DES,
'', MCRYPT_MODE_CBC,
'');
73 mcrypt_generic_init($td, $key, $iv);
74 $tktvalue = mcrypt_generic($td, $ticket);
75 $tktvalue = bin2hex($tktvalue);
76 mcrypt_generic_deinit($td);
77 mcrypt_module_close($td);
78 return array($tktvalue, $ticket);
84 function encrypt_ticket_openssl($ticket, $key, $iv) {
86 # pad so length of ticket is even multiple of 8 87 $ticket .= str_repeat(
' ', 8 - (strlen($ticket) % 8));
88 $openssl_enc = hcuOpenSSLEncrypt($ticket,
91 THIRDPARTY_DEFAULT_AUTH_ALGO,
94 $context=
"connect_chkfree");
95 return array(bin2hex($openssl_enc[
"message"]), $ticket);
96 }
catch (exception $ex) {
105 function decrypt_ticket_mcrypt($ticket, $key, $iv) {
106 check_mcrypt_exists();
108 if(function_exists(
"mdecrypt_generic")) {
109 $ticket = hex2bin($ticket);
110 $td = mcrypt_module_open(MCRYPT_3DES,
'', MCRYPT_MODE_CBC,
'');
111 mcrypt_generic_init($td, $key, $iv);
112 $tktvalue = mdecrypt_generic($td, $ticket);
113 mcrypt_generic_deinit($td);
114 mcrypt_module_close($td);
118 }
catch (exception $ex) {
126 function decrypt_ticket_openssl($ticket, $key, $iv) {
128 $ticket = hex2bin($ticket);
129 $dec_ticket = hcuOpenSSLDecrypt($ticket,
133 THIRDPARTY_DEFAULT_AUTH_ALGO,
136 $context=
"connect_chkfree");
140 }
catch (exception $ex) {
152 function encrypt_ezcard_openssl($ssoPkt, $key, $iv) {
157 $pad = $blocksize - (strlen($ssoPkt) % $blocksize);
158 $ssoPkt = ($ssoPkt . str_repeat(chr($pad), $pad));
160 $openssl_enc = hcuOpenSSLEncrypt($ssoPkt,
163 THIRDPARTY_DEFAULT_AUTH_ALGO,
166 $context=
"connect_ezcard");
167 $packet = base64_encode($openssl_enc[
"message"]);
168 $packet = str_replace(array(
"+",
"/",
"="), array(
"-",
"_",
"."), $packet);
169 return array($packet, $ssoPkt);
171 }
catch (exception $ex) {
180 function encrypt_ezcard_mcrypt($td_cbc, $ssoPkt, $key, $iv) {
181 check_mcrypt_exists();
184 $blocksize = mcrypt_enc_get_block_size($td_cbc);
185 $pad = $blocksize - (strlen($ssoPkt) % $blocksize);
186 $ssoPkt = ($ssoPkt . str_repeat(chr($pad), $pad));
188 mcrypt_generic_init($td_cbc, $key, $iv);
189 $packet = mcrypt_generic($td_cbc, $ssoPkt);
190 mcrypt_generic_deinit($td_cbc);
192 $packet = base64_encode($packet);
193 $packet = str_replace(array(
"+",
"/",
"="), array(
"-",
"_",
"."), $packet);
195 return array($packet, $ssoPkt);
201 function decrypt_ezcard_openssl($ssoPktEnc, $key, $iv) {
203 $ssoPktEnc = str_replace(array(
"-",
"_",
"."), array(
"+",
"/",
"="), $ssoPktEnc);
204 $ssoPktEnc = base64_decode($ssoPktEnc);
205 $ssoPktDec = hcuOpenSSLDecrypt($ssoPktEnc,
209 THIRDPARTY_DEFAULT_AUTH_ALGO,
212 $context=
"connect_ezcard");
215 }
catch (exception $ex) {
224 function decrypt_ezcard_mcrypt($ssoPktEnc, $key, $priviv) {
225 check_mcrypt_exists();
227 $td_cbc = mcrypt_module_open(MCRYPT_3DES,
'', MCRYPT_MODE_CBC,
'');
228 $iv = substr($priviv, 0, mcrypt_enc_get_iv_size($td_cbc));
230 $ssoPktEnc = str_replace(array(
"-",
"_",
"."), array(
"+",
"/",
"="), $ssoPktEnc);
231 $ssoPktEnc = base64_decode($ssoPktEnc);
233 mcrypt_generic_init($td_cbc, $key, $iv);
234 $ssoPktDec = mdecrypt_generic($td_cbc, $ssoPktEnc);
235 mcrypt_generic_deinit($td_cbc);
238 mcrypt_module_close($td_cbc);
247 function encrypt_ezcard_sso($ssoRequest,
254 list($innerpkt, $paddedSsoRequest) = encrypt_ezcard_openssl($ssoRequest, $privkey, $iv);
257 $ssoWrap =
"$salt<SSOWrapper ClientId=\"{$clientId}\" SSORequest=\"$innerpkt\" Ver=\"3.0\"/>";
258 list($outpkt, $paddedSsoWrap) = encrypt_ezcard_openssl($ssoWrap, $pubkey, $iv);
260 $outpkt = str_replace(array(
"+",
"/",
"="), array(
"-",
"_",
"."), $outpkt);
261 return array($paddedSsoRequest, $innerpkt, $paddedSsoWrap, $outpkt);
269 function encrypt_ezcard_sso_mcrypt($ssoRequest,
275 check_mcrypt_exists();
277 $td = mcrypt_module_open(MCRYPT_3DES,
'', MCRYPT_MODE_CBC,
'');
278 $iv = substr($priviv, 0, mcrypt_enc_get_iv_size($td));
281 list($innerpkt, $paddedSsoRequest) = encrypt_ezcard_mcrypt($td, $ssoRequest, $privkey, $iv);
284 $ssoWrap =
"$salt<SSOWrapper ClientId=\"{$clientId}\" SSORequest=\"$innerpkt\" Ver=\"3.0\"/>";
285 list($outpkt, $paddedSsoWrap) = encrypt_ezcard_mcrypt($td, $ssoWrap, $pubkey, $iv);
287 $outpkt = str_replace(array(
"+",
"/",
"="), array(
"-",
"_",
"."), $outpkt);
290 mcrypt_module_close($td);
292 return array($paddedSsoRequest, $innerpkt, $paddedSsoWrap, $outpkt);
302 function encrypt_certegy_account_mcrypt($ACCOUNT, $key) {
304 # set values for testing 307 # $FIID="8000"; #CU Institution ID for testing 308 # $ACCOUNT="214027153"; # not enrolled 309 # $ACCOUNT="210328770"; # enrolled"; 310 # Implement PKCS5 / PKCS7 Padding (PKCS7 apparently extends PKCS5, but they 311 # are the same for 8-byte blocks) 312 # cipher algorithm needs 8-byte blocks, pad data with binary bytes 313 # - 01 if you need 1 byte, 02 02 if you need 2 bytes, ... 314 # 07 07 07 07 07 07 07 if you need 7 bytes, and if you have a multiple of 8, 315 # pad with 8 bytes 08. 317 check_mcrypt_exists();
318 $blocksize = mcrypt_get_block_size(
'tripledes',
'ecb');
319 $pad = $blocksize - (strlen($ACCOUNT) % $blocksize);
320 $ACCOUNT = ($ACCOUNT . str_repeat(chr($pad), $pad));
322 $td = mcrypt_module_open(MCRYPT_TRIPLEDES,
'',
'ecb',
'');
323 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
324 mcrypt_generic_init($td, $key, $iv);
325 $dm_token = mcrypt_generic($td, $ACCOUNT);
326 mcrypt_generic_deinit($td);
327 mcrypt_module_close($td);
329 return array($dm_token, $ACCOUNT);
336 function decrypt_certegy_account_mcrypt($ACCOUNT_CIPHER, $key) {
337 check_mcrypt_exists();
338 $td = mcrypt_module_open(MCRYPT_TRIPLEDES,
'',
'ecb',
'');
339 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
340 mcrypt_generic_init($td, $key, $iv);
341 $ACCOUNT_DECRYPTED = mdecrypt_generic($td, $ACCOUNT_CIPHER);
342 mcrypt_generic_deinit($td);
343 mcrypt_module_close($td);
344 return $ACCOUNT_DECRYPTED;
350 function encrypt_certegy_openssl($account, $key) {
354 $pad = $blocksize - (strlen($account) % $blocksize);
355 $account = ($account . str_repeat(chr($pad), $pad));
358 $openssl_enc = hcuOpenSSLEncrypt($account,
361 THIRDPARTY_DEFAULT_AUTH_ALGO,
364 $context=
"connect_certegy");
365 $account_enc = $openssl_enc[
"message"];
367 return array($account_enc, $account);
369 }
catch (exception $ex) {
377 function decrypt_certegy_openssl($account_cipher, $key) {
379 $account_dec = hcuOpenSSLDecrypt($account_cipher,
383 THIRDPARTY_DEFAULT_AUTH_ALGO,
386 $context=
"connect_certegy");
389 }
catch (exception $ex) {
402 function encrypt_digital_mcrypt($srcstring, $servicekey) {
403 # Implement PKCS5 / PKCS7 Padding (PKCS7 apparently extends PKCS5, but they 404 # are the same for 8-byte blocks) 405 # cipher algorithm needs 8-byte blocks, pad data with binary bytes 406 # - 01 if you need 1 byte, 02 02 if you need 2 bytes, ... 407 # 07 07 07 07 07 07 07 if you need 7 bytes, and if you have a multiple of 8, 408 # pad with 8 bytes 08. 418 check_mcrypt_exists();
419 $blocksize = mcrypt_get_block_size(
'tripledes',
'ecb');
420 $pad = $blocksize - (strlen($srcstring) % $blocksize);
421 $srcstring = ($srcstring . str_repeat(chr($pad), $pad));
423 $td = mcrypt_module_open(MCRYPT_TRIPLEDES,
'',
'ecb',
'');
424 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
425 mcrypt_generic_init($td, $servicekey, $iv);
426 $dm_token = mcrypt_generic($td, $srcstring);
427 mcrypt_generic_deinit($td);
428 mcrypt_module_close($td);
430 return array($dm_token, $srcstring);
437 function decrypt_digital_mcrypt($srcstring_cipher, $servicekey) {
439 check_mcrypt_exists();
440 $td = mcrypt_module_open(MCRYPT_TRIPLEDES,
'',
'ecb',
'');
441 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
442 mcrypt_generic_init($td, $servicekey, $iv);
443 $dec_srcstring = mdecrypt_generic($td, $srcstring_cipher);
444 mcrypt_generic_deinit($td);
445 mcrypt_module_close($td);
446 return $dec_srcstring;
452 function encrypt_digital_openssl($srcstring, $key) {
456 $pad = $blocksize - (strlen($srcstring) % $blocksize);
457 $srcstring = ($srcstring . str_repeat(chr($pad), $pad));
460 $openssl_enc = hcuOpenSSLEncrypt($srcstring,
463 THIRDPARTY_DEFAULT_AUTH_ALGO,
466 $context=
"connect_digital");
467 $srcstring_enc = $openssl_enc[
"message"];
469 return array($srcstring_enc, $srcstring);
471 }
catch (exception $ex) {
479 function decrypt_digital_openssl($srcstring_cipher, $key) {
481 $srcstring_dec = hcuOpenSSLDecrypt($srcstring_cipher,
485 THIRDPARTY_DEFAULT_AUTH_ALGO,
488 $context=
"connect_digital");
489 return $srcstring_dec;
491 }
catch (exception $ex) {
503 function encrypt_billpay_mcrypt($billpayid, $key) {
504 check_mcrypt_exists();
505 # if billpayid is less than 2 chars, left pad w/zero 506 if (strlen($billpayid) < 2) {
507 $billpayid = substr(
"00$billpayid", -2, 2);
510 $billpayid .= str_repeat(
"\0", 8 - (strlen($billpayid) % 8));
512 $td = mcrypt_module_open(MCRYPT_CAST_128,
'',
'ecb',
'');
513 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
514 mcrypt_generic_init($td, $key, $iv);
515 $ipay_token = urlencode(base64_encode(mcrypt_generic($td, $billpayid)));
516 mcrypt_generic_deinit($td);
517 mcrypt_module_close($td);
518 return array($ipay_token, $billpayid);
525 function decrypt_billpay_mcrypt($billpayid_cipher, $key) {
526 check_mcrypt_exists();
527 $td = mcrypt_module_open(MCRYPT_CAST_128,
'',
'ecb',
'');
528 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
529 mcrypt_generic_init($td, $key, $iv);
530 $billpayid_cipher = base64_decode(urldecode($billpayid_cipher));
531 $ipay_token_dec = mdecrypt_generic($td, $billpayid_cipher);
532 mcrypt_generic_deinit($td);
533 mcrypt_module_close($td);
534 return $ipay_token_dec;
540 function encrypt_billpay_openssl($billpayid, $key) {
542 # if billpayid is less than 2 chars, left pad w/zero 543 if (strlen($billpayid) < 2) {
544 $billpayid = substr(
"00$billpayid", -2, 2);
547 $billpayid .= str_repeat(
"\0", 8 - (strlen($billpayid) % 8));
549 $openssl_enc = hcuOpenSSLEncrypt($billpayid,
552 THIRDPARTY_DEFAULT_AUTH_ALGO,
555 $context=
"connect_ipay");
556 $billpayid_enc = urlencode(base64_encode($openssl_enc[
"message"]));
558 return array($billpayid_enc, $billpayid);
560 }
catch (exception $ex) {
568 function decrypt_billpay_openssl($billpayid_cipher, $key) {
570 $billpayid_cipher = base64_decode(urldecode($billpayid_cipher));
571 $billpayid_dec = hcuOpenSSLDecrypt($billpayid_cipher,
575 THIRDPARTY_DEFAULT_AUTH_ALGO,
578 $context=
"connect_ipay");
579 return $billpayid_dec;
581 }
catch (exception $ex) {
593 function encrypt_vsoftquery_mcrypt($vsoftqry, $vsoftkey) {
594 check_mcrypt_exists();
595 # blocksize is 16 bytes 596 $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,
'ecb');
597 $pad = $blocksize - (strlen($vsoftqry) % $blocksize);
598 $vsoftqry = ($vsoftqry . str_repeat(chr($pad), $pad));
600 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,
'',
'ecb',
'');
601 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
602 mcrypt_generic_init($td, $vsoftkey, $iv);
603 $vsoftenc = mcrypt_generic($td, $vsoftqry);
604 mcrypt_generic_deinit($td);
605 mcrypt_module_close($td);
607 $vsoftenc = base64_encode($vsoftenc);
608 return array($vsoftenc, $vsoftqry);
615 function decrypt_vsoftquery_mcrypt($vsoftqry_cipher, $vsoftkey) {
616 check_mcrypt_exists();
617 $vsoftqry_cipher = base64_decode($vsoftqry_cipher);
619 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,
'',
'ecb',
'');
620 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
621 mcrypt_generic_init($td, $vsoftkey, $iv);
622 $vsoftdec = mdecrypt_generic($td, $vsoftqry_cipher);
623 mcrypt_generic_deinit($td);
624 mcrypt_module_close($td);
632 function encrypt_vsoftquery_openssl($vsoftqry, $key) {
636 $pad = $blocksize - (strlen($vsoftqry) % $blocksize);
637 $vsoftqry = ($vsoftqry . str_repeat(chr($pad), $pad));
639 $openssl_enc = hcuOpenSSLEncrypt($vsoftqry,
642 THIRDPARTY_DEFAULT_AUTH_ALGO,
645 $context=
"connect_vsoft");
646 $vsoftqry_enc = base64_encode($openssl_enc[
"message"]);
648 return array($vsoftqry_enc, $vsoftqry);
650 }
catch (exception $ex) {
658 function decrypt_vsoftquery_openssl($vsoftqry_cipher, $key) {
660 $vsoftqry_cipher = base64_decode($vsoftqry_cipher);
661 $vsoftqry_dec = hcuOpenSSLDecrypt($vsoftqry_cipher,
665 THIRDPARTY_DEFAULT_AUTH_ALGO,
668 $context=
"connect_vsoft");
669 return $vsoftqry_dec;
671 }
catch (exception $ex) {
683 function encrypt_mvi_mcrypt($mvi_query, $ckhexkey, $iv=
"") {
684 # MCRYPT_RIJNDAEL_256 is not AES. The 256 in that constant refers to the blocksize, not the keysize. 685 # Use MCRYPT_RIJNDAEL_128 to get the same algorithm as AES. The keysize is set by the number of bytes 686 # in the key argument you supply. So supply 32 bytes and you get AES with a 256-bit key. 687 check_mcrypt_exists();
688 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,
'',
'cbc',
'');
690 # uncomment the following line to get a random iv for each call 691 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
695 mcrypt_generic_init($td, $ckhexkey, $iv);
696 $mvi_data = bin2hex(mcrypt_generic($td, $mvi_query));
697 mcrypt_generic_deinit($td);
698 mcrypt_module_close($td);
704 for ($i = 0; $i < strlen($iv); $i++) {
705 $h = dechex(ord($iv[$i]));
706 $hexiv_this = substr(
'0' . $h, -2);
707 $hexiv .= $hexiv_this;
718 function decrypt_mvi_mcrypt($mvi_cipher_iv, $ckhexkey) {
719 check_mcrypt_exists();
720 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,
'',
'cbc',
'');
721 $iv_size = mcrypt_enc_get_iv_size($td);
723 $iv_hex = substr($mvi_cipher_iv, $iv_size * -2);
724 $mvi_cipher_hex = substr($mvi_cipher_iv, 0, $iv_size * -2);
725 $iv = hex2bin($iv_hex);
727 mcrypt_generic_init($td, $ckhexkey, $iv);
728 $mvi_dec = mdecrypt_generic($td, hex2bin($mvi_cipher_hex));
729 mcrypt_generic_deinit($td);
730 mcrypt_module_close($td);
732 return rtrim($mvi_dec,
"\0");
738 function encrypt_mvi_openssl($mvi_query, $ckhexkey, $iv=
"") {
741 $blocksize = openssl_cipher_iv_length(MVI_CIPHERMODE);
743 $mvi_query .= str_repeat(
"\0", $blocksize - (strlen($mvi_query) % $blocksize));
744 $openssl_enc = hcuOpenSSLEncrypt($mvi_query,
747 THIRDPARTY_DEFAULT_AUTH_ALGO,
750 $context=
"connect_mvi");
751 $mvi_enc = bin2hex($openssl_enc[
"message"]);
752 $mvi_iv = bin2hex($openssl_enc[
"iv"]);
754 return $mvi_enc.$mvi_iv;
756 }
catch (exception $ex) {
764 function decrypt_mvi_openssl($mvi_cipher_iv, $ckhexkey) {
767 $blocksize = openssl_cipher_iv_length(MVI_CIPHERMODE);
768 $iv_hex = substr($mvi_cipher_iv, $blocksize * -2);
769 $mvi_cipher_hex = substr($mvi_cipher_iv, 0, $blocksize * -2);
771 $mvi_dec = hcuOpenSSLDecrypt(hex2bin($mvi_cipher_hex),
775 THIRDPARTY_DEFAULT_AUTH_ALGO,
777 $iv=hex2bin($iv_hex),
778 $context=
"connect_mvi");
780 return rtrim($mvi_dec,
"\0");
781 }
catch (exception $ex) {
788 function encrypt_smo_openssl($smo_query, $smo_key) {
790 $blocksize = openssl_cipher_iv_length(SAVVYMO_CIPHERMODE);
791 $pad = $blocksize - (strlen($smo_query) % $blocksize);
792 $smo_query = ($smo_query . str_repeat(chr($pad), $pad));
793 $smo_key = base64_decode($smo_key);
795 for ($i = 0; $i < $blocksize; $i++) {
799 $openssl_enc = hcuOpenSSLEncrypt($smo_query,
802 THIRDPARTY_DEFAULT_AUTH_ALGO,
805 $context=
"connect_smo");
806 $smo_enc = base64_encode($openssl_enc[
"message"]);
810 }
catch (exception $ex) {