旧手机中的 J2ME TEA 加密问题

发布于 2024-12-04 14:48:07 字数 3814 浏览 1 评论 0原文

嘿伙计们,与新手机相比,在旧手机中加密消息时我遇到了一个很大的问题。

我已编译代码以在较旧的硬件(CLDC1.0、MIDP2.0)上运行,并且出于某种原因,当我执行 TEA 诺基亚 N70 中的加密 当它从纯文本转换为 TEA 时,我最终得到了一个被破坏的字符。 (是的,我知道,在很多字符中,只有一个小字符被破坏了......)

当我在 N8 和其他更新的手机上运行完全相同的应用程序时,我得到了正确的加密。

在我发布代码之前,这里有一个关于它的作用的小解释: 基本上它接收一个字符串和一个布尔输入,布尔值表明它是否用于加密或解密目的,而字符串是我想要编码或解码的内容。

从那里开始,我基本上将字符串剥离为字节数组,相应地处理它(如果用于加密或解密),然后将其转换为字符串,然后返回(解密)或以 Base64 编码(加密)。

采用 Base64 封装的原因是它可以通过短信发送,因为这种编码使用非特殊字符,因此短信限制最多为 160 个字符,这对于应用程序来说是理想的。

现在是代码:

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;
 }

有人知道可能会发生什么吗?

作为比较,这是我从 N70 得到的结果: e+TgV/fU5RUOYocMRfG7vqpQT+jKlujU6eIzZfEjGhXdFwNB46wYNSiUj5H/tWbta26No6wjQylgTexhS6uqyw==

和来自 N8: e+TgV/fU5RUOYocMRfG7vqpQT+jKlujU6eIzZfEjgBXdFwNB46wYNSiUj5H/tWbta26No6wjQylgTexhS6uqyw==

如您所见,一切看起来都很相似,但在代码中间,在 N70 上编码为 Gh 的内容在 N70 上显示为 gB N8...

当解密由 N70 加密的数据时,我们得到一些非常奇怪的字符:

明天将在此处添加此字符,因为我没有保存的输出

两者都使用相同的密钥(在现实生活中,他们将使用启动时随机生成的密钥)

以下是使用的密钥: 0b1b5e0167aaee06

希望您能帮助我解决这个问题,感谢您的兴趣和帮助!

Hey guys I'm having a huge problem when Encrypting 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情空心 2024-12-11 14:48:07

您的代码很难理解,但是 Baseless = new String(outBytes, "ISO-8859-1"); 和任何类似的构造几乎肯定是不正确的。为什么你想用密码创建一个字符串?只需直接对 outBytes 进行 base64 编码即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文