MD5 Encryption in Android
1.Md5.javapackage com.JavaPages4All.md5encryption; import java.security.MessageDigest; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Md5 extends Activity { EditText textview; Button get; TextView textlbl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.md5); textview=(EditText)findViewById(R.id.textview); get=(Button)findViewById(R.id.get); textlbl=(TextView)findViewById(R.id.textlbl); get.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String strpassword=textview.getText().toString(); if(strpassword!=null && strpassword.trim().length()!=0) { String md5=getMD5Encryption(strpassword); textlbl.setText(md5); } } }); } public String getMD5Encryption(String strpassword) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(strpassword.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); for (int i = 0; i < messageDigest.length; i++) { String h = Integer.toHexString(0xFF & messageDigest[i]); while (h.length() < 2) h = "0" + h; hexString.append(h); } return hexString.toString(); } catch (Exception e) { e.printStackTrace(); } return ""; } } |
2.md5.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter String"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get MD5" />
<TextView
android:id="@+id/textlbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Md5 string"
android:textColor="#ff00ff"
android:textSize="20sp" />
</LinearLayout>
No comments:
Post a Comment