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;
}

 

发表评论

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