openssl 使用

关于nets的传输加密,尼玛坑爹啊,什么东西都不给。

<?php
 $fp = fsockopen("203.116.93.163", 80, $errno, $errstr, 30);
 if (!$fp)
 {
  echo 'Could not open connection.';
 }
 else
 {
  $time    = time();
  $mid     = '116';
  $mKey    = 'wFmLP5OF5xUZivXCXqgnL0Rl4C78vVkn';
  $ref     = $mid.$time;
  $card_number = '0004500100000000014';

  //generate random
  $initial_random = bin2hex(openssl_random_pseudo_bytes(8));
  echo 'Initial Random : '.$initial_random.'<br>';
  //======================================================================
  //Encrypted
  //======================================================================
  
  //encrypt by merchant's private key
  $fp=fopen("D:/wamp/www/key/icol_priv_key.pem","r"); 
  $priv_key=fread($fp,8192); 
  fclose($fp);
  $res = openssl_get_privatekey($priv_key,'icolumn1014'); 

  //private encrypt
  openssl_private_encrypt($initial_random,$crypttext_ico_priv,$res);

  $cry_len = strlen($crypttext_ico_priv);
  $num = ceil($cry_len/100);

  //Block encrypted
  $fp=fopen("D:/wamp/www/key/icol_public.cert","r");
  $pub_key=fread($fp,8192); 
  fclose($fp);

  openssl_get_publickey($pub_key); 
  $encrypt_val = '';
  $new_encrypt_val = '';

  for($i=0; $i<$num; $i++)
  {
   $encrypt_val = substr($crypttext_ico_priv, $i*100, 100 );
   openssl_public_encrypt($encrypt_val, $pub_encrypt, $pub_key);
   $new_encrypt_val.=$pub_encrypt;
  }

  $full_enctypt_str = $new_encrypt_val;

  //======================================================================
  //Decrypted
  //======================================================================
  $len = strlen($full_enctypt_str);
  $f_num = ceil($len/256);

  $full_pri_decrypt = '';
  for($j=0; $j<$f_num; $j++)
  {
   $block = substr($full_enctypt_str, $j*256, 256 );
   openssl_private_decrypt($block,$block_decrypt,$res);

   $full_pri_decrypt .= $block_decrypt;
  }

  openssl_public_decrypt($full_pri_decrypt,$original_data,$pub_key);
  echo 'Original Data &nbsp;: '.$original_data;
 }
?>