12306曾经的车站名扰码算法

12306曾经按车站名查询余票,对站名做了编码,估计是防止别人直接使用余票查询功能。

这个算不上加密,不过也确实防止了被民间盗用。

编码是用js实现的,函数名还比较有欺骗性,还好我无意中发现了并翻译了JAVA和Objective-c两个版本

代码没什么难度,不过靠这个功能使我这个余票功能的APK单日达到了8000多得下载量

JAVA版本

public static String tranCode(String str, String pwd) {
        String prand = "";
        for (int i = 0; i < pwd.length(); i++) {
            prand += ((int) pwd.charAt(i));
        }

        int sPos = (int) Math.floor(prand.length() / 5);

        long mult = Integer.parseInt(String.valueOf(prand.charAt(sPos)) + String.valueOf(prand.charAt(sPos * 2)) + String.valueOf(prand.charAt(sPos * 3)) + String.valueOf(prand.charAt(sPos * 4)) + String.valueOf(prand.charAt(sPos * 5)));

        int incr = (int) Math.ceil(pwd.length() / 2);

        int modu = (int) Math.pow(2, 31);

        //int salt = (int)(Math.round(Math.random() * 1000000000) % 100000000);
        int salt = 25988303;

        prand += salt;


        prand = String.valueOf((mult * prand.length() + incr) % modu);

        int enc_chr = 0;
        String enc_str = "";


        for (int i = 0; i < str.length(); i++) {

            enc_chr = (int) (str.charAt(i)) ^ (int) ((Double.parseDouble(prand) / modu) * 255);

            if (enc_chr < 16) {
                enc_str += "0" + Integer.toHexString(enc_chr);
            } else
                enc_str += Integer.toHexString(enc_chr);


            prand = String.valueOf((mult * Integer.parseInt(prand) + incr) % modu);

        }

        String hexsalt = Integer.toHexString(salt);

        while (hexsalt.length() < 8)
            hexsalt = "0" + hexsalt;
        enc_str += hexsalt;

        return enc_str;

    }

Objective-C 版本

-(NSString *)tranCode:(NSString *)str{
    NSMutableString *prand = [[NSMutableString alloc] init ];
    NSString *pwd = @"*******";
    for(int i=0;i<pwd.length;i++){
        unichar ch = [pwd characterAtIndex:i];
        [prand appendFormat:@"%d",ch];
    }
    //NSLog(@"%@",prand);
    int sPos = floor(prand.length/5);
    //NSLog(@"sPos=%d",sPos);
    NSMutableString *multStr = [[NSMutableString alloc] init];
    
    [multStr appendFormat:@"%@",[prand substringWithRange:NSMakeRange(sPos, 1)]];
    [multStr appendFormat:@"%@",[prand substringWithRange:NSMakeRange(sPos*2, 1)]];
    [multStr appendFormat:@"%@",[prand substringWithRange:NSMakeRange(sPos*3, 1)]];
    [multStr appendFormat:@"%@",[prand substringWithRange:NSMakeRange(sPos*4, 1)]];
    [multStr appendFormat:@"%@",[prand substringWithRange:NSMakeRange(sPos*5, 1)]];
    
    //NSLog(@"multStr=%@",multStr);
    
    int incr =  ceil(pwd.length / 2);
    //NSLog(@"incr=%d",incr);
    
    int modu = 2147483647;//fabs(pow(2, 31));
    //NSLog(@"modu=%d",modu);
    int salt = 25988303;
    
    [prand appendFormat:@"%d",salt];
    //NSLog(@"prand=%@",prand);
    
    //prand = String.valueOf((mult * prand.length() + incr) % modu);
    
    unsigned long mult = [multStr longLongValue];
    //NSLog(@"mult=%ld",mult);
    long a = (mult*prand.length+incr)%modu;
    //NSLog(@"%ld",a);
    NSString *prandStr = [NSString stringWithFormat:@"%ld",a];
    NSString *prandStr2 = [NSString stringWithFormat:@"%@",prandStr];
    //NSLog(@"prandStr2=%@",prandStr2);
    int enc_chr = 0;
    NSMutableString *enc_str = [[NSMutableString alloc] init ];
    
    //NSLog(@"str len %d",str.length);
    for (int i = 0; i < str.length; i++) {
        //NSLog(@"prandStr2=%@",prandStr2);
        enc_chr = (int) ([str characterAtIndex:i]) ^ ( int) (([prandStr2 doubleValue]/ modu) * 255);
        
        //NSLog(@"([str characterAtIndex:i])=%d",(int) ([str characterAtIndex:i]) );
        //NSLog(@" (([prandStr doubleValue]/ modu) * 255)=%f  %d",[prandStr2 doubleValue], (unsigned int)(([prandStr2 doubleValue]/ modu)*255));
        
         //NSLog(@"enc_chr=%d",enc_chr);
        //NSLog(@"%@",multStr);
        if (enc_chr < 16) {
            //enc_str += "0" + Integer.toHexString(enc_chr);
            [enc_str appendFormat:@"%@",@"0"];
            [enc_str appendFormat:@"%0x",enc_chr];
        } else{
            //enc_str += Integer.toHexString(enc_chr);
            [enc_str appendFormat:@"%0x",enc_chr];
        }
        
        //NSLog(@" (([prandStr  =%@   %d",prandStr2,i);
        
        prandStr2 = [NSString stringWithFormat:@"%lu",((mult * [prandStr2 intValue] + incr) % modu)];
        //NSLog(@" (([prandStr eee =%@   %d",prandStr2,i);
    }
    //NSLog(@"enc_str %@",enc_str);
    
    NSString *hex = [NSString stringWithFormat:@"%x",salt];
    NSMutableString *hexsalt = [[NSMutableString alloc] init ];
    [hexsalt appendFormat:@"%@",hex];
    
    while (hexsalt.length < 8)
        [hexsalt insertString:@"0" atIndex:0];
    [enc_str appendFormat:hexsalt];
    NSLog(@"%@",enc_str);

    return enc_str;
}

 

14 Comments

  1. calculation

    This article will assist the internet users for setting up new web site or even a blog from start to end.|

    Reply
  2. Health News

    My brother recommended I might like this blog. He was totally right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!

    Reply
  3. Legal

    My spouse and i felt so cheerful Michael managed to finish off his investigation by way of the precious recommendations he gained in your web page. It is now and again perplexing to just always be handing out secrets which usually other folks could have been trying to sell. Therefore we understand we need the writer to be grateful to for this. These illustrations you have made, the simple website menu, the relationships you assist to foster – it’s got mostly sensational, and it’s helping our son and our family consider that this topic is entertaining, and that is exceedingly serious. Thank you for the whole lot!

    Reply
  4. outsourcing

    Hello! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My web site looks weird when browsing from my iphone4. I’m trying to find a theme or plugin that might be able to correct this problem. If you have any recommendations, please share. With thanks!

    Reply
  5. IvlevEmola

    cialis generika zoll

    [url=http://cheapcialismsz.com/]buy cialis online[/url]

    cialis price

    generische cialis online

    Reply
  6. Car Rentals

    you’re really a excellent webmaster. The website loading speed is amazing. It seems that you’re doing any unique trick. Moreover, The contents are masterpiece. you’ve done a wonderful job in this matter!

    Reply
  7. Education and Training

    Wow! Thank you! I constantly needed to write on my website something like that. Can I implement a part of your post to my website?

    Reply
  8. Travel International

    I have recently started a site, the information you provide on this website has helped me greatly. Thank you for all of your time & work. “If you see a snake, just kill it. Don’t appoint a committee on snakes.” by H. Ross Perot.

    Reply
  9. Education and Training

    I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an impatience over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly very often inside case you shield this hike.

    Reply
  10. Car & Automotive

    It¡¦s actually a nice and helpful piece of information. I¡¦m happy that you simply shared this helpful information with us. Please keep us informed like this. Thank you for sharing.

    Reply
  11. Auto Care and Service

    Awsome blog! I am loving it!! Will come back again. I am taking your feeds also

    Reply

Leave a Comment

电子邮件地址不会被公开。 必填项已用*标注