1

由于我是加密新手,我被困住了,非常感谢您的帮助。

我有两个类一个用于加密和解密发送的文本main.xml

Base64活动:

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());

}
}
});
} 
}

解密器:

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")*);

并使用此值对其进行解密,给我一个错误“解密中的最后一个块不完整”。我认为将字节转换为字符串时存在问题,反之亦然。

我很绝望,需要你的帮助。我想要的是将加密文本存储在文本框或文件中,然后使用它来解密文本。

提前谢谢你

4

2 回答 2

0

无需绝望,您可以检查如下:

txtEncr.setText(encrypted.toString());

然后

byte[] txtEncrBytes = txtEncr.getText().toString().getBytes();

通过比较encryptedtxtEncrBytes使用

Arrays.equals(encrypted,txtEncyBytes);

你会发现两个字节数组不相等。

原因是 txtEncr 的文本被加密的 toString(),你的代码在一个数组上调用 toString,这将产生一个相当无用的结果,例如 [C@16f0472. 您至少应该使用 Arrays.toString 将数组转换为提供数组内容的可读字符串。参见Programming Puzzlers,第 3 章,谜题 12。

但请注意:加密的字节数组通常包含与默认平台编码中的字符不对应的字节值。在这种情况下,当您创建 String 时,该字节将被转换为“替换字符”,U+FFFD (�),并且正确的值将无法挽回地丢失。看到这个

所以答案是:只需将您的代码保留为原始版本:

DesEncrypter a= new DesEncrypter();
String a1=txtEncr.getText().toString();
byte[] decrypted = a.decrypt(encrypted);

总之,您应该始终保留加密的字节数组,而不是使用临时字符串保存它然后从临时字符串中检索字节数组。

PS:我认为您可能需要格式化您的帖子,您的代码列表看起来很糟糕。

于 2012-06-15T12:04:22.837 回答
0

存储和传输加密数据字节数组的最简单方法是将其转换为 Base64:

从您的加密方法中获取 encryptedBytes。然后:

String storeThis = Base64.encode(encryptedBytes, Base64.DEFAULT);

然后,当您想将字节取回解密时:

byte[] encryptedBytes = Base64.decode(storeThis, Base64.DEFAULT);

将 encryptedBytes 传递给您的解密方法。

于 2017-01-06T12:08:49.157 回答