×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Hybrid View

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    1

    crypting with J2ME decrypting with PHP

    Hallo,

    i'm working on an encryption methode for post-streams.
    my J2ME device encrypt my content (using bouncy castle api) and send it to a php script which decrypt the post-stream. But my php script decrypt it into something useless.

    -----------J2ME CODE-------------------------------------
    private String key = "1234567890";
    private String text = "Crypt this";

    private String encryptText(String key, String text) throws CryptoException
    {
    byte[] keyBytes = Base64.decode(key.getBytes());
    byte[] ptBytes = text.getBytes();

    bfCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new BlowfishEngine()));
    bfCipher.init(true, new KeyParameter(keyBytes));
    byte[] result = new byte[bfCipher.getOutputSize(ptBytes.length)];
    int len = bfCipher.processBytes(ptBytes, 0, ptBytes.length, result, 0);
    bfCipher.doFinal(result, len);

    return new String(Base64.encode(result));
    }//encryptText

    private String decryptText(String key, String cipherText) throws CryptoException
    {
    byte[] keyBytes = Base64.decode(key.getBytes());
    byte[] textBytes = Base64.decode(cipherText.getBytes());

    bfCipher.init(false, new KeyParameter(keyBytes));
    byte[] result = new byte[bfCipher.getOutputSize(textBytes.length)];
    int len = bfCipher.processBytes(textBytes, 0, textBytes.length, result, 0);
    bfCipher.doFinal(result, len);

    return new String(result).trim();
    }//decryptText
    ----------------------------------------------------------------
    PHP CODE:

    <?php
    if (isset($_POST["test"])) { $test = $_POST["test"]; }

    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
    $iv = "ha123"; //this isn't used in J2ME, don't know where to
    //implement it
    $key = "1234567890";
    function strhex($string)
    {
    $hex="";
    for ($i=0;$i<strlen($string);$i++)
    $hex.=dechex(ord($string[$i]));
    return $hex;
    }
    $textkey = strhex($key);
    echo " hexkey: $textkey ";

    echo " bevor: $test ";
    $decrypt = mcrypt_decrypt(MCRYPT_BLOWFISH, $textkey, $test, MCRYPT_MODE_CBC, $iv);
    echo " decrypt: $decrypt ";
    flush();

    ?>

    I hope you've an idea.
    Thx for helping.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Re: crypting with J2ME decrypting with PHP

    your PHP decryption code should be like-

    $key = "273e1557de5e4499";
    $B64key=base64_decode($key);
    $B64data_string=base64_decode($data_string);
    $decrypt = @mcrypt_decrypt(MCRYPT_BLOWFISH, $B64key , $B64data_string, MCRYPT_MODE_CBC);
    echo " <br /> decrypted data = $decrypt ";


    hope you've an idea.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    270

    Re: crypting with J2ME decrypting with PHP

    I haven't understand what I need to set as "key" for the javaME side and the PHP one.

    Can you help me please?

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    270

    Re: crypting with J2ME decrypting with PHP

    I fixed the interesting lines, now it works like a charm on every input.

    J2ME Code

    Code:
     
       private String key = "SADFo92jzVnzSj39IUYGvi6eL8v6RvJH8Cytuiouh547vCytdyUFl76R";
        private String text = "Crypt this";
        PaddedBufferedBlockCipher bfCipher;
    
        private String encryptText(String key, String text) throws CryptoException {
            byte[] keyBytes = Base64.decode(key.getBytes());
            byte[] ptBytes = text.getBytes();
    
            bfCipher = new PaddedBufferedBlockCipher (new CBCBlockCipher(new BlowfishEngine()), new ZeroBytePadding());
            bfCipher.init(true, new KeyParameter(keyBytes));
            byte[] result = new byte[bfCipher.getOutputSize(ptBytes.length)];
            int len = bfCipher.processBytes(ptBytes, 0, ptBytes.length, result, 0);
            bfCipher.doFinal(result, len);
    
            return new String(Base64.encode(result));
        }//encryptText
    
        private String decryptText(String key, String cipherText) throws CryptoException {
            byte[] keyBytes = Base64.decode(key.getBytes());
            byte[] textBytes = Base64.decode(cipherText.getBytes());
    
            bfCipher.init(false, new KeyParameter(keyBytes));
            byte[] result = new byte[bfCipher.getOutputSize(textBytes.length)];
            int len = bfCipher.processBytes(textBytes, 0, textBytes.length, result, 0);
            bfCipher.doFinal(result, len);
    
            return new String(result).trim();
        }//decryptText

    PHP CODE:

    Code:
    $key = "SADFo92jzVnzSj39IUYGvi6eL8v6RvJH8Cytuiouh547vCytdyUFl76R";
    $B64key=base64_decode($key);
    $B64data_string=base64_decode($data_string);
    $decrypt = @mcrypt_decrypt(MCRYPT_BLOWFISH, $B64key , $B64data_string, MCRYPT_MODE_CBC);
    echo " <br /> decrypted data = $decrypt ";
    Hope it helped.

  5. #5
    Regular Contributor
    Join Date
    May 2010
    Posts
    64

    Re: crypting with J2ME decrypting with PHP

    Hi,

    I am trying to learn about encryption in phone and decryption on the web (server or php). Good that I came across to this post.

    As long as I can get from the discussion, there is something about the key. So, why did SADFo92jzVnzSj39IUYGvi6eL8v6RvJH8Cytuiouh547vCytdyUFl76R work and 1234567890 didnot?

  6. #6
    Registered User
    Join Date
    Jul 2012
    Location
    South America
    Posts
    43

    Re: crypting with J2ME decrypting with PHP

    sir ive got this error ...

    i'll try that code .

    symbol : method encode(byte[])
    location: class hello.Base64
    return new String(Base64.encode(result));


    symbol : method doFinal(byte[],int)
    location: class hello.PaddedBufferedBlockCipher
    bfCipher.doFinal(result, len);



    ive got that error)

    what should i do with that ?

    thanks

  7. #7
    Nokia Developer Champion
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,545

    Re: crypting with J2ME decrypting with PHP

    Why are you not using/trying the code in #4 as it seems to be complete functions (one function for you to call to encrypt the data and another function to decrypt the data) with a fixed key?

    If all you have tried is the few lines you posted, it is not something that'll compile to anything useful at all; it is not a complete application or even comple function/class, or anything, really.

    And when ever you have compilation problems, "that error" is not very helpful as far as descriptions go. Post the actual compiler error messages with line numbers, and complete code (not just a few isolated lines), as well as information as to which development environment and SDK you are trying to use.

    Let's start with really elementary stuff...

    Do you have a working J2ME build environment where you can actually compile and run, e.g., some "Hello World" application successfully in the emulator?

    If you have, which tools and SDKs are you using? NetBeans? Eclipse? Something else?

    If do not have a working environment, start by going through this "Getting Started" guide all the way until you manage to do the last "Run your app on a phone" step, too: https://www.developer.nokia.com/Deve...rted/NetBeans/

    If you can't build and run a complete, ready-made example application, trying to do anything more advanced is not going to be possible. You have to learn to walk, before you can run.

  8. #8
    Registered User
    Join Date
    Jul 2012
    Location
    South America
    Posts
    43

    Re: crypting with J2ME decrypting with PHP

    sir im so sorry but its hard for me to encrypt the data and to decrypt it i just want a tutorial?
    because i don't know on how to encrypt the message then it will send to the server of our school that's
    why i need a very simple standard encrypter in j2me and how can i do this ..

    thankx

  9. #9
    Nokia Developer Champion
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,545

    Re: crypting with J2ME decrypting with PHP

    Quote Originally Posted by AnorEnaj2330 View Post
    sir im so sorry but its hard for me to encrypt the data and to decrypt it i just want a tutorial?
    because i don't know on how to encrypt the message then it will send to the server of our school that's
    why i need a very simple standard encrypter in j2me and how can i do this ..
    You have already the simplest possible (more or less) encryption and decryption code in post #4 in this thread.

    Here are the steps you need to take:

    1. If you need to send data to a server, stop playing around with irrelevant SMS examples, but take one of the existing HTTP examples in the wiki (which already have been pointed to you in your other threads), and first make that example work with your server side code, so that you know how to send data to a server, and you know how to receive it on the server side.

    2. Once sending data to the server works (i.e. what you wish to send in clear, unencrypted form is received correctly on the server), then use the encryption code in #4 to encrypt the data before sending, and make sure the server receives it.

    3. When the server can receive the encrypted data (check that the encrypted data looks/is the same on the phone, and the server side), add the PHP decryption step before you try to use the data on the server.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Location
    South America
    Posts
    43

    Re: crypting with J2ME decrypting with PHP

    Good day !!

    this is what ive looking for ..
    im a new begginer in java then we have a project ..

    i have my message that was encrypted in j2me then it will send in php .
    i want to decrypt it by php can you please help me on it ..

    what codes can you offer to me ?

    thanks in regard.
    god Bless more power !

  11. #11
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,572

    Re: crypting with J2ME decrypting with PHP

    Well, for codes, you may want to consider reading post #4 in this discussion. Of course decryption depends a lot on the actual way you encrypt the data.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    270

    Re: crypting with J2ME decrypting with PHP

    Quote Originally Posted by wizard_hu_ View Post
    Well, for codes, you may want to consider reading post #4 in this discussion. Of course decryption depends a lot on the actual way you encrypt the data.
    it's incredible that in 2012 there is someone who talk about JavaME.

  13. #13
    Registered User
    Join Date
    Jul 2012
    Location
    South America
    Posts
    43

    Re: crypting with J2ME decrypting with PHP

    thank you for that
    Mr.Petrib ??

    what can I use blowfish or bouncy castle?
    what is more easier ?



    the flow of the program

    hello sir good day !!
    can you please guide me in this problem sir ??
    im a j2me programmer

    i have my form in my emulator named " REGISTRATION"
    then it have a textfields

    example
    NAME:
    ADDRESS:
    AGE:

    something like that
    then i have my button " SEND "
    all i want is if i click the button send
    the data that inserted in that field

    example
    NAME:Sushmita takeyi
    ADDRESS:South korea
    AGE:35 years old

    the high lighten color red
    will be sent to the receiver but that data is secured and
    it is encrypted when the receiver received it.


    sir is the bouncy castle is for a code or installer.
    thanks in regard. !!
    Last edited by AnorEnaj2330; 2012-10-03 at 02:17.

  14. #14
    Nokia Developer Champion
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,545

    Re: crypting with J2ME decrypting with PHP

    Quote Originally Posted by AnorEnaj2330 View Post
    what can I use blowfish or bouncy castle?
    Use the code/functions from #4.


    Quote Originally Posted by AnorEnaj2330 View Post
    what is more easier ?
    It really can't get much more easier/simpler, it seems, than than using the code in #4 as all you need to do is to add it to your existing code as I outlined for you in #22.


    Quote Originally Posted by AnorEnaj2330 View Post
    im a j2me programmer
    Hmmm.

  15. #15
    Registered User
    Join Date
    Jul 2012
    Location
    South America
    Posts
    43

    Re: crypting with J2ME decrypting with PHP

    hello good day ..

    sir so it is possible that using a encrypted data by j2me you can decrypt it by php is there any code for this .

    thanks in regard.

    God bless !!1 more power ..

Posting Permissions

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