2011-12-21 60 views
2

嗨我想在我的應用程序中加密和解密給定的編輯文本值。我完成了加密。但是加密值太長。我的代碼是:如何隱藏和解密android中給定的編輯文本值?

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中加密和解密離子的最佳方式?有誰能夠幫助我。提前致謝。

+2

HTTP一些例子://計算器。 com/questions/8397213/encrypt-and-decrypt-data-for-android-app-client/8397465#8397465 – 2011-12-21 10:52:06

+1

這不是一種加密。這是一個哈希算法。哈希算法被設計成不可逆的...看看AES,DES等不是SHA或MD5 ..;) – 2011-12-21 10:52:12

+0

我在Padma Kumar共享的鏈接中看到AES加密... – jlvaquero 2011-12-21 11:08:06

回答

1

通過您的代碼我看到您正在使用SHA加密StringSHAcryptographic hash function。正如coding.mof正確地指出,你不能真的decrypt字符串encrypted使用SHA因爲它是non-reversible。 如果您需要加密某些內容並稍後將其解密,則需要使用諸如AESRSA之類的內容,或者可能是DES,具體取決於適合的內容。

這裏有幾個鏈接,讓你開始:

1)Link 1

2)Link2