【问题标题】:How to crypt and decrypt the given edit text value in android?如何在android中加密和解密给定的编辑文本值?
【发布时间】:2011-12-21 10:46:31
【问题描述】:

您好,我想在我的应用程序中加密和解密给定的编辑文本值。我完成了加密。但是加密值太长了。我的代码是:

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

我得到了这样的加密值

  da7b83206f136b263d2cf0ff968aa6301bdc002101e4dd980832411b67cb54cfcb9e862c8f3344abb72741c2312b84b562d623676b2af49913f486f1b4cef73a

现在我想解密这个加密。我怎样才能做到这一点?任何人都可以建议在android中加密和解密离子的最佳方法吗?有谁能够帮我。提前致谢。

【问题讨论】:

  • 这不是加密。这是一种哈希算法。哈希算法被设计为不可逆的......看看AES,DES等,而不是SHA或MD5......;)
  • 我在 Padma Kumar 分享的链接中看到 AES 加密...

标签: java android encryption


【解决方案1】:

按照您的代码,我看到您正在使用SHA 来加密StringSHAcryptographic hash function。正如coding.mof 正确指出的那样,您不能真正使用SHA decrypt字符串encrypted,因为它是non-reversible。 如果您需要加密然后解密它,您需要使用AESRSA 之类的东西,或者可能是DES,这取决于您喜欢什么。

这里有几个链接可以帮助您入门:

1)Link 1

2)Link2

【讨论】:

    【解决方案2】:

    【讨论】:

    • 链接似乎已经死了,至少对我来说是这样。
    猜你喜欢
    • 2011-05-15
    • 2013-08-16
    • 2015-02-01
    • 1970-01-01
    • 2017-08-18
    • 2012-01-01
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多