加密/解密字符串在Android中和Java不同的价值观 [英] Encrypt/Decrypt String in Android and Java different values

查看:175
本文介绍了加密/解密字符串在Android中和Java不同的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用I'm此code加密和解密使用Java和Android琴弦并在每个系统中,我得到一个不同的值。

I´m using this code to Encrypt and Decrypt in Java and Android some Strings and in each system I get a different value.

在code我使用的是来自 http://www.androidsnippets.com/encryptdecrypt -strings (怎么一回事,因为这是相当大的,我不会粘贴)。

The code I'm using comes from http://www.androidsnippets.com/encryptdecrypt-strings (I won't paste it beacuse it's quite big).

例如在Android中加密AA我得到这个:
1C6BD31C57F42ACFD0EDD2DD5B7A92CA

For example in Android for encrypting "aa" I get this: 1C6BD31C57F42ACFD0EDD2DD5B7A92CA

和恰与在Java中相同的密钥作为种子我得到相同的字符串:
61FAD1203B7AC92AD9345771AA273DA5

and exactly the same String with the same key as seed in Java I get: 61FAD1203B7AC92AD9345771AA273DA5

任何想法?

在此先感谢!

推荐答案

这只是我的猜测,但我想原因是你的密钥派生。我不是一个真正的Java开发人员的,所以我可能无法正确​​理解code。

This is just my guess, but I think the reason is your key derivation. I'm not really a Java developer though, so I might not be understanding the code correctly.

这code总是调用getRawKey()当您加密和解密。 getRawKey()看起来像它需要他们所谓的种子,或共享机密的东西,并用它来计算出一个新的随机密钥来完成实际的加密/解密。

This code always calls getRawKey() when you encrypt and decrypt. getRawKey() looks like it takes something they call a seed, or your shared secret, and uses it to compute a new random key to do the actual encryption/decryption.

SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed);
    kgen.init(128, sr); // 192 and 256 bits may not be available
    SecretKey skey = kgen.generateKey();

据Java文档中发现的这里,过setSeed()重新提供此随机对象。给定种子补充,而不是取代后,现有的种子。

According to Java docs found here, setSeed() "Reseeds this random object. The given seed supplements, rather than replaces, the existing seed."

我的猜测是,RNG的初始状态为每个系统/平台上的不同,因此它给你不同的结果。你应该可以解决密钥派生的东西更标准和一致,或者使用已经建立的加密系统,就像在充气城堡库PGP。

My guess is that the initial state of the RNG is different on each system/platform, and thus it's giving you different results. You should fix the key derivation to something more standard and consistent, or use an already established crypto system, like PGP in the Bouncy Castle libraries.

这篇关于加密/解密字符串在Android中和Java不同的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆