Une fois le langage C RC4 du périphérique sous-jacent chiffré, pourquoi Java ne peut-il pas utiliser rc4 pour déchiffrer, alors que les programmes PHP d'autres personnes peuvent déchiffrer avec succès ?
PHP中文网
PHP中文网 2017-06-17 09:15:15
0
1
1174

Le périphérique sous-jacent est une sonde wifi qui transmet régulièrement les données à un service. Cette adresse de service reçoit les données et utilise rc4 pour les déchiffrer.
Le cryptage rc4 du périphérique sous-jacent est écrit en langage C.

récepteur Java

@RequestMapping("/acc")
public void acc1(HttpServletRequest request,HttpServletResponse response) throws Exception{
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    System.out.println(sdf.format(new Date())+"-->post begin.......");
     BufferedReader reader = new BufferedReader(
             new InputStreamReader(request.getInputStream(),"UTF-8"));
     String line=null;
     StringBuilder buffer = new StringBuilder();
     while((line = reader.readLine())!=null){
             buffer.append(line);
           }
     System.out.println("recive:   "+buffer.toString());
     String key = buffer.toString().substring(0, 16);
     String content =  buffer.toString().substring(16);
     System.out.println("before 16 string:    "+key);
     System.out.println("content :    "+content);
     System.out.println("decrypt :       "+RC4.HloveyRC4(content, key));
     
    System.out.println(sdf.format(new Date())+"-->post end.......");
}

Programme de décryptage Java rc4 :
classe publique RC4 {

public static String HloveyRC4(String aInput,String aKey) {   
    int[] iS = new int[256];   
    byte[] iK = new byte[256];   
    for (int i=0;i<256;i++)   
        iS[i]=i;   
    int j = 1;   
    for (short i= 0;i<256;i++) {   
        iK[i]=(byte)aKey.charAt((i % aKey.length()));   
    }   
    j=0;   
    for (int i=0;i<255;i++){   
        j=(j+iS[i]+iK[i]) % 256;   
        int temp = iS[i];   
        iS[i]=iS[j];   
        iS[j]=temp;   
    }   
    int i=0; j=0;   
    char[] iInputChar = aInput.toCharArray();   
    char[] iOutputChar = new char[iInputChar.length];   
    for(short x = 0;x<iInputChar.length;x++) {   
        i = (i+1) % 256;   
        j = (j+iS[i]) % 256;   
        int temp = iS[i];   
        iS[i]=iS[j];   
        iS[j]=temp;   
        int t = (iS[i]+(iS[j] % 256)) % 256;   
        int iY = iS[t];   
        char iCY = (char)iY;   
        iOutputChar[x] =(char)( iInputChar[x] ^ iCY) ;      
    }   
    return new String(iOutputChar);   
}  }


Le programme de décryptage php peut décrypter avec succès
$json=file_get_contents('php://input');

if ($json) {

$time_str=date('YmdHms',time());

//开始RC4解密
$key = substr($json,0,16);                               
$json = rc4($key, substr($json,16));

$json=mb_convert_encoding($json,"UTF-8","auto");

}

fonction rc4($pwd, $data)//$pwd key $data doit être une chaîne chiffrée
{

    $key[] ="";
    $box[] ="";
    $cipher="";
    $pwd_length = strlen($pwd);
    $data_length = strlen($data);
 
    for ($i = 0; $i < 256; $i++)
    {
        $key[$i] = ord($pwd[$i % $pwd_length]);
        $box[$i] = $i;
    }
 
    for ($j = $i = 0; $i < 256; $i++)
    {
        $j = ($j + $box[$i] + $key[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
 
    for ($a = $j = $i = 0; $i < $data_length; $i++)
    {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
 
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
 
        $k = $box[(($box[$a] + $box[$j]) % 256)];
        $cipher .= chr(ord($data[$i]) ^ $k);
    }
   // var_dump($cipher);
    //exit();
    return $cipher;

}

Les données déchiffrées par la console Java sont tronquées

Pourquoi le décryptage Java échoue-t-il ? J'ai essayé différents codages pour obtenir les données, mais ils étaient tous tronqués après le décryptage.

PHP中文网
PHP中文网

认证高级PHP讲师

répondre à tous(1)
为情所困

Ne pouvons-nous pas utiliser le RC4 de JCE pour décrypter ? De plus, les clés des deux côtés sont-elles correctes ?

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!