2

Hi I want to encrypt and decrypt the given edit text value in my application. I completed encryption. But the encryption value is too long. My code is:

  import java.security.MessageDigest;
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;

  public class EncodeAndDEcode extends Activity 
  {
TextView txt,encry,decry;
TextView encrypt_txt,decrypt_txt;
Button encrypt_but,decrypt_but;
EditText text;
String my_text="";
static String myString1="";

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    txt=(TextView)findViewById(R.id.tv1);
    encry=(TextView)findViewById(R.id.tv2);
    decry=(TextView)findViewById(R.id.tv3);
    encrypt_txt=(TextView)findViewById(R.id.tv4);
    decrypt_txt=(TextView)findViewById(R.id.tv5);

    text=(EditText)findViewById(R.id.et1);

    decrypt_but=(Button)findViewById(R.id.bt1);
    encrypt_but=(Button)findViewById(R.id.bt2);


    encrypt_but.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v)
        {
            System.out.println("Encrypt button has been clicked");
            my_text=text.getText().toString();
            System.out.println("My string is---> "+my_text);

          // myEncrypt(my_text);
          encrypt_txt.setText(myEncrypt(my_text));


        }
    });  


    decrypt_but.setOnClickListener(new View.OnClickListener()
    {           
        @Override
        public void onClick(View v) 
        {
            System.out.println("Decrypt button has been clicked");


        }
    });

}




public static String myEncrypt(String data1)
{
    StringBuffer sb = new StringBuffer();
    try 
    {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
        messageDigest.update(data1.getBytes("UTF-8"));
        byte[] digestBytes = messageDigest.digest();

        String hex = null;
        for (int i = 0; i < digestBytes.length; i++) 
        {
            hex = Integer.toHexString(0xFF & digestBytes[i]);
            if (hex.length() < 2) 
                sb.append("0");
            sb.append(hex);
            }
       myString1 = sb.toString();
        System.out.println(myString1);
        }
    catch (Exception ex) 
    {
        System.out.println(ex.getMessage());
        }
   return new String(sb);
} }

I got the encrypt value like this

  da7b83206f136b263d2cf0ff968aa6301bdc002101e4dd980832411b67cb54cfcb9e862c8f3344abb72741c2312b84b562d623676b2af49913f486f1b4cef73a

Now I want to decrypt this encryption. How can i do this? Can anybody suggest the best way of encryption and decrypt ion in android? Can anybody help me. Thanks in advance.

malavika
  • 1,331
  • 4
  • 21
  • 54

2 Answers2

1

Going by your code I see that you are using SHA for encrypting the String. SHA is a cryptographic hash function. As coding.mof rightly pointed out that you can't really decryptthe string encrypted using SHA as it is non-reversible. If you need to encrypt something and later decrypt it, you need to use something like AES or RSA or may be DES depending upon what are comfortable with.

Here are few links to get you started :

1) Link 1

2) Link2

Community
  • 1
  • 1
Parth Doshi
  • 4,200
  • 15
  • 79
  • 129
0

I found some examples in the following link http://www.tutorials-android.com/learn/How_to_encrypt_and_decrypt_strings.rhtml