Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 51
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Without your code there is no possibility that anyone can help.
    You have never explained how your algorithm is supposed to work. The one you posted will change valid characters to characters that are out of the normal ASCII character set range. That is what is show as boxes.
    What do you want the code to do? At a detail level, given an input char what do you want the output char to be?

  2. #22
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    I have a function that is called encrypt, in php. This returns boxes but encodes everything in base64 so no probs. The decrypt function, which is in Java, will take that base64 string and change it to the first input that was given to the encrypt function.

    I can give you the whole decrypt method that is working right now, and the encrypt function in PHP if needed. I just want this to work.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Without your code there is no possibility that anyone can help.
    Is this the way your app works:
    The input string is encrypted to some data.
    The some data is encoded again in base64
    .. Sent
    The received data is decoded as base64 (this should be the same as the some data out of the encrypt)
    The decrypt method converts the some data back to the input string.

    Now can you explain the algorithm used in the encrypt and decrypt methods?

  4. #24
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package funstein.desifre;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.Arrays;
    import org.apache.commons.codec.binary.Base64;
    /**
     *
     * @author Funstein
     */
    public class crypt {
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException {
            String abc = "2v_3_vysEgYEBu_A" ;
            System.out.println(abc);
            String decrypted = decrypt(abc, "funstein") ;
            System.out.println(decrypted);
            
    
    
        }
        
    
        public static String decrypt(String anahtar, String key){
            String sResult = "";
            anahtar = Decode_Base64(anahtar);
            for (int i = 0; i < anahtar.length(); i++) {
                char sChar = anahtar.charAt(i);
                char sKeyChar ;
                if((i % key.length()) >= 1 && (i % key.length()) <= 7){
                    sKeyChar = key.charAt((i % key.length()) -1) ;
                }
                else{
                    sKeyChar = key.charAt(key.length() - 1) ;
                }
                int diff = (sChar) - (sKeyChar);
                sResult += (char)diff;
            }
            return sResult;
        }
    
        private static String Decode_Base64(String encodedString){
            String decodethis = encodedString.replace("-_", "+/") ;
            String decodedString = Decode64(decodethis);
    
            return decodedString;
        }
    
        private static String Decode64(String hello){
            byte[] decoded = Base64.decodeBase64(hello.getBytes());
    
            String decodedString = new String(decoded);
            return decodedString;
        }
    }
    This is the code. The algorithm is simple, the encoder gets ASCII values of the DATA and the KEY(keeps looping through key until all of data is converted), adds them together to create invalid characters. The problematic part, the decoder, gets the invalid characters, processes them using the key and finds the DATA. But it is in chinese.

    Using this code I sent you, try decrypt("2v_3_vysEgYEBu_A","funstein");

    The first parameter is an encrypted key, which was encrypted by the encrypter, in PHP. It is encrypted version of the string "Hello world.", but the Java one converts the thing into chinese letters. Help me please.

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    encoder gets ASCII values of the DATA and the KEY(keeps looping through key until all of data is converted), adds them together
    The output from the encrypter ( byte by byte) is the sum of the chars from the key and the input.
    You didn't post the code for the encrypter so that can be verified.
    The decrypter then should subtract the key from the encrypted char to restore the input char.

  6. #26
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    PHP Code:
    <?php
        function encrypt($data, $key) {
            $sResult = "";
            for ($i = 0; $i < strlen($data) ; $i++) {
                $sChar = substr($data, $i, 1);
                $sKeyChar = "" ;
                if(($i % strlen($key)) >= 1 && ($i % strlen($key)) <= 7){
                    $position = ($i % strlen($key)) - 1 ;
                    $sKeyChar = substr($key, $position, 1) ;
                }
                else{
                    $position = strlen($key) - 1;
                    $sKeyChar = substr($key, $position, 1) ;
                }
                $diff = ord($sChar) - ord($sKeyChar);
                $sResult .= chr($diff);
            }
            return encode_base64($sResult) ;
        }
    
    
    function encode_base64($sData){ 
        $sBase64 = base64_encode($sData); 
        return strtr($sBase64, '+/', '-_'); 
    } 
    
    $code = "Hello world." ;
    $code = encrypt($code, "funstein") ;
    echo $code ;
    
    
    
    ?>
    Here is the encrypter. I really need help because this was due Monday and I've been struggling with this since then. I'd appreciate if you could run the codes and take a look at what happens.

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Sorry the PHP code is useless for me. I can not compile and execute it.
    Your algorithm doesn't work. Here is how I asked you how it worked:
    The decrypter: subtract the key char from the encrypted char to restore the input char.
    Take the first chars of the encrypted and the key:
    '2' - 'f' is not a valid character '2' = 50 and 'f' = 102 gives 50-102 = -52???
    Here the first key char is 'f' and the first encrypted is a '2'. '2'-'f' is not a valid character.
    Last edited by Norm; 08-17-2011 at 11:26 PM.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Ignore that last bit. I see that the String input is base64. I don't have a base64 decoder I can use.

  9. #29
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    Alright, implementing a workaround to support this :

    The function chr() also accepts negative numbers as an ascii code, so chr(-number) is equal to chr((number%256)+256).
    And for ascii code higher than 255 is chr(number%256)
    I'll do it with a bunch of if statements.

  10. #30
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    Changed code to :
    Java Code:
       public static String decrypt(String anahtar, String key){
            String sResult = "";
            anahtar = Decode_Base64(anahtar);
            for (int i = 0; i < anahtar.length(); i++) {
                char sChar = anahtar.charAt(i);
                char sKeyChar ;
                if((i % key.length()) >= 1 && (i % key.length()) <= 7){
                    sKeyChar = key.charAt((i % key.length()) -1) ;
                }
                else{
                    sKeyChar = key.charAt(key.length() - 1) ;
                }
                int diff ;
                if((int)sChar > (int)sKeyChar){
                    diff = (int)sChar - (int)sKeyChar;
                }
                else{
                    diff = (int)sKeyChar - (int)sChar;
                    diff = ((diff%256)+256) ;
                    
                }
                sResult += (char)diff;
            }
            return sResult;
        }
    Still no luck, the same chinese thing : マラネマハŢşťŨラ

    Edit : It is NOT the same chinese string, though it is in chinese and not Hello world.

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Put the output from the base64 into an array of int {23, -44, 55,....} and post that. I can use that as input to a decrypt method.

    You've never said if the algorithm I posted was correct.

  12. #32
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    [B@569374d3
    there you go, that is the decoded string.

    Nevermind, I misunderstood. Byte array coming in a minute.

  13. #33
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    [-38, -1, -9, -2, -4, -84, 18, 6, 4, 6, -17, -64]

    there it is.

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    x + f = -38 // The encrypt step

    now to solve the equation for the unknown char x
    Last edited by Norm; 08-18-2011 at 12:02 AM. Reason: Wrong encrypt algorithm

  15. #35
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    Nope, the 1st letter is "H". I think there is an error in some unicode convertion, as the values PHP sum are ASCII values.

  16. #36
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I don't understand your algorithm. 'H' + 'f' = 174
    How do you get -38 from that?
    Java Code:
              System.out.println((int)'H' + " " + (int)'f' + " " + ('H' + 'f'));
              //                     72              102                  174

  17. #37
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    Its all because of cross-platform issues. When I encode stuff from PHP and decode in PHP, everything is OK. When I encode in java and decode in java, everything is OK again. But when I go cross-platform, they go nuts. Is there a way of calling a Java method that is in a .class file on the server from PHP? That would help I think.

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I have no idea what the problem is other than perhaps you don't understand the algorithm.

    How do you get -38 as the encrypted value for the first letter of the input and the key?
    If the first letter of the input is 'H' and the first letter of the key is 'f'.
    When you understand that, then maybe there can be progress.

  19. #39
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    I suspect this is all because Java won't wrap the same way as PHP.
    PHP is working with a char as an ASCII character (hence 256)...Java's char is a Unicode character, so wraps at a far higher point that PHP.
    The only way to handle that is to read the PHP sent stuff as bytes and calculate using that, converting to a char when finished.

  20. #40
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default

    Yes! Thats what I've been trying to say. Can you please tell me how to do that? I'd appreciate.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. problem with encryption in steganography
    By stegano in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 01:10 PM
  2. Java encryption problem
    By lqu in forum Advanced Java
    Replies: 0
    Last Post: 01-24-2011, 04:30 AM
  3. OAEP Padding Problem by encryption
    By Buhmann in forum Advanced Java
    Replies: 1
    Last Post: 09-07-2009, 01:19 PM
  4. WebService encryption problem
    By falnik in forum Advanced Java
    Replies: 3
    Last Post: 07-26-2009, 04:21 AM
  5. Encryption-Decryption Problem
    By dinesh.1.sharma in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2008, 10:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •