2012-11-30 16 views
5

चलो कहते हैं कि मैं एक सादा पाठ दूध चाय, जो कुंजी साथ XOR सिफर एड होने जा रहा है का एक अच्छा कप करते हैं।XOR सिफर: अलग-अलग परिणाम

यह जावा कोड:

एनक्रिप्टेड:

import sun.misc.BASE64Encoder; 

import sun.misc.BASE64Decoder; 

public class XORTest { 

    public static void main(String args[]){ 

    String plaintext = "a nice cup of milk tea"; 
    String key = "12345"; 
    String encrypted = xor_encrypt(plaintext, key); 
    String decrypted = xor_decrypt(encrypted, key); 
    System.out.println("Encrypted: "+encrypted); 
    System.out.println("Decrypted: "+decrypted); 
    } 

    public static String xor_encrypt(String message, String key){ 
    try { 
     if (message==null || key==null) return null; 

     char[] keys=key.toCharArray(); 
     char[] mesg=message.toCharArray(); 
     BASE64Encoder encoder = new BASE64Encoder(); 

     int ml=mesg.length; 
     int kl=keys.length; 
     char[] newmsg=new char[ml]; 

     for (int i=0; i<ml; i++){ 
     newmsg[i]=(char)(mesg[i]^keys[i%kl]); 
     } 
     mesg=null; 
     keys=null; 
     String temp = new String(newmsg); 
     return new String(new BASE64Encoder().encodeBuffer(temp.getBytes())); 
    } 
    catch (Exception e) { 
     return null; 
    } 
    } 


    public static String xor_decrypt(String message, String key){ 
    try { 
     if (message==null || key==null) return null; 
     BASE64Decoder decoder = new BASE64Decoder(); 
     char[] keys=key.toCharArray(); 
     message = new String(decoder.decodeBuffer(message)); 
     char[] mesg=message.toCharArray(); 

     int ml=mesg.length; 
     int kl=keys.length; 
     char[] newmsg=new char[ml]; 

     for (int i=0; i<ml; i++){ 
     newmsg[i]=(char)(mesg[i]^keys[i%kl]); 
     } 
     mesg=null; keys=null; 
     return new String(newmsg); 
    } 
    catch (Exception e) { 
     return null; 
    } 
    }} 

मुझे देता है UBJdXVZUElBBRRFdVRRYWF5YFEFUUw ==

Decrypted: दूध चाय

और यह पीएचपी का एक अच्छा कप कोड:

<?php 

$input = "a nice cup of milk tea"; 
$key = "12345"; 
$encrypted = XOR_encrypt($input, $key); 
$decrypted = XOR_decrypt($encrypted, $key); 

echo "Encrypted: " . $encrypted . "<br>"; 
echo "Decrypted: " . $decrypted . "<br>"; 

function XOR_encrypt($message, $key){ 
    $ml = strlen($message); 
    $kl = strlen($key); 
    $newmsg = ""; 

    for ($i = 0; $i < $ml; $i++){ 
    $newmsg = $newmsg . ($msg[$i]^$key[$i % $kl]); 
    } 

    return base64_encode($newmsg); 
} 

function XOR_decrypt($encrypted_message, $key){ 
    $msg = base64_decode($encrypted_message); 
    $ml = strlen($msg); 
    $kl = strlen($key); 
    $newmsg = ""; 

    for ($i = 0; $i < $ml; $i++){ 
    $newmsg = $newmsg . ($msg[$i]^$key[$i % $kl]); 
    } 

    return $newmsg; 
} 

?> 

मुझे देता है:

एनक्रिप्टेड: MTIzNDUxMjM0NTEyMzQ1MTIzNDUxMg ==

Decrypted:

आश्चर्य क्यों दोनों परिणाम अलग हैं। मुझे यह स्वीकार करना होगा कि PHP मेरा चाय का प्याला नहीं है।

बीटीडब्ल्यू, मैं इसे खिलौना परियोजना के लिए उपयोग करता हूं, इसलिए उच्च सुरक्षा की आवश्यकता नहीं है।

for ($i = 0; $i < $ml; $i++){ 
    $newmsg = $newmsg . ($msg[$i]^$key[$i % $kl]); 
} 

हालांकि, $msg कहीं भी परिभाषित नहीं है:

+0

मैं सुरक्षा विशेषज्ञ नहीं हूं लेकिन यहां मेरा लेना है: चूंकि आप जावा और PHP दोनों के आधार 64_encode का उपयोग कर रहे हैं, बेस 64 एन्कोडिंग diffirent है। जावा और php में कुछ एन्कोड करने के लिए इस समाधान पर नज़र डालें: http://stackoverflow.com/questions/11002603/base64-encode- अलग-between-java-and-php – Jozzeh

+0

मेरा उत्तर उम्मीद है कि आपके मुद्दों में से एक हल हो जाएगा। आपके PHP उदाहरण आउटपुट में, आपको लगता है कि आपका डिक्रिप्शन आउटपुट खाली है। क्या यह सही है या प्रतिलिपि/पिछली त्रुटि है? –

+0

@DuncanJones यह काफी सही है, और हाँ, यह समस्या का समाधान करता है। – phant0m

उत्तर

4

अपने PHP एन्क्रिप्शन विधि में, आपको निम्न कोड है। यह $message होना चाहिए।

+0

+1 मुझे इसे हराया – phant0m

+3

* बंदूकें, रेहोल्स्टर से धुआं उड़ाता है ... * ;-) –

+0

हाँ डंकन, आप सही हैं। कभी-कभी इस तरह की त्रुटि आपको नींद के दौरान स्पॉट करना कठिन होता है। कहानी का नैतिक: हमेशा त्रुटि_ रिपोर्टिंग (E_ALL) का उपयोग करें :) – anta40

संबंधित मुद्दे