我试图开发一个可以加密和解密值的android应用程序。所以我遵循这个链接在这里输入链接描述
到目前为止,我能够加密文本,但无法解密它。在我的代码中,我使用了在提供的链接中提到的相同的AESHelper类。
下面是我用来加密和解密值
的活动类import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText text ;
TextView encp,decriptom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText);
encp = (TextView) findViewById(R.id.valueexcript);
decriptom = (TextView) findViewById(R.id.deexcript);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void Ecript(View v)
{
String Key = "avc";
try {
String normalTextEnc = AHShelper.encrypt(Key, text.getText().toString());
Toast.makeText(this,normalTextEnc,Toast.LENGTH_LONG).show();
encp.setText(normalTextEnc);
} catch (Exception e) {
e.printStackTrace();
}
// Toast.makeText(this,"Hello",Toast.LENGTH_LONG).show();
String decript;
try {
decript = AHShelper.decrypt(Key,encp.getText().toString());
decriptom.setText(decript);
Toast.makeText(this,decript,Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我使用的AHShelper类在
下面import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AHShelper {
public static String encrypt(String seed, String cleartext)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted)
throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
}
基本上,这段代码依赖于一个小技巧:如果您在使用SUN提供程序和Bouncy Castle提供程序之前为SHA1PRNG播种,那么它将始终生成相同的随机字节流。
但并非每个提供者都是如此;其他提供商只是简单地混合了种子。换句话说,他们可能会使用预先播种的PRNG,并将种子混合在一起。在这种情况下,getRawKey
方法为加密和解密生成不同的密钥,这将导致解密失败。
提供者也可能决定完全使用基于SHA-1的不同算法,因为SUN/Oracle使用的算法没有很好地指定——至少是公开的。
基本上,这个可怕的代码片段滥用SHA1PRNG作为密钥派生函数或KDF。如果输入的是密码,则应该使用真正的KDF,例如PBKDF2;如果输入的是密钥,则应该使用HKDF。PBKDF2被构建到Java中。
该代码片段应该被删除。它已经从Android片段复制,但我找不到那个网站了。换句话说,当它可用时,它似乎更不正常。
在使用SUN加密时检索数据的一个可能的解决方案是在Oracle提供的JDK上解密它。否则,您也可以复制SHA1PRNG的内部实现类的代码,并使用它来解密数据。请注意,您确实需要记住SUN的源代码是遵循GPL的;如果你这样做,你需要遵守那个许可证。对于旧的Android版本,你可以使用它的源代码。我强烈建议删除这段可怕的代码,转而使用PBKDF2。
如果你使用的实现返回一个完全随机的键,那么你就完全不走运了。您的数据消失了,句号。请放心,我们会永远保密的。