文本加密/解密方法Java

时间:2016-04-27 16:36:41

标签: android encryption des

我正在开发Android项目,我必须使用加密和解密方法。场景如下:

  1. SignupActivity中的用户类型密码
  2. 用户密码已加密
  3. 在登录活动中从数据库中检索密码,并且称为解密方法
  4. 我的问题是在步骤3中,它总是从解密返回null。

    代码示例:

    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;
    
        }
    

1 个答案:

答案 0 :(得分:0)

首先,不要使用DES,使用AES。

您可以做的最好的事情就是不保存密码。而是使用密码作为AES的关键。这意味着您的用户必须在每次启动时输入密码,但这是最安全的解决方案。

如果必须在本地保存密码,则需要一致的密钥 - 您的代码会在每个函数调用时生成一个新密钥。在注册时生成新密钥,并使用固定密钥将其加密存储在共享首选项上。

相关问题