login.java




java代码:
package eoe.md5demo;
import android.app.Activity;
import android.os.Bundle;
public class Login extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
}


MD5Demo.java
java代码:
package eoe.md5demo;
import java.security.MessageDigest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MD5Demo extends Activity {
private EditText username,password;
private Button savebtn,loginbtn;
String user,pass;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
savebtn = (Button)findViewById(R.id.save);
loginbtn = (Button)findViewById(R.id.login);
savebtn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
SharedPreferences pre = getSharedPreferences("loginvalue",MODE_WORLD_WRITEABLE);
pass = MD5(password.getText().toString());
user = username.getText().toString();
if(!pass.equals("")&&!user.equals("")){
pre.edit().putString("username", username.getText().toString()).
putString("password",encryptmd5(pass)).commit();
Toast.makeText(getApplicationContext(), "保存成功!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "密码不能为空!", Toast.LENGTH_LONG).show();
}
}
});
loginbtn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
SharedPreferences sp = getSharedPreferences("loginvalue", MODE_WORLD_READABLE);
String loginuser = sp.getString("username", null);
String loginpass = sp.getString("password", null);
user = username.getText().toString();
pass = password.getText().toString();
String passmd5 = MD5(pass);
String encryptmd5 = encryptmd5(passmd5);
System.out.println("username="+loginuser+"-------------password="+loginpass);
System.out.println("user=="+user+"-------------encryptmd5=="+encryptmd5);
if(!user.equals("")&&!pass.equals(""))
{
if( user.equals(loginuser)&& encryptmd5.equals(loginpass))
{
Intent intent = new Intent();
intent.setClass(MD5Demo.this, Login.class);
MD5Demo.this.startActivity(intent);
finish();
}else{
Toast.makeText(getApplicationContext(), "密码是错误的!", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), "密码不能为空!", Toast.LENGTH_LONG).show();
}
}
});
}
//MD5加密,32位
public static String MD5(String str){
MessageDigest md5 = null;
try{
md5 = MessageDigest.getInstance("MD5");
}catch(Exception e){
e.printStackTrace();
return "";
}
char[] charArray = str.toCharArray();
byte[] byteArray = new byte[charArray.length];
for(int i = 0; i < charArray.length; i++){
byteArray[i] = (byte)charArray[i];
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for( int i = 0; i < md5Bytes.length; i++)
{
int val = ((int)md5Bytes[i])&0xff;
if(val < 16)
{
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
// 可逆的加密算法
public static String encryptmd5(String str) {
char[] a = str.toCharArray();
for (int i = 0; i < a.length; i++)
{
a[i] = (char) (a[i] ^ 'l');
}
String s = new String(a);
return s;
}
}













更多相关文章

  1. Android PopupWindow动画效果代码
  2. Android获取高清app图标代码分享
  3. 收藏代码-Android状态栏工具代码
  4. Android 2.1 GPS定位和拍照功能代码
  5. android 震动和提示音的实现代码
  6. Android 内核源代码交叉编译纺
  7. [置顶] Android常用功能代码
  8. android 代码中设置字体大小

随机推荐

  1. Mac系统下对Android(安卓)apk进行反编译
  2. android中隐藏ListView滚动条
  3. 快速实现android手机端多人视频会议直播(
  4. 【原】Android多任务下载,使用Notificatio
  5. 运行时动态注销在AndroidManifest.xml声
  6. android adb root方法
  7. uni-app之APP和小程序微信授权方法
  8. 22、TTS技术
  9. Android(安卓)Toolbar 使用总结
  10. android网络传输的传送对象