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

 

1,225 thoughts on “12306曾经的车站名扰码算法”

  1. [url=http://acyclovir.run/]buy zovirax[/url] [url=http://doxycycline.us.org/]Doxycycline[/url] [url=http://synthroid.irish/]synthroid[/url] [url=http://cephalexin.irish/]cephalexin[/url] [url=http://wellbutrin.institute/]wellbutrin[/url] [url=http://retina.recipes/]retin-a[/url] [url=http://valtrex.wtf/]valtrex[/url] [url=http://gracefulurls.com/]Metformin[/url]

  2. [url=http://buywellbutrin.company/]wellbutrin online[/url] [url=http://prozacforyou.us.com/]buy prozac[/url] [url=http://medrol.team/]solu medrol[/url] [url=http://tadalafil.rodeo/]tadalafil[/url] [url=http://erythromycin.team/]erythromycin[/url]

  3. [url=http://viagrasoft.team/]viagra soft[/url] [url=http://arimidex.team/]arimidex[/url] [url=http://1stazithromycinnow.com/]azithromycin[/url] [url=http://1stacyclovirnow.com/]acyclovir 400mg[/url] [url=http://lexapro.yoga/]lexapro[/url] [url=http://cefixime.team/]cefixime 400 mg buy online[/url] [url=http://effexor.us.com/]effexor 75mg[/url] [url=http://tadalafil.rodeo/]tadalafil prescription[/url] [url=http://valtrex.yoga/]where to buy valtrex online[/url] [url=http://propecia.best/]propecia[/url]

  4. [url=http://buylipitor.us.com/]lipitor online[/url] [url=http://metformin365.us.org/]price of metformin[/url] [url=http://places-to-visit.org/]buy hydrochlorothiazide[/url]

  5. [url=http://buyalbuterol.ooo/]buy albuterol[/url] [url=http://hydrochlorothiazide.institute/]hydrochlorothiazide[/url] [url=http://acyclovir.us.com/]acyclovir[/url] [url=http://erythromycin.us.org/]ilosone[/url] [url=http://allopurinol.institute/]allopurinol[/url] [url=http://propecia4you.us.com/]propecia 5 mg for sale[/url] [url=http://amoxicillin.run/]amoxicillin[/url] [url=http://metformin.wtf/]metformin er 1000 mg[/url] [url=http://diflucan.recipes/]continued[/url] [url=http://azithromycin.wtf/]azithromycin[/url]

  6. [url=http://cephalexin.us.org/]cephalexin online[/url] [url=http://baclofen.run/]buy baclofen online[/url] [url=http://erythromycin.company/]erythromycin[/url] [url=http://allopurinol.ooo/]allopurinol drug[/url] [url=http://rbstfacts.org/]furosemide[/url] [url=http://ventolin.wtf/]proventil inhaler for sale[/url] [url=http://orlistat.company/]disgrasil orlistat 120 mg[/url]

  7. [url=http://michelletrachtenberg.org/]buy albendazole[/url] [url=http://traitsjs.org/]indocin 50 mg tablets[/url] [url=http://abilify.irish/]abilify[/url] [url=http://azithromycin.wtf/]cheap azithromycin[/url] [url=http://generickamagra.company/]kamagra 100mg oral jelly[/url] [url=http://wellbutrinxr.com/]wellbutrin without a prescription[/url] [url=http://prednisone365.us.org/]prednisone[/url] [url=http://albuterol.wtf/]albuterol mdi[/url]

  8. [url=http://tadacip.company/]tadacip[/url] [url=http://buytenormin.us.com/]buy tenormin[/url] [url=http://cialis18.us.com/]cialis[/url] [url=http://markcrozermusic.com/]buy wellbutrin[/url] [url=http://antihypertensionmeds.com/]Furosemide[/url] [url=http://tadacip.us.org/]tadacip[/url] [url=http://genericviagraprofessional100mg.com/]viagra cost per pill[/url]

  9. [url=http://femaleviagrawithoutprescription.com/]Viagra For Woman[/url] [url=http://buylevaquin.us.com/]Levaquin Without A Prescription[/url]

  10. [url=http://umransociety.org/]trazodone[/url] [url=http://cafergot.wtf/]cafergot[/url] [url=http://augmentin.company/]buy generic augmentin[/url] [url=http://hammerhorrorposters.com/]synthroid[/url] [url=http://robaxin.us.com/]robaxin online[/url]

  11. [url=http://tadalafil.irish/]tadalafil[/url] [url=http://genericvaltrex.company/]generic valtrex[/url] [url=http://atenolol.wtf/]atenolol[/url] [url=http://wellbutrin.wtf/]wellbutrin[/url]

  12. [url=http://buspar.company/]buspar[/url] [url=http://amoxicillin.run/]buy amoxicillin 500mg[/url] [url=http://117th-cav.org/]lisinopril[/url] [url=http://baclofen.club/]baclofen[/url] [url=http://hydrochlorothiazide.institute/]hydrochlorothiazide triamterene[/url] [url=http://buyclindamycin.us.org/]clindamycin hydrochloride[/url] [url=http://baclofen.institute/]baclofen[/url] [url=http://zithromax.club/]zithromax[/url] [url=http://buyventolin.ooo/]buy ventolin inhaler[/url] [url=http://cytotec.us.org/]buy cytotec pills[/url] [url=http://saemedargentina.net/]buy propranolol for anxiety[/url] [url=http://genericalbuterol.company/]albuterol[/url] [url=http://kamagra365.us.org/]kamagra soft[/url]