Android 用MD5加密算法加密密码
来源:互联网 发布:无锡知原药业面试 编辑:程序博客网 时间:2024/05/22 01:47
很多的网络相关的软件都需要用户名密码登录,在开发的时候像这些密码都是保存在SharedPreferences中,这些密码保存在/data/data/包名/shared_prefs下,保存在一个XML文件中,如下:
开始说道正题,MD5加密算法虽然现在有些人已经将其解开了,但是它的加密机制依然很强大,我想绝大对数还是不会解开的。MD5加密算法是单向加密,只能用你的密码才能解开,要不就是会解密算法,否则想都别想解开。为了防止这种情况的发生。还可以对加密过的密码进行再次加密。
Log.xml:
MD5Demo.java
开始说道正题,MD5加密算法虽然现在有些人已经将其解开了,但是它的加密机制依然很强大,我想绝大对数还是不会解开的。MD5加密算法是单向加密,只能用你的密码才能解开,要不就是会解密算法,否则想都别想解开。为了防止这种情况的发生。还可以对加密过的密码进行再次加密。
Log.xml:
- <<span style="font-size:18px;">?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:id="@+id/username"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="20dp"
- android:layout_marginRight="10dp"
- android:hint="帐号"
- />
- <EditText
- android:id="@+id/password"
- android:password="true"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:layout_marginRight="10dp"
- android:hint="密码"
- />
- <Button
- android:id="@+id/save"
- android:text="保存"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:layout_marginRight="10dp"
- />
- <Button
- android:id="@+id/login"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:layout_marginRight="10dp"
- android:text="登录"
- />
- </LinearLayout> </span>
- <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="login successful!"
- />
- </LinearLayout> </span>
- <span style="font-size:18px;">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);
- }
- } </span>
- 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;
- }
- }
0 0
- Android 用MD5加密算法加密密码
- Android杂谈---用MD5加密算法加密密码
- Android 用MD5加密算法加密密码
- Android 用MD5加密算法加密密码
- Android 用MD5加密算法加密密码
- Android 用MD5加密算法加密密码
- 【平台开发】-Android使用MD5加密算法加密密码
- Android加密算法之MD5加密
- Android 密码MD5加密
- Android常见加密算法(MD5加密)
- 插入数据时密码使用MD5加密算法加密
- android使用MD5算法加密密码
- Android 指纹解锁和MD5加密密码
- Android网络传输中的加密算法:MD5加密方法
- MD5 密码加密算法 系统等待
- MD5加密算法--->java使用md5存储密码
- ASP.NET下用MD5加密密码
- 用java实现MD5密码加密
- Unity3D内置Shader翻译二
- C语言中两个实用的宏定义__DATE__和__TIME__
- Android-Fragment的生命周期
- 第四周——数组作数据结构(扩展2)
- java.lang.ClassCastException: android.app.Application cannot be cast to serialport_api.sample.App问题
- Android 用MD5加密算法加密密码
- android listview 显示指定数量的item
- eclipse和myeclipse关联java jar包以及源文件,doc文件
- 总结 deep learning matlab 练习 相关函数总结
- 松鼠在女孩头发安营扎寨 聚焦那些大到惊人的动物
- SSL,HTTPS
- 关于多线程编程您不知道的 5 件事
- poj——2031——Building a Space Station
- /MT、/MD编译选项,以及可能引起在不同堆中申请、释放内存的问题