[英]Encryption and Decryption in android
由于我是加密技术的新手,因此我陷入了困境,非常感谢您的帮助。
我有两个类,一个用于加密和解密从main.xml
发送的文本
Base64Activity:
public class Base64Activity extends Activity {
private Button btnEncrypt, btnDecrypt;
private EditText txtOrg, txtEncr, txtEncr1, txtDecr;
private byte[] encrypted;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnEncrypt = (Button)findViewById(R.id.button1);
btnDecrypt = (Button)findViewById(R.id.button2);
txtOrg = (EditText)findViewById(R.id.editText1);
txtEncr = (EditText)findViewById(R.id.editText2);
txtEncr1 = (EditText)findViewById(R.id.editText3);
txtDecr = (EditText)findViewById(R.id.editText4);
btnEncrypt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
// Generate a temporary key. In practice, you would save this key.
// See also Encrypting with DES Using a Pass Phrase.
String key="Helloooooo";
Log.d("prk","step0");
DesEncrypter a= new DesEncrypter();
String a1=txtOrg.getText().toString();
Log.d("prk",a1);
// Encrypt
encrypted = a.encrypt(a1.getBytes("UTF8"));
String value1=new String(encrypted);
txtEncr.setText(encrypted.toString());
txtEncr1.setText(value1.toString());
Log.d("prk","step2");
// Decrypt
} catch (Exception e)
{
Log.d("Exception ",e.toString());
}
}
});
btnDecrypt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
DesEncrypter a= new DesEncrypter();
//Log.d("while decrypting",encrypted );
String a1=txtEncr.getText().toString();
//byte[] decrypted = a.decrypt(a1.getBytes("UTF-8"));
byte[] decrypted = a.decrypt(encrypted);
String value=new String(decrypted);
Log.d("Decrypted Test",value);
txtDecr.setText(value);
// Decrypt
byte[] decrypted1 = a.decrypt(a1.getBytes("UTF-8"));
//byte[] decrypted = a.decrypt(encrypted);
String value1=new String(decrypted1);
Log.d("Loaded Decrypted=",value1);
FileOutputStream fout=openFileOutput("textFile.txt",MODE_WORLD_READABLE);
//OutputStreamWriter osw=new OutputStreamWriter(fout);
//write the string to the file
fout.write(encrypted);
fout.flush();
fout.close();
} catch (Exception e)
{
Log.d("Exception ",e.toString());
}
}
});
}
}
DesEncrypter:
public class DesEncrypter {
private static final String ALGO="AES";
private static final String a="TheBestSecretKey";
private static final byte[] keyValue=a.getBytes();
public byte[] encrypt(byte[] bs) throws Exception{
byte[] key={'h','e','l','l','o','o','o','o','h','e','l','l','o','o','o','o'};
SecretKeySpec skeyspec=new SecretKeySpec(key,"AES");
Log.d("Encrypted Key= ",key+"");
Cipher c=Cipher.getInstance("AES/ECB/PKCS7Padding");
c.init(Cipher.ENCRYPT_MODE,skeyspec);
byte[] encVal=bs;
Log.d("Encrypted",encVal.toString());
return c.doFinal(encVal);
}
public byte[] decrypt(byte[] encryptedData) throws Exception{
byte[] key={'h','e','l','l','o','o','o','o','h','e','l','l','o','o','o','o'};
Log.d("Decrypted Key= ",key+"");
SecretKeySpec skeyspec=new SecretKeySpec(key,"AES");
Cipher c=Cipher.getInstance("AES/ECB/PKCS7Padding");
c.init(Cipher.DECRYPT_MODE,skeyspec);
Log.d("Inside Decryption 2 ",encryptedData+"");
byte[] decValue=c.doFinal(encryptedData);
return decValue;
}
}
加密有效,使用时
DesEncrypter a= new DesEncrypter();
String a1=txtEncr.getText().toString();
byte[] decrypted = a.decrypt(encrypted);
这部分也可以。 但是当我使用
byte[] decrypted = a.decrypt(*a1.getBytes("UTF8")*);
并使用该值对其进行解密,给我一个错误“解密中的最后一块未完成”。 我认为将字节转换为字符串时存在问题,反之亦然。
我很拼命,需要你的帮助。 我想要的是将加密的文本存储在文本框中或文件中,然后再使用它来解密文本。
预先谢谢你
无需绝望,您可以检查以下内容:
txtEncr.setText(encrypted.toString());
然后
byte[] txtEncrBytes = txtEncr.getText().toString().getBytes();
通过比较加密和txtEncrBytes使用
Arrays.equals(encrypted,txtEncyBytes);
您会发现两个字节的数组不相等。
原因是txtEncr的文本是加密的toString(),您的代码在数组上调用toString,这将生成相当无用的结果,例如[C @ 16f0472。 您至少应该使用Arrays.toString将数组转换为可读的String,该String给出数组的内容。 请参阅《 编程拼图》第3章,拼图12。
但是请注意:加密的字节数组通常包含与默认平台编码中的字符不对应的字节值。 在这种情况下,当您创建String时,该字节将被转换为``替换字符''U + FFFD( ),并且正确的值将不可避免地丢失。 看到这个 。
因此答案是:只需保留您的代码为其原始版本:
DesEncrypter a= new DesEncrypter();
String a1=txtEncr.getText().toString();
byte[] decrypted = a.decrypt(encrypted);
总之,您应该始终保留加密的字节数组,而不是使用临时字符串保存它,然后从临时字符串中检索字节数组。
PS:我认为您可能需要格式化您的帖子,您的代码列表看起来很糟糕。
存储和传输加密数据的字节数组的最简单方法是将其转换为Base64:
从您的加密方法中获取encryptedBytes。 然后:
String storeThis = Base64.encode(encryptedBytes, Base64.DEFAULT);
然后,当您想让字节恢复解密时:
byte[] encryptedBytes = Base64.decode(storeThis, Base64.DEFAULT);
将cryptobytes传递给您的解密方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.