Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的加密与解密_Android_Android Intent - Fatal编程技术网

android中的加密与解密

android中的加密与解密,android,android-intent,Android,Android Intent,由于我是一名加密新手,我感到很受挫,非常感谢您的帮助 我有两个类,第一类用于加密和解密从main.xml base64活动: public class Base64Activity extends Activity { private Button btnEncrypt, btnDecrypt; private EditText txtOrg, txtEncr, txtEncr1, txtDecr; private byte[] encrypted; /** Called when the ac

由于我是一名加密新手,我感到很受挫,非常感谢您的帮助

我有两个类,第一类用于加密和解密从
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;
}
}

去加密器:

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")*);
然后用这个值来解密它,给我一个错误“解密中最后一个块未完成”。我认为在将字节转换为字符串以及将字节转换为字符串时会出现问题

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


提前感谢您

无需绝望,您可以查看以下内容:

txtEncr.setText(encrypted.toString());
然后

通过使用

您将发现两个字节数组不相等

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

但请注意:加密字节数组通常包含与默认平台编码中的字符不对应的字节值。在这种情况下,当您创建字符串时,字节将被转换为“替换字符”,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);

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

谢谢您的回复,您的建议已被高度接受。但我的要求仍未满足。我希望将加密数据保存在文件存储或数据库中。是否有方法将字节数组以其原始形式保存到外部存储?也许您可以将加密数据写入文件使用OutputStream打开SD卡上的文件。确保在AndroidManifest.xml中声明android.permission.WRITE_EXTERNAL_存储权限