- 浏览: 752526 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (139)
- 玩转Android (48)
- Android创意美工 (0)
- Android杂谈 (23)
- Android实例练习 (2)
- Android ROM研究 (5)
- Android NDK开发指南 (5)
- Android NDK (0)
- Android Tips (3)
- Windows Phone 7 (5)
- iPhone (0)
- HTML5学习室 (0)
- JAVA (9)
- SSH+ibatis (8)
- PHP (0)
- IT生活 (1)
- linux (2)
- C (4)
- C++ (1)
- web 前端 (1)
- 云计算 (0)
- 设计模式 (0)
- C# (2)
- 其他 (1)
- 数据结构 (5)
- Web开发 (10)
- 数据库 (3)
- 搜索引擎 (0)
- Go语言 (0)
最新评论
-
wi100sh:
多谢分享~
玩转Android---UI篇---ImageButton(带图标的按钮) -
zhanghaichang:
好文章的。
高性能web开发技术(一) -
yingang:
引用classes.dex.dex2jar.jar 拖入 j ...
Andorid杂谈---Apk文件的反编译 -
扶摇诺:
讲解的简明易懂,多谢啦!
玩转Android---UI篇---LinearLayout(线性布局) -
a13429921973:
更为详细的图文介绍,可参考这个http://blog.csdn ...
Android ROM研究---CyanogenMod源代码下载及编译
很多的网络相关的软件都需要用户名密码登录,在开发的时候像这些密码都是保存在SharedPreferences中,这些密码保存在/data/data/包名/shared_prefs下,保存在一个XML文件中,如下:
可以用FileBrower查看
开始说道正题,MD5加密算法虽然现在有些人已经将其解开了,但是它的加密机制依然很强大,我想绝大对数还是不会解开的。MD5加密算法是单向加密,只能用你的密码才能解开,要不就是会解密算法,否则想都别想解开。为了防止这种情况的发生。还可以对加密过的密码进行再次加密。
下面是个小例子:
main.xml
<?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>
login.xml
<?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>
login.java
package com.loulijun.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
package com.loulijun.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; } }
程序很简单,下面是运行的效果:
发表评论
-
Android版本自动飞行模式小蜜
2012-05-31 15:21 527可以定时切换飞行模式的一款小工具,喜欢的下! 自动飞行模式小 ... -
Android杂谈--关于解析包时错误解决方案
2012-03-21 17:17 6020最近在一个深度定制的产品上运行apk软件,由于产品所使用的系统 ... -
Android杂谈--代码混淆及proguard一些错误处理
2011-12-12 17:01 4045代码混淆说简单的其实就是为了防止别人反编译你的源代码,因为JA ... -
Android杂谈--闹钟详谈
2011-12-11 15:07 15647闹钟已经学过一段时间了,但是对它了解的不是很多,由于最近开发的 ... -
Android杂谈--闹钟详谈
2011-12-10 22:23 2闹钟已经学过一段时间了,但是对它了解的不是很多,由于最近开发的 ... -
Android内存泄露调试分享
2011-10-21 22:34 3185各位兄弟姐妹,Java开发中的内存泄露的问题经常会给 ... -
Android杂谈--开发游戏时选择游戏级别
2011-09-01 13:23 1431在开发游戏的时候,开始游戏时需要几个难度选择的选项供用户选择。 ... -
Android杂谈--Android生命周期
2011-09-01 10:48 2577引言 应用程序组件 ... -
Android杂谈--获取系统及应用程序(PackageManager)
2011-08-27 19:39 5612PackageManager是个非常好的东西,其他的详细的细节 ... -
Androidz杂谈---在ContextMenu中添加/删除ListView的Item
2011-08-16 16:28 2986我们在写Android程序的时候,免不了要使用ListView ... -
Android杂谈---各种Toast
2011-07-30 21:58 1575相信各位对这个Toast已经了解的差不多了,不过我们还可以定义 ... -
Android杂谈---设置模拟器壁纸
2011-07-25 20:34 1567天天看着模拟器里面的那个图片感觉很恼火,所以干脆想将其换掉,顺 ... -
Android杂谈---TextView的跑马灯效果
2011-07-24 17:45 3369下面的是转载自农民伯伯的文章,但是有些属性还不是很清楚,所以又 ... -
Android杂谈---layout_x与layout_y的正确使用
2011-05-26 22:22 3041<?xml version="1.0" ... -
Android杂谈---分享eoeAndroid第1--16期资源
2011-05-26 22:00 2072Android杂谈---分享eoeAndroid第1--16期 ... -
Android杂谈---关于drawable文件夹的错误
2011-05-26 13:13 4973今天做一个东西的时候,用PS做了几个图片,但是更改了后缀,于是 ... -
Android杂谈---获取手机屏幕大小
2011-05-20 23:04 1601开发手机应用程序的时候,除了底层对API的掌握外,最重要的仍是 ... -
Android杂谈---TextView的12种文字颜色
2011-05-20 22:35 6064要想设置Android的TextView控件不同的颜色有两种方 ... -
Andorid杂谈---Apk文件的反编译
2011-04-09 14:53 37721、 首先是将下载到 ... -
Android杂谈---带图片的Toast
2011-03-24 21:28 2154当需要提示的时候,我们可以用Toast来显示信息 如: T ...
相关推荐
NULL 博文链接:https://hualang.iteye.com/blog/1304054
NULL 博文链接:https://hualang.iteye.com/blog/1125355
项目管理故事2-实施策略杂谈-开元项目实施.ppt
swing开发杂谈--初版本程序源码 http://blog.csdn.net/ssrc0604hx/article/details/8737673
程序设计经验杂谈-给予帮助没,同时此文档是一位大师谢的,很经典。。
软件工程 (Software Engineering,简称为SE)是一门研究用工程化方法构建和维护有效的、实用的和高质量的软件的学科。它涉及到程序设计语言,数据库,软件开发工具,系统平台,标准,设计模式等方面。 请看 软件工程...
「安全管理」Linux_HIDS杂谈 - WEB应用防火墙 安全管理 安全资讯 安全开发 零信任 身份管理
java杂谈-一个计算机专业学生几年的编程经验汇总谈.pdf
作业指导书编制规范,作业指导书编制的各部分内容要点 - 仓库管理杂谈 - 库管易
java杂谈-一个计算机专业学生几年的编程经验汇总谈实用.pdf
NULL 博文链接:https://marlboro027.iteye.com/blog/960954
并行计算是当今热门的一个技术,本文档简单介绍了多核多线程的入门知识,可以作为初学者入门的好材料。
嵌入式开发必须要知道的知识点,主要是要点方面的总结,很给力!
。。。
。。。
Android开发杂谈