iTV android与客户端加密交互流程

来源:互联网 发布:php输出当前时间 编辑:程序博客网 时间:2024/05/19 13:07

文章来源:http://blog.csdn.net/lx554968634/article/details/21399137


itv手机App,充值后台加密流程1、对于一般情况下一个电视账户应该对应有3个手机app账户。2、每一个手机app账户将会对应一个支付随机数,该随机数将会在手机绑定或者是登陆时更新4、在手机支付宝交易成功后,想后台请求加钱的函数具有4个参数。辅助参数为支付随机数 randomNum

a、UserId :对应的电视账户的uidb、type :对应的交易成功的类型。现阶段有6个分别为1 , 2, 3 ,4 , 5, 6元。c、对应支付宝交易成功的订单号。d、md5效验码、



校验过程是:手机端,实际参数 sParam 先和随机数组合为新的字符串 值A,将值A进行md5加密,作为第二个参数。值A进行DES加密作为第一个参数。
后台:捕获参数后,客户端拼凑的DES加密值A 解密获得 值B, 在进行MD5加密,比对参数中的md5值,从而整个流程上面完成校验工作




C# MD5代码

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static string MD5(string encryptString)  
  2.        {  
  3.            byte[] result = Encoding.Default.GetBytes(encryptString);  
  4.            MD5 md5 = new MD5CryptoServiceProvider();  
  5.            byte[] output = md5.ComputeHash(result);  
  6.            string encryptResult = BitConverter.ToString(output).Replace("-""");  
  7.            return encryptResult;  
  8.        }  

java MD5代码

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private static final char HEX_DIGITS[] = { '0''1''2''3''4''5',  
  2.             '6''7''8''9''A''B''C''D''E''F' };  
  3.   
  4.     public static String getMd5Code(String localPath) throws Exception {  
  5.         MessageDigest md5 = null;  
  6.         md5 = MessageDigest.getInstance("MD5");  
  7.   
  8.         md5.update(localPath.getBytes());  
  9.         return toHexString(md5.digest());  
  10.     }  
  11.   
  12.     private static String toHexString(byte[] b) {  
  13.         StringBuilder sb = new StringBuilder(b.length * 2);  
  14.         for (int i = 0; i < b.length; i++) {  
  15.             sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);  
  16.             sb.append(HEX_DIGITS[b[i] & 0x0f]);  
  17.         }  
  18.         return sb.toString();  
  19.     }  


C# DES加密 密钥12345678
[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using System.Collections.Generic;  
  7. using System.ComponentModel;  
  8. using System.Data;  
  9. using System.Text;  
  10. using System.Security.Cryptography;//加密部分  
  11. using System.IO;  
  12. namespace ConsoleApplication1  
  13. {  
  14.     class Program  
  15.     {  
  16.         static void Main(string[] args)  
  17.         {  
  18.             Console.WriteLine(EncryptDES("price1""12345678"));  
  19.             Console.WriteLine(EncryptDES("price5""12345678"));  
  20.             Console.WriteLine(EncryptDES("price10""12345678"));  
  21.             Console.WriteLine(EncryptDES("price20""12345678"));  
  22.             Console.WriteLine(EncryptDES("price50""12345678"));  
  23.             Console.WriteLine(EncryptDES("price100""12345678"));  
  24.             Console.ReadLine();  
  25.         }  
  26.         private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };  
  27.         public static string DecryptDES(string decryptString, string decryptKey)  
  28.         {  
  29.             try  
  30.             {  
  31.                 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);  
  32.                 byte[] rgbIV = System.Text.Encoding.Default.GetBytes("12345678"); ;  
  33.                 byte[] inputByteArray = Convert.FromBase64String(decryptString);  
  34.                 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();  
  35.                 MemoryStream mStream = new MemoryStream();  
  36.                 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);  
  37.                 cStream.Write(inputByteArray, 0, inputByteArray.Length);  
  38.                 cStream.FlushFinalBlock();  
  39.                 return Encoding.UTF8.GetString(mStream.ToArray());  
  40.             }  
  41.             catch  
  42.             {  
  43.                 return decryptString;  
  44.             }  
  45.         }  
  46.         public static string EncryptDES(string encryptString, string encryptKey)  
  47.         {  
  48.             try  
  49.             {  
  50.                 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));  
  51.                 byte[] rgbIV = System.Text.Encoding.Default.GetBytes("12345678");  
  52.                 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);  
  53.                 DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();  
  54.                 MemoryStream mStream = new MemoryStream();  
  55.                 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);  
  56.                 cStream.Write(inputByteArray, 0, inputByteArray.Length);  
  57.                 cStream.FlushFinalBlock();  
  58.                 return Convert.ToBase64String(mStream.ToArray());  
  59.             }  
  60.             catch  
  61.             {  
  62.                 return encryptString;  
  63.             }  
  64.         }  
  65.     }  
  66. }  

java DES加密 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static String encode(String key,byte[] data) throws Exception    
  2.    {    
  3.        try    
  4.        {    
  5.            DESKeySpec dks = new DESKeySpec(key.getBytes());    
  6.                
  7.            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");    
  8.            //key的长度不能够小于8位字节     
  9.            Key secretKey = keyFactory.generateSecret(dks);    
  10.            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);    
  11.            IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());    
  12.            AlgorithmParameterSpec paramSpec = iv;    
  13.            cipher.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec);    
  14.                
  15.            byte[] bytes = cipher.doFinal(data);    
  16.                
  17.            return Base64.encode(bytes);    
  18.        } catch (Exception e)    
  19.        {    
  20.            throw new Exception(e);    
  21.        }    
  22.    }    
  23.    
  24.    /**  
  25.     * DES算法,解密  
  26.     *  
  27.     * @param data 待解密字符串  
  28.     * @param key  解密私钥,长度不能够小于8位  
  29.     * @return 解密后的字节数组  
  30.     * @throws Exception 异常  
  31.     */    
  32.    public static byte[] decode(String key,byte[] data) throws Exception    
  33.    {    
  34.        try    
  35.        {    
  36.            SecureRandom sr = new SecureRandom();    
  37.            DESKeySpec dks = new DESKeySpec(key.getBytes());    
  38.            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");    
  39.            //key的长度不能够小于8位字节     
  40.            Key secretKey = keyFactory.generateSecret(dks);    
  41.            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);    
  42.            IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());    
  43.            AlgorithmParameterSpec paramSpec = iv;    
  44.            cipher.init(Cipher.DECRYPT_MODE, secretKey,paramSpec);    
  45.            return cipher.doFinal(data);    
  46.        } catch (Exception e)    
  47.        {    
  48.            throw new Exception(e);    
  49.        }    
  50.    }    
  51.        
  52.    /**  
  53.     * 获取编码后的值  
  54.     * @param key  
  55.     * @param data  
  56.     * @return  
  57.     * @throws Exception  
  58.     */    
  59.    public static String decodeValue(String key,String data)     
  60.    {    
  61.        byte[] datas;    
  62.        String value = null;    
  63.        try {    
  64.            if(System.getProperty("os.name") != null && (System.getProperty("os.name").equalsIgnoreCase("sunos") || System.getProperty("os.name").equalsIgnoreCase("linux")))    
  65.            {    
  66.                datas = decode(key, Base64.decode(data));    
  67.            }    
  68.            else    
  69.            {    
  70.                datas = decode(key, Base64.decode(data));    
  71.            }    
  72.                
  73.            value = new String(datas);    
  74.        } catch (Exception e) {    
  75.            value = "";    
  76.        }    
  77.        return value;    
  78.    }    
辅助类Base64.java

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * Copyright (C) 2010 The MobileSecurePay Project 
  3.  * All right reserved. 
  4.  * author: shiqun.shi@alipay.com 
  5.  */  
  6.   
  7.   
  8.   
  9. public final class Base64 {  
  10.   
  11.     static private final int BASELENGTH = 128;  
  12.     static private final int LOOKUPLENGTH = 64;  
  13.     static private final int TWENTYFOURBITGROUP = 24;  
  14.     static private final int EIGHTBIT = 8;  
  15.     static private final int SIXTEENBIT = 16;  
  16.     static private final int FOURBYTE = 4;  
  17.     static private final int SIGN = -128;  
  18.     static private final char PAD = '=';  
  19.     static private final boolean fDebug = false;  
  20.     static final private byte[] base64Alphabet = new byte[BASELENGTH];  
  21.     static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH];  
  22.   
  23.     static {  
  24.         for (int i = 0; i < BASELENGTH; ++i) {  
  25.             base64Alphabet[i] = -1;  
  26.         }  
  27.         for (int i = 'Z'; i >= 'A'; i--) {  
  28.             base64Alphabet[i] = (byte) (i - 'A');  
  29.         }  
  30.         for (int i = 'z'; i >= 'a'; i--) {  
  31.             base64Alphabet[i] = (byte) (i - 'a' + 26);  
  32.         }  
  33.   
  34.         for (int i = '9'; i >= '0'; i--) {  
  35.             base64Alphabet[i] = (byte) (i - '0' + 52);  
  36.         }  
  37.   
  38.         base64Alphabet['+'] = 62;  
  39.         base64Alphabet['/'] = 63;  
  40.   
  41.         for (int i = 0; i <= 25; i++) {  
  42.             lookUpBase64Alphabet[i] = (char) ('A' + i);  
  43.         }  
  44.   
  45.         for (int i = 26, j = 0; i <= 51; i++, j++) {  
  46.             lookUpBase64Alphabet[i] = (char) ('a' + j);  
  47.         }  
  48.   
  49.         for (int i = 52, j = 0; i <= 61; i++, j++) {  
  50.             lookUpBase64Alphabet[i] = (char) ('0' + j);  
  51.         }  
  52.         lookUpBase64Alphabet[62] = (char'+';  
  53.         lookUpBase64Alphabet[63] = (char'/';  
  54.   
  55.     }  
  56.   
  57.     private static boolean isWhiteSpace(char octect) {  
  58.         return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);  
  59.     }  
  60.   
  61.     private static boolean isPad(char octect) {  
  62.         return (octect == PAD);  
  63.     }  
  64.   
  65.     private static boolean isData(char octect) {  
  66.         return (octect < BASELENGTH && base64Alphabet[octect] != -1);  
  67.     }  
  68.   
  69.     /** 
  70.      * Encodes hex octects into Base64 
  71.      *  
  72.      * @param binaryData 
  73.      *            Array containing binaryData 
  74.      * @return Encoded Base64 array 
  75.      */  
  76.     public static String encode(byte[] binaryData) {  
  77.   
  78.         if (binaryData == null) {  
  79.             return null;  
  80.         }  
  81.   
  82.         int lengthDataBits = binaryData.length * EIGHTBIT;  
  83.         if (lengthDataBits == 0) {  
  84.             return "";  
  85.         }  
  86.   
  87.         int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;  
  88.         int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;  
  89.         int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1  
  90.                 : numberTriplets;  
  91.         char encodedData[] = null;  
  92.   
  93.         encodedData = new char[numberQuartet * 4];  
  94.   
  95.         byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;  
  96.   
  97.         int encodedIndex = 0;  
  98.         int dataIndex = 0;  
  99.         if (fDebug) {  
  100.             System.out.println("number of triplets = " + numberTriplets);  
  101.         }  
  102.   
  103.         for (int i = 0; i < numberTriplets; i++) {  
  104.             b1 = binaryData[dataIndex++];  
  105.             b2 = binaryData[dataIndex++];  
  106.             b3 = binaryData[dataIndex++];  
  107.   
  108.             if (fDebug) {  
  109.                 System.out.println("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3);  
  110.             }  
  111.   
  112.             l = (byte) (b2 & 0x0f);  
  113.             k = (byte) (b1 & 0x03);  
  114.   
  115.             byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)  
  116.                     : (byte) ((b1) >> 2 ^ 0xc0);  
  117.             byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)  
  118.                     : (byte) ((b2) >> 4 ^ 0xf0);  
  119.             byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6)  
  120.                     : (byte) ((b3) >> 6 ^ 0xfc);  
  121.   
  122.             if (fDebug) {  
  123.                 System.out.println("val2 = " + val2);  
  124.                 System.out.println("k4   = " + (k << 4));  
  125.                 System.out.println("vak  = " + (val2 | (k << 4)));  
  126.             }  
  127.   
  128.             encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];  
  129.             encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];  
  130.             encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3];  
  131.             encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f];  
  132.         }  
  133.   
  134.         // form integral number of 6-bit groups  
  135.         if (fewerThan24bits == EIGHTBIT) {  
  136.             b1 = binaryData[dataIndex];  
  137.             k = (byte) (b1 & 0x03);  
  138.             if (fDebug) {  
  139.                 System.out.println("b1=" + b1);  
  140.                 System.out.println("b1<<2 = " + (b1 >> 2));  
  141.             }  
  142.             byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)  
  143.                     : (byte) ((b1) >> 2 ^ 0xc0);  
  144.             encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];  
  145.             encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];  
  146.             encodedData[encodedIndex++] = PAD;  
  147.             encodedData[encodedIndex++] = PAD;  
  148.         } else if (fewerThan24bits == SIXTEENBIT) {  
  149.             b1 = binaryData[dataIndex];  
  150.             b2 = binaryData[dataIndex + 1];  
  151.             l = (byte) (b2 & 0x0f);  
  152.             k = (byte) (b1 & 0x03);  
  153.   
  154.             byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)  
  155.                     : (byte) ((b1) >> 2 ^ 0xc0);  
  156.             byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)  
  157.                     : (byte) ((b2) >> 4 ^ 0xf0);  
  158.   
  159.             encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];  
  160.             encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];  
  161.             encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2];  
  162.             encodedData[encodedIndex++] = PAD;  
  163.         }  
  164.   
  165.         return new String(encodedData);  
  166.     }  
  167.   
  168.     /** 
  169.      * Decodes Base64 data into octects 
  170.      *  
  171.      * @param encoded 
  172.      *            string containing Base64 data 
  173.      * @return Array containind decoded data. 
  174.      */  
  175.     public static byte[] decode(String encoded) {  
  176.   
  177.         if (encoded == null) {  
  178.             return null;  
  179.         }  
  180.   
  181.         char[] base64Data = encoded.toCharArray();  
  182.         // remove white spaces  
  183.         int len = removeWhiteSpace(base64Data);  
  184.   
  185.         if (len % FOURBYTE != 0) {  
  186.             return null;// should be divisible by four  
  187.         }  
  188.   
  189.         int numberQuadruple = (len / FOURBYTE);  
  190.   
  191.         if (numberQuadruple == 0) {  
  192.             return new byte[0];  
  193.         }  
  194.   
  195.         byte decodedData[] = null;  
  196.         byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;  
  197.         char d1 = 0, d2 = 0, d3 = 0, d4 = 0;  
  198.   
  199.         int i = 0;  
  200.         int encodedIndex = 0;  
  201.         int dataIndex = 0;  
  202.         decodedData = new byte[(numberQuadruple) * 3];  
  203.   
  204.         for (; i < numberQuadruple - 1; i++) {  
  205.   
  206.             if (!isData((d1 = base64Data[dataIndex++]))  
  207.                     || !isData((d2 = base64Data[dataIndex++]))  
  208.                     || !isData((d3 = base64Data[dataIndex++]))  
  209.                     || !isData((d4 = base64Data[dataIndex++]))) {  
  210.                 return null;  
  211.             }// if found "no data" just return null  
  212.   
  213.             b1 = base64Alphabet[d1];  
  214.             b2 = base64Alphabet[d2];  
  215.             b3 = base64Alphabet[d3];  
  216.             b4 = base64Alphabet[d4];  
  217.   
  218.             decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);  
  219.             decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));  
  220.             decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);  
  221.         }  
  222.   
  223.         if (!isData((d1 = base64Data[dataIndex++]))  
  224.                 || !isData((d2 = base64Data[dataIndex++]))) {  
  225.             return null;// if found "no data" just return null  
  226.         }  
  227.   
  228.         b1 = base64Alphabet[d1];  
  229.         b2 = base64Alphabet[d2];  
  230.   
  231.         d3 = base64Data[dataIndex++];  
  232.         d4 = base64Data[dataIndex++];  
  233.         if (!isData((d3)) || !isData((d4))) {// Check if they are PAD characters  
  234.             if (isPad(d3) && isPad(d4)) {  
  235.                 if ((b2 & 0xf) != 0)// last 4 bits should be zero  
  236.                 {  
  237.                     return null;  
  238.                 }  
  239.                 byte[] tmp = new byte[i * 3 + 1];  
  240.                 System.arraycopy(decodedData, 0, tmp, 0, i * 3);  
  241.                 tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);  
  242.                 return tmp;  
  243.             } else if (!isPad(d3) && isPad(d4)) {  
  244.                 b3 = base64Alphabet[d3];  
  245.                 if ((b3 & 0x3) != 0)// last 2 bits should be zero  
  246.                 {  
  247.                     return null;  
  248.                 }  
  249.                 byte[] tmp = new byte[i * 3 + 2];  
  250.                 System.arraycopy(decodedData, 0, tmp, 0, i * 3);  
  251.                 tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);  
  252.                 tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));  
  253.                 return tmp;  
  254.             } else {  
  255.                 return null;  
  256.             }  
  257.         } else { // No PAD e.g 3cQl  
  258.             b3 = base64Alphabet[d3];  
  259.             b4 = base64Alphabet[d4];  
  260.             decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);  
  261.             decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));  
  262.             decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);  
  263.   
  264.         }  
  265.   
  266.         return decodedData;  
  267.     }  
  268.   
  269.     /** 
  270.      * remove WhiteSpace from MIME containing encoded Base64 data. 
  271.      *  
  272.      * @param data 
  273.      *            the byte array of base64 data (with WS) 
  274.      * @return the new length 
  275.      */  
  276.     private static int removeWhiteSpace(char[] data) {  
  277.         if (data == null) {  
  278.             return 0;  
  279.         }  
  280.   
  281.         // count characters that's not whitespace  
  282.         int newSize = 0;  
  283.         int len = data.length;  
  284.         for (int i = 0; i < len; i++) {  
  285.             if (!isWhiteSpace(data[i])) {  
  286.                 data[newSize++] = data[i];  
  287.             }  
  288.         }  
  289.         return newSize;  
  290.     }  
  291. }  

如果有问题请指正,还有很多漏洞需要补齐。



0 0