This discussion is archived
1 2 Previous Next 18 Replies Latest reply: Aug 13, 2012 3:46 AM by sabre150 RSS

Encrypt and decrypt using ECB mode

user10070648 Newbie
Currently Being Moderated
Dear All,

I'm looking the way to convert special character to hex and perform the encryption

Example : (All are dummy values )

Encryption key = K (Double length and using triple-DES)


Clear test (C) 1234AB=C6578ABE3 -----(Encode) ------------>After encoding the data should 0-F (ABC12E340987EA12) --------(Encrypt using K)------>Ciper data ( ACF1BE547659AC34) ------->and sending to another server.

At server level

Ciper data( ACF1BE547659AC34) ------>(Decrypt using K)------>Getting encoding value (ABC12E340987EA12)-------->(Decode)------>Clear test (C) = 1234AB=C6578ABE3 -----------> and processing


Q1. Is there any encoding method that can be used ?

Note : Really I'm not in position to change length of clear test (such as encoding asciii ) .

Regards
  • 1. Re: Encrypt and decrypt using ECB mode
    sabre150 Expert
    Currently Being Moderated
    I have read this 3 times but I really can't understand your requirements. To convert a special character to hex you hex encode it! Why do you insist on hex encoding before encryption? Why do you think that your hex cleartext will be hex ciphertext after encryption?

    Modern encryption takes as input bytes and produces bytes. The input may or may not be binary but the ciphertext most certainly is binary. One can Hex or Base64 or ASCII85 encode the ciphertext so as to produce only ASCII. There is no need to Hex or Base64 or ASCII85 encode the cleartext before encryption; in fact it makes little or no sense to do so since it inflates the cleartext and therefore the ciphertext.
  • 2. Re: Encrypt and decrypt using ECB mode
    user10070648 Newbie
    Currently Being Moderated
    Thanks

    I have send the my test code
    -------------------------------------------------------------------------
    import java.security.Provider;
    import java.security.Security;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    
    public class Encrypter {
         
         private static Provider PR   = null;
         private static SecretKey KEY = null;
         
         public static void init() throws Exception{
              PR = new com.sun.crypto.provider.SunJCE();
              Security.addProvider(PR);
              
               
            KeyGenerator desKeyGen = KeyGenerator.getInstance("DESede", PR);
            desKeyGen.init(112);
            KEY=  desKeyGen.generateKey();
         }
    
         public static byte[] encrypt(byte[] clearBytes) throws Exception {
              Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding", PR.getName());
              cipher.init(Cipher.ENCRYPT_MODE,KEY);
              return cipher.doFinal(clearBytes);
    
         }
    
         public static byte[] decrypt(byte[] encr) throws Exception {
              Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding", PR.getName());
              cipher.init(Cipher.DECRYPT_MODE, KEY);
              return cipher.doFinal(encr);
         }
    
         static String byteArrayToHexString(byte in[])
         {
             byte ch = 0x00;
             int i = 0;
             if (in == null || in.length <= 0)
             {
                     return null;
             }
             String pseudo[] = {"0", "1", "2","3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E", "F"};
             StringBuffer out = new StringBuffer(in.length * 2);
             while (i < in.length)
             {
                     ch = (byte) (in[i] & 0xF0); // Strip off high nibble
                     ch = (byte) (ch >>> 4);     // shift the bits down
                     ch = (byte) (ch & 0x0F);    // must do this is high order bit is on!
                     out.append(pseudo[ (int) ch]); // convert the nibble to a String Character
                     ch = (byte) (in[i] & 0x0F); // Strip off low nibble
                     out.append(pseudo[ (int) ch]); // convert the nibble to a String Character
                     i++;
             }
             String rslt = new String(out);
             return rslt;
         }
         
         public static byte[] hexStringToByteArray(String s) {
               int len = s.length();
               byte[] data = new byte[len / 2];
               for (int i = 0; i < len; i += 2){
               data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
               + Character.digit(s.charAt(i+1), 16));
               }
               return data;
        }
         public static void main(String[] args) {
              
              String clearString = "1234AB=C6578ABE3";
              byte clearByte[] = hexStringToByteArray(clearString);
              
              try {
                   
                   init();
                   
                   System.out.println("Clear     data : "+clearString);
                   
                   byte enrByte[] = encrypt(clearByte);
                   System.out.println("Encrypted data : "+byteArrayToHexString(enrByte));
                   
                   byte decByte[] = decrypt(enrByte);
                   System.out.println("Decrypted data : "+byteArrayToHexString(decByte));
                   
              } catch (Exception e) {
                   
                   e.printStackTrace();
              }
              
              
         }
    
    }
    ---------------------------------------



    Out put as follow,

    Clear data : 1234AB=C6578ABE3
    Encrypted data : 7EAEA4AB3FAA8DC1
    Decrypted data : 1234ABFC6578ABE3

    I'm not getting correct clear value after decrypting. (= character is getting replace with F)

    Thanks

    Edited by: sabre150 on Aug 9, 2012 2:46 PM

    Moderator action : added [ code] tags to format the code. Please do this yourself in the future.
  • 3. Re: Encrypt and decrypt using ECB mode
    sabre150 Expert
    Currently Being Moderated
    Err ... why are you doing
         String clearString = "1234AB=C6578ABE3";
              byte clearByte[] = hexStringToByteArray(clearString);
    when "1234AB=C6578ABE3" is NOT NOT NOT a hex string ? Why do you thing you need to hex decode it rather than just using
    byte[] clearByte = clearString.getBytes("utf-8");
    Notes - you do realise that using NoPadding means your cleartext must be an exact multiple of 8 bytes. You do realize that DES ede was fabricated because DES was considered too weak and that it is now superceded by AES. You do realise that ECB mode is considered insecure since it allows forgery of ciphertext by splicing.
  • 4. Re: Encrypt and decrypt using ECB mode
    user10070648 Newbie
    Currently Being Moderated
    Thanks,

    As I mentioned that I have to consider sending request packet size to the server. It should be 8 bytes. But with your encoding it's exceeding the packet size (16 bytes)

                   System.out.println("Clear data : "+clearString);
                   byte[] clearByte = clearString.getBytes("utf-8");
                   init();
                   
                   
                   byte enrByte[] = encrypt(clearByte);
                   System.out.println("Encrypted data : "+byteArrayToHexString(enrByte));
                   
                   byte decByte[] = decrypt(enrByte);
                   System.out.println("Decrypted data : "+new String(decByte));

    Output as bellow,

    Clear data : 1234AB=C6578ABE3
    Encrypted data : 3AD6DFEA7E73FCFA843652B507718BFD
    Decrypted data : 1234AB=C6578ABE3

    --------------------------------------------
    Really, It's casing me only = charcter and I wnat to do some replacemnt of = charcter and same thing after decrypting.

    Example : (This just a esay understanding)

    byte tmp[] = FFFFFFFFFFFFFFFF

    After use XOR

    byte encode[] = tmp XOR clearData (Hope all digits are 0-F value in encode byte array)

    And server side, it's decoding

    byte clear data [] = tmp XOR DecryptedDate

    Somting like that.
    --------------------------------------------

    Regards,
  • 5. Re: Encrypt and decrypt using ECB mode
    EJP Guru
    Currently Being Moderated
    But with your encoding
    You mean by removing your decoding.
    it's exceeding the packet size (16 bytes)
    But with your decoding it doesn't work at all. Which do you prefer?
    Really, It's casing me only = charcter and I wnat to do some replacemnt of = charcter and same thing after decrypting.
    Please translate that into standard English. If your data is hex, by all means decode it to binary, but it isn't hex, is it? That's the issue you currently need to address: what is it?
  • 6. Re: Encrypt and decrypt using ECB mode
    sabre150 Expert
    Currently Being Moderated
    user10070648 wrote:

    As I mentioned that I have to consider sending request packet size to the server. It should be 8 bytes. But with your encoding it's exceeding the packet size (16 bytes)
    Where did you say that "It should be 8 bytes"? Why are you limited to 8 bytes?

    I think I now understand what you are trying to do. You have 16 characters that you need to pack into 8 bytes. Most of the time your characters are in the hex range [0-9,A-F] but one (or more?) could also be the '=' char. Therefore you have at least 16^16 +1 possible values which as your are finding won't fit into the 16^16 values that are available with just 8 bytes. The means you have to find some other compression method.

    Compression makes use of redundancy within the message being compressed so you need to establish what redundancy you can exploit. We cannot help here until you describe exactly what the message represents.

    P.S. This problem has nothing at all to do with ECB and encryption.
  • 7. Re: Encrypt and decrypt using ECB mode
    user10070648 Newbie
    Currently Being Moderated
    Thanks,



    Actually, in the data to be encrypted is including only non-hex character '=' all other values are hex value (0-9, a-f). Really I'm facing this issue when character '=' is in the data except that it's not an issue for me. And always the length should be 8 bytes (After encrypting)


    Regards
  • 8. Re: Encrypt and decrypt using ECB mode
    EJP Guru
    Currently Being Moderated
    I'm facing this issue when character '=' is in the data except that it's not an issue for me.
    You're "facing this issue" but "it's not an issue"? Please make some attempt to make sense.

    If the data contains non-hex characters you cannot hex-decode it. End of discussion. Period. Punto basta. Finis.
    And always the length should be 8 bytes (After encrypting)
    Why?
  • 9. Re: Encrypt and decrypt using ECB mode
    user10070648 Newbie
    Currently Being Moderated
    You're "facing this issue" but "it's not an issue"? Please make some attempt to make sense.



    Means, character '=' will not include in data at all time (most of time it's totally hex values). However, before encrypting, i have to check whether it's contained non-hex value (=) if yes, I have to appliy encoding or any mechanism which I'm seeking in this forum. else nothing to do me.





    Why?



    It's not relevant me (spec of server side). Hope your answer rather than asking why



    Regards
  • 10. Re: Encrypt and decrypt using ECB mode
    EJP Guru
    Currently Being Moderated
    However, before encrypting, i have to check whether it's contained non-hex value (=) if yes, I have to appliy encoding or any mechanism which I'm seeking in this forum. else nothing to do me.
    So what other values can occur?
    It's not relevant me (spec of server side). Hope your answer rather than asking why
    Of course it's relevant. It is entirely possible that you have:

    - misunderstood the server specification
    - misunderstood the input data specification
    - left out some important information.

    At present what you are asking for doesn't begin to make sense. In other words there is no apparent solution. That's why everybody, not just me, has been asking you to provide more context. If you want a solution you would be wise to heed what you're being told here instead or arbitrarily deciding you have provided enough information to solve it, as though you were a CS professor and already knew the answer. You don't, otherwise you wouldn't be posting here.
  • 11. Re: Encrypt and decrypt using ECB mode
    user10070648 Newbie
    Currently Being Moderated
    So what other values can occur?



    Only character '=' is casing me no any more non-hex values.



    Okey



    What host side is telling that they wish to implement the relevant decoding method according to our specification. but they are not in position to change the length of packet (it should be 8 bytes)
  • 12. Re: Encrypt and decrypt using ECB mode
    EJP Guru
    Currently Being Moderated
    This is not a mere encryption/Java programming question. It is an application protocol problem. You need to talk to your system designers.
  • 13. Re: Encrypt and decrypt using ECB mode
    sabre150 Expert
    Currently Being Moderated
    user10070648 wrote:
    So what other values can occur?



    Only character '=' is casing me no any more non-hex values.
    Every two hex characters defines 16^2 different values which will just and only just fit into a single byte which has 256 possible values. If you try to expand each character so that it is no longer hex but hex + '=' then you have 17^2 (289) values for each pair which cannot possibly fit into a single byte. I find it difficult to comprehend how you can expect this to fit; it is fundamental that it cannot fit.

    >
    >
    Okey



    What host side is telling that they wish to implement the relevant decoding method according to our specification. but they are not in position to change the length of packet (it should be 8 bytes)
    I think I am just wasting my time here. So what the hell is your specification? We really cannot help until we know the context and for some obscure reason you seem reluctant to provide the context.

    Bye
  • 14. Re: Encrypt and decrypt using ECB mode
    user10070648 Newbie
    Currently Being Moderated
    Thanks

    bye
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points