Has anyone ever had a UnionPay POS terminal mac algorithm [PHP version]?

Source: Internet
Author: User
Tags mcrypt
Security mechanisms

MAC for messages for tamper-proof

The initiator first Base64 the label data, according to the encoded data, then loop xor (divide the data into several 8-byte data segments, and finally less than 8 bits plus 0x00 8-bit). Get the tag data. The new message is then sent to the other party.

BASE64 encoded results for the original message

xml
  
   
  
       
   
            
    
     
      
     ccb_pos
    
             
    
     
      
     pos_service
    
             
    
     20111121094051    
   
            
   
    
     
    PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48ZGF0YT48cHJvZHVjdD4wMzI2PC9wcm9kdWN0PjxwcmVtaXVtPjExMDAuMDwvcHJlbWl1bT48ZHJpdmVycz48ZHJpdmVyPkphbWVzPC9kcml2ZXI+PGRyaXZlcj5UZWRkeTwvZHJpdmVyPjwvZHJpdmVycz48L2RhdGE+
   
        
   
    
     
     8U42KXyd76FSCmRT
   
       //char 16    
  
   

Processing mode

After receiving the message, the receiver divides the label data into several 8-byte data segments, the last less than 8 bits plus 0x00 8-bit, loop xor or one time. Then compare it with the label data. If the comparison is consistent with the validation, finally the label data is decoded according to Base64, and the transaction plaintext data is obtained.

Reference documents

http://blog.163.com/gene_lu/blog/static/6402542120133151112717/

I found a PHP code on the Internet.

This code has a problem, probably the way.

php
  

'; Var_dump (Base64_decode ($STR)); Echo '


'; Var_dump (Mac_ ($STR)); Echo '


';//Description: Mac algorithm, the string $data is divided into eight bytes of data block, labeled D1,D2,D3, if the last chunk of data is less than//8 bytes, then add \x00 until the length reaches 8 bytes, and then follow the process below to generate the MAC,//8 byte initial value : \x00\x00\x00\x00\x00\x00\x00\x00//process: Initial value XOR or d1-> result of des XOR or d2-> xor result of des XOR or d2-> xor result desfunction ma C_ ($data) {//Set initial value $init = "\x00\x00\x00\x00\x00\x00\x00\x00"; The $data is divided into 8 bytes, and the array $tmp _data=strtoarray ($data, 8) are obtained. $tmp _init= $init; foreach ($tmp _data as $value) {//XOR $tmp _init=myencrypt ($tmp _init, $value); Var_dump ($tmp _init); Encrypt//$tmp _init=mcrypt_str ($tmp _init); } var_dump ($tmp _init); Return Bin2Hex ($tmp _init);} Function Name: strtoarray//Description: The string is cut by 8 bytes, and the last less than 8 bytes of \x00 fill function Strtoarray ($str, $limit) {$len =strlen ($STR); $tmp _arr=array (); $count =floor ($len/$limit); $left _num= $len-$count * $LIMIT; for ($i =0; $i < $left _num; $i + +) {$str. = "\x00"; } for ($i =0; $i < $count; $i + +) {$tmp _arr[]=substr ($str, 0, $limit); if (strlen ($STR) > $limit + 1) {$str =substr ($str, $limit, strlen ($STR)); }} return $tmp _arr;} Function Name: myencrypt//Description: Xor operator function Myencrypt ($string, $key) {for ($i =0; $i

I do not know who has studied, how to do, no clue.

Reply content:

Security mechanisms

MAC for messages for tamper-proof

The initiator first Base64 the label data, according to the encoded data, then loop xor (divide the data into several 8-byte data segments, and finally less than 8 bits plus 0x00 8-bit). Get the tag data. The new message is then sent to the other party.

BASE64 encoded results for the original message

xml
  
   
  
       
   
            
    
     
      
     ccb_pos
    
             
    
     
      
     pos_service
    
             
    
     20111121094051    
   
            
   
    
     
    PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48ZGF0YT48cHJvZHVjdD4wMzI2PC9wcm9kdWN0PjxwcmVtaXVtPjExMDAuMDwvcHJlbWl1bT48ZHJpdmVycz48ZHJpdmVyPkphbWVzPC9kcml2ZXI+PGRyaXZlcj5UZWRkeTwvZHJpdmVyPjwvZHJpdmVycz48L2RhdGE+
   
        
   
    
     
     8U42KXyd76FSCmRT
   
       //char 16    
  
   

Processing mode

After receiving the message, the receiver divides the label data into several 8-byte data segments, the last less than 8 bits plus 0x00 8-bit, loop xor or one time. Then compare it with the label data. If the comparison is consistent with the validation, finally the label data is decoded according to Base64, and the transaction plaintext data is obtained.

Reference documents

http://blog.163.com/gene_lu/blog/static/6402542120133151112717/

I found a PHP code on the Internet.

This code has a problem, probably the way.

php
  

'; Var_dump (Base64_decode ($STR)); Echo '


'; Var_dump (Mac_ ($STR)); Echo '


';//Description: Mac algorithm, the string $data is divided into eight bytes of data block, labeled D1,D2,D3, if the last chunk of data is less than//8 bytes, then add \x00 until the length reaches 8 bytes, and then follow the process below to generate the MAC,//8 byte initial value : \x00\x00\x00\x00\x00\x00\x00\x00//process: Initial value XOR or d1-> result of des XOR or d2-> xor result of des XOR or d2-> xor result desfunction ma C_ ($data) {//Set initial value $init = "\x00\x00\x00\x00\x00\x00\x00\x00"; The $data is divided into 8 bytes, and the array $tmp _data=strtoarray ($data, 8) are obtained. $tmp _init= $init; foreach ($tmp _data as $value) {//XOR $tmp _init=myencrypt ($tmp _init, $value); Var_dump ($tmp _init); Encrypt//$tmp _init=mcrypt_str ($tmp _init); } var_dump ($tmp _init); Return Bin2Hex ($tmp _init);} Function Name: strtoarray//Description: The string is cut by 8 bytes, and the last less than 8 bytes of \x00 fill function Strtoarray ($str, $limit) {$len =strlen ($STR); $tmp _arr=array (); $count =floor ($len/$limit); $left _num= $len-$count * $LIMIT; for ($i =0; $i < $left _num; $i + +) {$str. = "\x00"; } for ($i =0; $i < $count; $i + +) {$tmp _arr[]=substr ($str, 0, $limit); if (strlen ($STR) > $limit + 1) {$str =substr ($str, $limit, strlen ($STR)); }} return $tmp _arr;} Function Name: myencrypt//Description: Xor operator function Myencrypt ($string, $key) {for ($i =0; $i

I do not know who has studied, how to do, no clue.

It's all binary, PHP, it's too laborious. Why not use Java for

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.