在Android Studio中发送加密文本时失败

时间:2017-06-14 13:28:20

标签: java android

我正在构建一个用于加密的Android应用程序&在Android工作室使用AES解密。该应用程序已成功安装在我的Android手机中。

加密功能&解密运行没有问题。但是,当我尝试通过短信发送加密文本时,它失败了。

我也允许在AndroidManifest.xml中发送短信。

以下是MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

import se.simbio.encryption.Encryption;

public final class MainActivity extends Activity {

    private static final String TAG = "Encryption";
    String hasil="";
    String isiText ;
    TextView cipherText ;
    TextView plainText ;
    private Button send;
    private EditText noTelp;

    public String encrypted;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btEnkrip = (Button)findViewById(R.id.btnEncrypt) ;
        Button btDekrip = (Button)findViewById(R.id.btnDecrypt);
        plainText =(TextView)findViewById(R.id.txtEncrypt);
        cipherText =(TextView)findViewById(R.id.txtDecrypt);

        noTelp = (EditText) findViewById(R.id.txtNomor);
        send = (Button) findViewById(R.id.btnSend);

        // Algoritma

        btEnkrip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String hasil = "";
                Encryption encryption = Encryption.getDefault("KeyEAAK", "SaltEAAK",new byte[16]);
                isiText = plainText.getText().toString();

                for (int i = 0; i < isiText.length(); i++) {
                    int index = isiText.charAt(i);
                    char s = (char)(index+1) ;
                    hasil = hasil + String.valueOf(s);
                }

                String encrypted = encryption.encryptOrNull(hasil);

                cipherText.setText(encrypted);
                plainText.setText(null);
            }
        });

        btDekrip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String hasil = "";
                Encryption encryption = Encryption.getDefault("KeyEAAK", "SaltEAAK",new byte[16]);
                isiText = cipherText.getText().toString();
                String decrypted = encryption.decryptOrNull(isiText);

                for (int i = 0 ; i < decrypted.length(); i++) {
                    int index = decrypted.charAt(i);
                    char s = (char) (index - 1);
                    hasil = hasil + String.valueOf(s);
                }
                plainText.setText(hasil);
                cipherText.setText(null);

            }
        });
// SMS Send
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = noTelp.getText().toString();

                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(number,null,encrypted,null,null);
                    Toast.makeText(getApplicationContext(), "SMS Success!",Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "SMS Failed, please try again!", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });

    }
}

如何解决问题?

0 个答案:

没有答案