0
私は暗号化と復号化の両方の方法を使用する必要があるAndroidプロジェクトに取り組んでいます。以下のようなシナリオ:SignupActivityでテキスト暗号化/復号化方法Java
- ユーザータイプのパスワードが
- ユーザーのパスワードがログイン・アクティビティー・パスワードで は、データベースから取得され、復号化方法は
私の問題がステップであると呼ばれる暗号化されています3それは常に解読からヌルを返します。
コードサンプル:
SignUpActivity:
String name_data = name.getText().toString();
String email_data = email.getText().toString();
String password_data = password.getText().toString();
password_data = enc.getEncryptedText(password_data);
LoginActivity
String password_in_database = helper.searchPassword(email_data);
password_in_database = enc.getDecryptedText(password_in_database);
暗号化/復号クラス
public class EncryptDecryptStringWithDES {
public static Cipher ecipher;
public static Cipher dcipher;
public static SecretKey key;
public static String getEncryptedText(String sty) throws Exception {
// generate secret key using DES algorithm
key = KeyGenerator.getInstance("DES").generateKey();
ecipher = Cipher.getInstance("DES");
// initialize the ciphers with the given key
ecipher.init(Cipher.ENCRYPT_MODE, key);
sty = encrypt(sty);
return sty;
}
public static String getDecryptedText(String sty) throws Exception {
key = KeyGenerator.getInstance("DES").generateKey();
dcipher = Cipher.getInstance("DES");
dcipher.init(Cipher.DECRYPT_MODE, key);
sty = decrypt(sty);
return sty;
}
public static String encrypt(String str) {
try {
// encode the string into a sequence of bytes using the named charset
// storing the result into a new byte array.
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
// encode to base64
enc = BASE64EncoderStream.encode(enc);
return new String(enc);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String str) {
try {
// decode with base64 to get bytes
byte[] dec = BASE64DecoderStream.decode(str.getBytes());
byte[] utf8 = dcipher.doFinal(dec);
// create new string based on the specified charset
return new String(utf8, "UTF8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
私のプロジェクトはパスワードマネージャアプリケーションです。ユーザーのパスワードを保存し、すべてのパスワードを同時に一覧表示します。なぜ私は暗号化と解読が必要なのか。 私は問題は常に両方の方法のために異なるキーを生成すると思う –
デバッグ:16進数のキーをダンプします。どのように暗号鍵を安全に保存しますか? DESを使用しないでください。安全ではないと考えられているため、AESを使用してください。 – zaph