开发者

J2ME TEA Encryption problem in older phones

开发者 https://www.devze.com 2023-04-05 10:22 出处:网络
Hey guys I\'m having a huge problem when Encryp开发者_运维百科ting a message in an older phone in comparison with the newer ones.

Hey guys I'm having a huge problem when Encryp开发者_运维百科ting a message in an older phone in comparison with the newer ones.

I've compiled the code to run on older hardware (CLDC1.0, MIDP2.0), and for some reason, when I do a TEA Encryption in a Nokia N70 I end up having one ruined character when it goes from plain-text to TEA. (yes I know, from a lot of chars only that one little char gets ruined...)

When I run exactly the same app on the N8 and other more recent phones however I get it encrypting correctly.

before I post the code however here's a small explanation on what it does: basically it receives a String and a boolean inputs, the boolean states if it's for encryption or decryption purposes, whilst the string is what I want to encode or decode.

from there, I basically strip the String into a byte array, treat it accordingly (if for encrypt or decrypt) and later turn it into a String, which I then return (decrypt) or I encode in Base64 (encrypt).

The reason to encapsulate in Base64 is so it can be sent by sms, since this encoding uses non-special characters it keeps the sms limit up to 160 characters, which is desirable for the app.

now for the code:

private String HandleTEA(String input, boolean aIsEncryption) throws UnsupportedEncodingException
 {
     System.out.println(input);
     String returnable = "";
     try
     {
         TEAEngine e = new TEAEngine(); 
         if (aIsEncryption)
         {
             e.init(true, TEAkey); 
         }
         else
         {
             if(getDebug())
             {
                input = input.substring(1);

             }
             input = base64.decodeString(input);
             e.init(false, TEAkey);

         }
         byte[] aData = input.getBytes("ISO-8859-1");

         byte[] textToUse = aData;

         int len = ((textToUse.length + 16 - 1) / 16) * 16;

         byte[] secondUse = new byte[len];


         for(int i = 0; i < textToUse.length; i++)
         {
            secondUse[i] = textToUse[i];
         }

         for(int i = textToUse.length; i < secondUse.length; i++)
         {
            secondUse[i] = 0;
         }

         int blockSize = e.getBlockSize();

         byte[] outBytes = new byte[secondUse.length];

         for (int chunkPosition = 0; chunkPosition < secondUse.length; chunkPosition += blockSize)
         {
             int chunkSize = Math.min(blockSize, (textToUse.length - (chunkPosition * blockSize)));
             e.processBlock(secondUse, chunkPosition, outBytes, chunkPosition);
         }

         if(aIsEncryption)
         {
             Baseless = new String(outBytes, "ISO-8859-1");
             String encodedString = base64.encodeString(Baseless);
             char[] theChars = new char[encodedString.length()+1];

             for(int i = 0; i < theChars.length; i++)
             {
                 if(i == 0)
                 {
                     theChars[i] = '1';
                 }
                 else
                 {
                     theChars[i] = encodedString.charAt(i-1);
                 }
             }

             byte[] treating = new byte[theChars.length];

                for(int i = 0; i < theChars.length; i++)
                {
                    treating[i] = (byte)theChars[i];
                }
             returnable = new String(treating, "ISO-8859-1");
         }
         else
         {
             char[] theChars = new String(outBytes, "ISO-8859-1").toCharArray();
            String fixed ="";
            for(int i = 0; i < theChars.length; i++)
            {
                char c = theChars[i];
                if (c > 0) fixed = fixed + c;
            }
             returnable = fixed;
         }
     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
     return returnable;
 }

Anyone have any idea on what might be happening?

for comparison this is what I'm getting from the N70: e+TgV/fU5RUOYocMRfG7vqpQT+jKlujU6eIzZfEjGhXdFwNB46wYNSiUj5H/tWbta26No6wjQylgTexhS6uqyw==

and from the N8: e+TgV/fU5RUOYocMRfG7vqpQT+jKlujU6eIzZfEjgBXdFwNB46wYNSiUj5H/tWbta26No6wjQylgTexhS6uqyw==

as you can see everything looks similar, but in the middle of the code what gets encoded as Gh on the N70 shows up as gB on the N8...

when decrypting the data encrypted by the N70 we get some really weird chars:

will add this here tomorrow since I don't have the saved output with me

both are using the same key (in real-life tho they'll be using a key that's randomly generated on startup)

here's the key used: 0b1b5e0167aaee06

Hope you can help me out with this and Thanks for your interest and assistance!


your code is hard to understand, but Baseless = new String(outBytes, "ISO-8859-1"); and any similar constructs are almost certainly incorrect. Why do you want to make a String out of cipher? Just base64 encode outBytes directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号