简体   繁体   English

NSData转换为Java String

[英]NSData to Java String

I've been writing a Web Application recently that interacts with iPhones. 我最近一直在编写与iPhone交互的Web应用程序。 The iPhone iphone will actually send information to the server in the form of a plist. iPhone iphone实际上将以plist的形式将信息发送到服务器。 So it's not uncommon to see something like... 因此,看到类似...的情况并不少见。

<key>RandomData</key>
<data>UW31vrxbUTl07PaDRDEln3EWTLojFFmsm7YuRAscirI=</data>

Now I know this data is hashed/encrypted in some fashion. 现在,我知道此数据已经以某种方式进行了哈希/加密。 When I open up the plist with an editor (Property List Editor), it shows me a more "human readable" format. 当我使用编辑器(“属性列表编辑器”)打开plist时,它向我展示了一种更加“易于阅读”的格式。 For example, the data above would be converted into something like... 例如,上面的数据将被转换为...

<346df5da 3c5b5259 74ecf683 4431249f 711630ba 232c54ac 9bf2ee44 0r1c8ab2>

Any idea what the method of converting it is? 知道转换的方法是什么吗? Mainly I'm looking to get this into a Java String. 主要是我希望将其放入Java String。

Thanks! 谢谢!

According to our friends at wikipedia , the <data> tag contains Base64 encoded data. 根据我们在Wikipedia上的朋友所说, <data>标签包含Base64编码的数据。 So, use your favorite Java "Base64" class to decode (see also this question ). 因此,请使用您喜欢的Java“ Base64”类进行解码(另请参见此问题 )。

ps. ps。 technically, this is neither "hashed" nor "encrypted", simply "encoded". 从技术上讲,这既不是“散列”也不是“加密”,只是“编码”。 "Hashed" implies a one-way transformation where multiple input values can yield the same output value. “哈希”表示单向转换,其中多个输入值可以产生相同的输出值。 "Encrypted" implies the need for a (usually secret) "key" to reverse the encryption. “已加密”表示需要一个(通常是秘密的)“密钥”来反转加密。 Base64 encoding is simply a way of representing arbitrary binary data using only printable characters. Base64编码只是一种仅使用可打印字符表示任意二进制数据的方式。

After base64 decoding it you need to hex encode it. base64解码后,您需要对其进行十六进制编码。 This is what PL Editor is showing you. 这就是PL编辑器向您显示的内容。

So... 所以...

<key>SomeData</key>
<data>UW31ejxbelle7PaeRAEen3EWMLojbFmsm7LuRAscirI=</data?

Can be represented with... 可以用...表示

byte[] bytes = Base64.decode("UW31ejxbelle7PaeRAEen3EWMLojbFmsm7LuRAscirI=");
BigInteger bigInt = new BigInteger(bytes);
String hexString = bigInt.toString(16);
System.out.println(hexString);

To get... 要得到...

<516df5aa 3c5b5259 74ecf683 4401259f 711630ba 236c59ac 9bb2ee44 0b1c8ab2>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM