Java 如何在android studio中对不同活动的文本进行加密/解密
我试着在android studio中开发一个文本加密/解密应用程序。因此,在MainActivity.java上,我运行了一个加密和解密的示例代码 MainActivity.javaJava 如何在android studio中对不同活动的文本进行加密/解密,java,android,encryption,text,cryptography,Java,Android,Encryption,Text,Cryptography,我试着在android studio中开发一个文本加密/解密应用程序。因此,在MainActivity.java上,我运行了一个加密和解密的示例代码 MainActivity.java import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; impo
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class MainActivity extends AppCompatActivity {
Button btn,btn2;
static final String TAG = "SymmetricAlgorithmAES";
String secr="k";
String secr2="d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code to use my specified defined key
byte[] key = new byte[0];
try {
key = (secr+secr2).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec sks = new SecretKeySpec(key, "AES");
// Original text
String theTestText = "This is just a simple test";
TextView tvorig = (TextView)findViewById(R.id.tvorig);
tvorig.setText("\n[ORIGINAL]:\n" + theTestText + "\n");
// Encode the original data with AES
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, sks);
encodedBytes = c.doFinal(theTestText.getBytes());
} catch (Exception e) {
Log.e(TAG, "AES encryption error");
}
TextView tvencoded = (TextView)findViewById(R.id.tvencoded);
tvencoded.setText("" +
Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");
// Decode the encoded data with AES
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, sks);
decodedBytes = c.doFinal(encodedBytes);
} catch (Exception e) {
Log.e(TAG, "AES decryption error");
}
TextView tvdecoded = (TextView)findViewById(R.id.tvdecoded);
tvdecoded.setText("[DECODED]:\n" + new String(decodedBytes) + "\n");
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Encryption extends AppCompatActivity {
static final String TAG = "SymmetricAlgorithmAES";
String secr="k";
String secr2="d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.encryption);
enc_text_edt=(EditText)findViewById(R.id.enc_text_edt);
enc_text_btn=(Button)findViewById(R.id.enc_text_btn);
enctv=(TextView)findViewById(R.id.enctv);
//code to use my specified defined key
byte[] key = new byte[0];
try {
key = (secr+secr2).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec sks = new SecretKeySpec(key, "AES");
final SecretKeySpec finalSks = sks;
enc_text_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// Encode the original data with AES
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, finalSks);
encodedBytes = c.doFinal(enc_text_edt.getText().toString().getBytes());
} catch (Exception e) {
Log.e(TAG, "AES encryption error");
}
enctv.setText("[ENCRYPTED]:\n" +
Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");
enc_text_edt.setText("");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Decryption extends AppCompatActivity {
Button dec_text_btn;
TextView dec_edtext_view, dectv;
static final String TAG = "SymmetricAlgorithmAES";
String secr = "k";
String secr2 = "d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.decryption);
dec_text_btn = (Button) findViewById(R.id.dec_text_btn);
dec_edtext_view = (EditText) findViewById(R.id.dec_edtext_view);
dectv = (TextView) findViewById(R.id.dectv);
//code to use my specified defined key
byte[] key = new byte[0];
try {
key = (secr + secr2).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec sks = new SecretKeySpec(key, "AES");
final SecretKeySpec finalSks = sks;
dec_text_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
// Decode the encoded data with AES
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, finalSks);
decodedBytes= c.doFinal(dec_edtext_view.getText().toString().getBytes());
} catch (Exception e) {
Log.e(TAG, "AES encryption error");
}
dectv.setText("[DECRYPTED]:\n" + new String(decodedBytes) + "\n");
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "creptography exception see log cat....", Toast.LENGTH_SHORT).show();
}
}
});
}
}
以上代码工作正常,输出正确。但当我试图修改代码并尝试在不同的活动中编写加密和解密时,解密部分无法正常工作
以下是加密部分的代码,该部分工作正常,没有任何错误
Encryption.java
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class MainActivity extends AppCompatActivity {
Button btn,btn2;
static final String TAG = "SymmetricAlgorithmAES";
String secr="k";
String secr2="d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code to use my specified defined key
byte[] key = new byte[0];
try {
key = (secr+secr2).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec sks = new SecretKeySpec(key, "AES");
// Original text
String theTestText = "This is just a simple test";
TextView tvorig = (TextView)findViewById(R.id.tvorig);
tvorig.setText("\n[ORIGINAL]:\n" + theTestText + "\n");
// Encode the original data with AES
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, sks);
encodedBytes = c.doFinal(theTestText.getBytes());
} catch (Exception e) {
Log.e(TAG, "AES encryption error");
}
TextView tvencoded = (TextView)findViewById(R.id.tvencoded);
tvencoded.setText("" +
Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");
// Decode the encoded data with AES
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, sks);
decodedBytes = c.doFinal(encodedBytes);
} catch (Exception e) {
Log.e(TAG, "AES decryption error");
}
TextView tvdecoded = (TextView)findViewById(R.id.tvdecoded);
tvdecoded.setText("[DECODED]:\n" + new String(decodedBytes) + "\n");
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Encryption extends AppCompatActivity {
static final String TAG = "SymmetricAlgorithmAES";
String secr="k";
String secr2="d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.encryption);
enc_text_edt=(EditText)findViewById(R.id.enc_text_edt);
enc_text_btn=(Button)findViewById(R.id.enc_text_btn);
enctv=(TextView)findViewById(R.id.enctv);
//code to use my specified defined key
byte[] key = new byte[0];
try {
key = (secr+secr2).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec sks = new SecretKeySpec(key, "AES");
final SecretKeySpec finalSks = sks;
enc_text_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// Encode the original data with AES
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, finalSks);
encodedBytes = c.doFinal(enc_text_edt.getText().toString().getBytes());
} catch (Exception e) {
Log.e(TAG, "AES encryption error");
}
enctv.setText("[ENCRYPTED]:\n" +
Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");
enc_text_edt.setText("");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Decryption extends AppCompatActivity {
Button dec_text_btn;
TextView dec_edtext_view, dectv;
static final String TAG = "SymmetricAlgorithmAES";
String secr = "k";
String secr2 = "d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.decryption);
dec_text_btn = (Button) findViewById(R.id.dec_text_btn);
dec_edtext_view = (EditText) findViewById(R.id.dec_edtext_view);
dectv = (TextView) findViewById(R.id.dectv);
//code to use my specified defined key
byte[] key = new byte[0];
try {
key = (secr + secr2).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec sks = new SecretKeySpec(key, "AES");
final SecretKeySpec finalSks = sks;
dec_text_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
// Decode the encoded data with AES
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, finalSks);
decodedBytes= c.doFinal(dec_edtext_view.getText().toString().getBytes());
} catch (Exception e) {
Log.e(TAG, "AES encryption error");
}
dectv.setText("[DECRYPTED]:\n" + new String(decodedBytes) + "\n");
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "creptography exception see log cat....", Toast.LENGTH_SHORT).show();
}
}
});
}
}
解密代码
解密.java
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class MainActivity extends AppCompatActivity {
Button btn,btn2;
static final String TAG = "SymmetricAlgorithmAES";
String secr="k";
String secr2="d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code to use my specified defined key
byte[] key = new byte[0];
try {
key = (secr+secr2).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec sks = new SecretKeySpec(key, "AES");
// Original text
String theTestText = "This is just a simple test";
TextView tvorig = (TextView)findViewById(R.id.tvorig);
tvorig.setText("\n[ORIGINAL]:\n" + theTestText + "\n");
// Encode the original data with AES
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, sks);
encodedBytes = c.doFinal(theTestText.getBytes());
} catch (Exception e) {
Log.e(TAG, "AES encryption error");
}
TextView tvencoded = (TextView)findViewById(R.id.tvencoded);
tvencoded.setText("" +
Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");
// Decode the encoded data with AES
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, sks);
decodedBytes = c.doFinal(encodedBytes);
} catch (Exception e) {
Log.e(TAG, "AES decryption error");
}
TextView tvdecoded = (TextView)findViewById(R.id.tvdecoded);
tvdecoded.setText("[DECODED]:\n" + new String(decodedBytes) + "\n");
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Encryption extends AppCompatActivity {
static final String TAG = "SymmetricAlgorithmAES";
String secr="k";
String secr2="d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.encryption);
enc_text_edt=(EditText)findViewById(R.id.enc_text_edt);
enc_text_btn=(Button)findViewById(R.id.enc_text_btn);
enctv=(TextView)findViewById(R.id.enctv);
//code to use my specified defined key
byte[] key = new byte[0];
try {
key = (secr+secr2).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec sks = new SecretKeySpec(key, "AES");
final SecretKeySpec finalSks = sks;
enc_text_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// Encode the original data with AES
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, finalSks);
encodedBytes = c.doFinal(enc_text_edt.getText().toString().getBytes());
} catch (Exception e) {
Log.e(TAG, "AES encryption error");
}
enctv.setText("[ENCRYPTED]:\n" +
Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");
enc_text_edt.setText("");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Decryption extends AppCompatActivity {
Button dec_text_btn;
TextView dec_edtext_view, dectv;
static final String TAG = "SymmetricAlgorithmAES";
String secr = "k";
String secr2 = "d";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.decryption);
dec_text_btn = (Button) findViewById(R.id.dec_text_btn);
dec_edtext_view = (EditText) findViewById(R.id.dec_edtext_view);
dectv = (TextView) findViewById(R.id.dectv);
//code to use my specified defined key
byte[] key = new byte[0];
try {
key = (secr + secr2).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec sks = new SecretKeySpec(key, "AES");
final SecretKeySpec finalSks = sks;
dec_text_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
// Decode the encoded data with AES
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, finalSks);
decodedBytes= c.doFinal(dec_edtext_view.getText().toString().getBytes());
} catch (Exception e) {
Log.e(TAG, "AES encryption error");
}
dectv.setText("[DECRYPTED]:\n" + new String(decodedBytes) + "\n");
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "creptography exception see log cat....", Toast.LENGTH_SHORT).show();
}
}
});
}
}
请帮我纠正这个错误。在执行解密部分时,它直接显示异常“加密异常请参阅日志cat”在阅读您的代码后,我认为我发现了问题,您编码到Base64,但从未解码。在
加密中
执行以下操作
enctv.setText(“[加密]:\n”+
Base64.encodeToString(encodedBytes,Base64.DEFAULT)+“\n”);
我猜用户会将其复制到解密字段,但在他们单击按钮后,您会这样做
decodedBytes=c.doFinal(dec_edtext_view.getText().toString().getBytes());
而不是从Base64解码。
我还想补充几点:
你的安全是不安全的,你几乎没有实现任何安全层时,钥匙在飞机上的网站像这样
注1:
密钥应随机生成,并使用。
您可以通过执行以下操作轻松完成此操作:
byte[]key=新字节[16];
新SecureRandom().nextBytes(密钥);
注2:
如果用户键入了相同的消息,请使用此选项。例如,考虑下面的加密“Hello World”的场景,并将其显示为“ABCFDGHIJK”。现在你再次发送它,它又是“ABCDEFGHIJK”。
对于IV,每次都会不同,只要您为每条消息生成一个新的IV,您就应该将此IV附加到消息中,以便稍后在解密时可以提取它
注3:
在声明密码时
使用的次数超过AES
。
有一篇关于如何提高您的安全性和知识的精彩文章:
注4:
如果发生异常,请不要像什么都没发生一样继续,您应该正确处理它,而不要继续使用依赖于异常原因的代码
注5:
更深入地学习Java,而不是跳到密码学,您的字段应该是私有的,如果您计划稍后使用它,如果您确实检查它是否为null,那么最后不要声明null。不要在get字节中声明“UTF-8”,用一个常量声明a,比如“UTF-8”,这很容易用
字符集来完成。forName(“UTF-8”)
我同意所说的一切
此外,您可能应该从活动中获得所有这些内容,并将其放入助手类中。这样,它将可重用,并且您可以使用如下所示的内容测试它的输入和输出(无需复制和粘贴):
public void myEncryptionTest(){
String message = "This is the message to encrypt and decrypt.";
String pass = "pass";
String encryption = Crypto.myEncrypt(message.getBytes(), pass);
byte[] decryption = Crypto.myDecrypt(encryption, pass);
String decrypted = new String(decryption);
Log.d("****DECRYPTION: ", decrypted);
}
助手类被称为“Crypto”,您正在测试的两个静态函数是“myEncrypt”和“myDecrypt”。logcat包含什么?另外,请检查嵌套的try-catch中是否存在任何异常,并共享它