解密算法一

一、ICCID 解密算法

//函数: static u8 HuaweiGenPICPSW(ascii *sSrc, u8 nSrcLen, ascii *sDec, u8 nDecLen)
//变量说明:
//函数返回值:-1,失败;TRUE,成功。
// ascii *sSrc: 源数据,待解密的ICCID码内容
// u8 nSrcLen: 源数据长度,待解密的ICCID码长度
// ascii *sDec: 目标数据,生成的解密码内容
// u8 nDecLen:目标数据长度,生成的解密码长度

//常量:
//HUAWEI_SPE_ICCID_LEN: ICCID码长度为15位
// HUAWEI_SPE_PIC_PSW_LEN: 解密码长度为14位


#define HUAWEI_SPE_ICCID_LEN 15
#define HUAWEI_SPE_PIC_PSW_LEN 14

static u8 HuaweiGenPICPSW(ascii *sSrc, u8 nSrcLen, ascii *sDec, u8 nDecLen)
{
    u8 i = 0;
    u8 l = 0;
    u8 tmp = 0;
    u8 vPSW[HUAWEI_SPE_PIC_PSW_LEN+1] = {0};
    u32 data1 = 0, data2 = 0, decData1 = 0, decData2;
    static u8 seed[] = "";  //密钥字符串,正式生成系统时,通过电话知会。
    if ((!sSrc) || (!sDec)||
        (HUAWEI_SPE_ICCID_LEN > nSrcLen) || ((HUAWEI_SPE_PIC_PSW_LEN+1) > nDecLen))
    {
        return -1;
    }
    i = 1;/*前1 位跳过*/
    while(i<HUAWEI_SPE_ICCID_LEN)
    {
        vPSW[l] = sSrc[i]^seed[l];
        if ((sSrc[i] > '9') || (sSrc[i] < '0'))
        {
            /*把非数字字符转换成数字*/
            vPSW[l] = sSrc[i]&0x07;
        }
        else
        {
            /*与seed进行异或*/
            vPSW[l] = sSrc[i]^seed[l];
        }
        /*转换成数值前,保证最高位非0*/
        if (vPSW[0] == '0')
        {
            vPSW[0] = '2';
        }
        if (vPSW[(HUAWEI_SPE_PIC_PSW_LEN/2)] == '0')
        {
            vPSW[(HUAWEI_SPE_PIC_PSW_LEN/2)] = '6';
        }
        i++;l++;
    }
    Cal4_509output_uart2("vPSW = %s\n", vPSW);
    i = 0;
    /*把14位字符串转换成两个数值*/
    while(i<(HUAWEI_SPE_PIC_PSW_LEN/2))
    {
        data2 = data2*10+(vPSW[i] - '0');
        data1 = data1*10+(vPSW[i+(HUAWEI_SPE_PIC_PSW_LEN/2)] - '0');
        i++;
    }
    /*对数值进行与,异或*/
    decData1 = data1&data2;
    decData2 = data1^data2;
    Cal4_509output_uart2("data1 = %d, data2 = %d\n", data1, data2);
    Cal4_509output_uart2("decData1 = %d, decData2 = %d\n", decData1, decData2);
    i= 0;
    /*再把数值转换成字符串*/
    while(i<(HUAWEI_SPE_PIC_PSW_LEN/2))
    {
        tmp = (u8)(decData1%10);
        sDec[i] = tmp + '0';
        decData1 = decData1/10;

        tmp = (u8)(decData2%10);
        sDec[i+(HUAWEI_SPE_PIC_PSW_LEN/2)] = tmp + '0';
        decData2 = decData2/10;

        i++;
    }
   
    sDec[HUAWEI_SPE_PIC_PSW_LEN] = '\0';
    Cal4_509output_uart2("----sDec = %s\n", sDec);
    return TRUE;
}

你可能感兴趣的:(算法)