由于我是加密新手,我被困住了,非常感谢您的帮助。
我有两个类一个用于加密和解密发送的文本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")*);
并使用此值对其进行解密,给我一个错误“解密中的最后一个块不完整”。我认为将字节转换为字符串时存在问题,反之亦然。
我很绝望,需要你的帮助。我想要的是将加密文本存储在文本框或文件中,然后使用它来解密文本。
提前谢谢你