尝试使用AES方案制作一个加密和解密文本的应用程序

时间:2017-12-20 08:21:51

标签: java android string encryption aes

在这个应用程序中,加密按钮以所需的功能运行,但解密按钮没有显示任何输出。在解密函数执行后,它返回一个空字符串,我没有得到错误,也没有错误。我认为有任何逻辑错误。

*全局变量和点击侦听器的代码是:*

 EditText input_text,password_text;
    TextView output_text;
    Button enc,dec;
    String outputstring;
    String AES="AES";
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input_text =(EditText) findViewById(R.id.input_text);
        password_text=(EditText) findViewById(R.id.password_text);
        output_text=(TextView) findViewById(R.id.output_text);
        enc=(Button) findViewById(R.id.encrypt);
        dec=(Button) findViewById(R.id.decrypt);
        final String pwdtext=password_text.getText().toString();
        final String inptext=input_text.getText().toString();

        enc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               try{
                   outputstring=encrypt(inptext,pwdtext);
                output_text.setText(outputstring);
               } catch (Exception e)
               {
                   e.printStackTrace();
               }
            }
        });

        dec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             try{
                 outputstring=decrypt(outputstring,pwdtext);
                 output_text.setText(outputstring);
             } catch (Exception e)
             {
                 e.printStackTrace();
             }

            }
        });
    }

加密方法ID的代码:

 private String encrypt(String data,String password_text) throws Exception
{
    SecretKeySpec key=generateKey(password_text);
    Cipher c=Cipher.getInstance(AES);
    c.init(Cipher.ENCRYPT_MODE,key);
    byte[] encVal=c.doFinal(data.getBytes());
    String encryptedvalue= Base64.encodeToString(encVal,Base64.DEFAULT);
    return encryptedvalue;
}

解密方法的代码是:

private String decrypt(String outputstring,String password_text) throws Exception
{
    SecretKeySpec key=generateKey(password_text);
    Cipher c =Cipher.getInstance(AES);
    c.init(Cipher.DECRYPT_MODE,key);
    byte[] decodedvalue =Base64.decode(outputstring,Base64.DEFAULT);
    byte[] decvalue=c.doFinal(decodedvalue);
    String decryptedvalue=new String(decvalue);
    return decryptedvalue;
}

生成密钥方法的代码是:

 private SecretKeySpec generateKey(String password) throws Exception{
    final MessageDigest digest=MessageDigest.getInstance("SHA-256");
    byte[] bytes=password.getBytes("UTF-8");
    digest.update(bytes,0,bytes.length);
    byte[] key=digest.digest();
    SecretKeySpec secretKeySpec=new SecretKeySpec(key,"AES");
    return secretKeySpec;
}

2 个答案:

答案 0 :(得分:0)

我在最后设置你的代码,这是我尝试过的,现在正在运行:

package com.example.stackoverflow;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class MainActivity extends AppCompatActivity {
String outputstring = "This is my output data";
String passwordText = "Nikhil Lotke";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        outputstring = encrypt(outputstring,passwordText);
    } catch (Exception e) {
        e.printStackTrace();
    }

    TextView dec = (TextView)findViewById(R.id.dec);
    dec.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try{
                Log.d("NIKHIL",decrypt(outputstring,passwordText));

            } catch (Exception e)
            {
                e.printStackTrace();
            }

        }
    });
}

private String encrypt(String data,String password_text) throws Exception
{
    SecretKeySpec key=generateKey(password_text);
    Log.d("NIKHIL","encrypt key:"+key.toString());
    Cipher c=Cipher.getInstance("AES/ECB/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE,key);
    byte[] encVal=c.doFinal(data.getBytes("UTF-8"));
    String encryptedvalue= Base64.encodeToString(encVal,Base64.DEFAULT);
    return encryptedvalue;

}

private String decrypt(String data, String password_text) throws Exception
{
    SecretKeySpec key=generateKey(password_text);
    Log.d("NIKHIL","encrypt key:"+key.toString());
        Cipher c =Cipher.getInstance("AES/ECB/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE,key);
    byte[] decodedvalue =Base64.decode(data,Base64.DEFAULT);
    byte[] decvalue=c.doFinal(decodedvalue);
    String decryptedvalue=new String(decvalue,"UTF-8");
    return decryptedvalue;
}

private SecretKeySpec generateKey(String password) throws Exception{
    final MessageDigest digest=MessageDigest.getInstance("SHA-256");
    byte[] bytes=password.getBytes("UTF-8");
    digest.update(bytes,0,bytes.length);
    byte[] key=digest.digest();
    SecretKeySpec secretKeySpec=new SecretKeySpec(key,"AES");
    return secretKeySpec;
}
}

您遗失的内容是:

  1. Cipher c = Cipher.getInstance(" AES / ECB / PKCS5Padding");在加密和解密功能中
  2. doFinal中的UTF-8 ... byte [] encVal = c.doFinal(data.getBytes(" UTF-8"));

答案 1 :(得分:0)

根据您的最新代码,我在我的结尾再次设置相同的内容,下面的工作代码应该100%工作

package com.example.stackoverflow;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESEncryptDecryptNew extends AppCompatActivity {
EditText input_text, password_text;
TextView output_text;
Button enc, dec;
String outputstring;
String AES = "AES";
String pwdtext;
String inptext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    input_text = (EditText) findViewById(R.id.input_text);
    password_text = (EditText) findViewById(R.id.password_text);
    output_text = (TextView) findViewById(R.id.output_text);
    enc = (Button) findViewById(R.id.encrypt);
    dec = (Button) findViewById(R.id.decrypt);

    enc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                inptext = input_text.getText().toString();
                pwdtext = password_text.getText().toString();
                outputstring = encrypt(inptext, pwdtext);
                output_text.setText(outputstring);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


    dec.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                inptext = input_text.getText().toString();
                pwdtext = password_text.getText().toString();
                outputstring = decrypt(outputstring, pwdtext);
                output_text.setText(outputstring);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });
}

private String encrypt(String data, String password_text) throws Exception {
    SecretKeySpec key = generateKey(password_text);
    Log.d("NIKHIL", "encrypt key:" + key.toString());
    Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(data.getBytes("UTF-8"));
    String encryptedvalue = Base64.encodeToString(encVal, Base64.DEFAULT);
    return encryptedvalue;

}

private String decrypt(String data, String password_text) throws Exception {
    SecretKeySpec key = generateKey(password_text);
    Log.d("NIKHIL", "encrypt key:" + key.toString());
    Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decodedvalue = Base64.decode(data, Base64.DEFAULT);
    byte[] decvalue = c.doFinal(decodedvalue);
    String decryptedvalue = new String(decvalue, "UTF-8");
    return decryptedvalue;
}

private SecretKeySpec generateKey(String password) throws Exception {
    final MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] bytes = password.getBytes("UTF-8");
    digest.update(bytes, 0, bytes.length);
    byte[] key = digest.digest();
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    return secretKeySpec;
}
}

更新后的代码中缺少的内容是: 1.您在加密或解密按钮时不考虑输入密码。 2.您应该为pwdtext和inptext

创建全局变量

希望现在很清楚。 如果这样有效,那么请批准答案