android 使用DigestUtilsmd5加密的方法

直接上代码吧。注意导入apache commons-codec的jar包。

1、布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hellom.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/notic"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/notic01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/notic"
        android:layout_below="@+id/notic"
        android:layout_marginTop="24dp"
        android:text="@string/notic01" />

    <EditText
        android:id="@+id/textIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/notic01"
        android:layout_alignBottom="@+id/notic01"
        android:layout_marginLeft="19dp"
        android:layout_toRightOf="@+id/notic01"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/ciphertext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/notic01"
        android:layout_below="@+id/textIn"
        android:layout_marginTop="33dp"
        android:text="@string/notic02" />

    <TextView
        android:id="@+id/ciphertext01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ciphertext"
        android:layout_alignBottom="@+id/ciphertext"
        android:layout_alignLeft="@+id/textIn"
        android:layout_alignRight="@+id/textIn"
        android:text="" />

    <TextView
        android:id="@+id/textOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ciphertext"
        android:layout_marginTop="23dp"
        android:text="@string/notic04" />

    <TextView
        android:id="@+id/text03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textOut"
        android:layout_alignLeft="@+id/ciphertext01"
        android:layout_alignRight="@+id/ciphertext01"
        android:text="" />

    <Button
        android:id="@+id/translation01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textOut"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="97dp"
        android:layout_marginLeft="16dp"
        android:text="@string/notic03" />

    <Button
        android:id="@+id/translation02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/translation01"
        android:layout_alignRight="@+id/text03"
        android:layout_marginRight="37dp"
        android:text="@string/notic05" />

</RelativeLayout>

2、mainActivity

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null)
        {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

3、fragment

public class PlaceholderFragment extends Fragment
{
    //明文
    public static EditText text01;
    //密文
    public static TextView text02;
    //测试明文
    public static TextView text03;
    //加密事件按钮
    public static Button translationBtn01;
    //解密事件按钮
    public static Button translationBtn02;
    

    public PlaceholderFragment()
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        text01 = (EditText) rootView.findViewById(R.id.textIn);
        text02 = (TextView) rootView.findViewById(R.id.ciphertext01);
        text03 = (TextView) rootView.findViewById(R.id.text03);
        translationBtn01 = (Button) rootView.findViewById(R.id.translation01);
        translationBtn02 = (Button) rootView.findViewById(R.id.translation02);
        translationBtn02.setEnabled(false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity)
    {
        // TODO Auto-generated method stub
        super.onAttach(activity);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        
        translationBtn01.setOnClickListener(new BtnOnClickListener(getActivity()));
        translationBtn02.setOnClickListener(new BtnOnClickListener(getActivity()));
    }
    
    
}

3、fragment中的按钮事件类(独立类的缺点是需要较多的static变量)

public class BtnOnClickListener implements OnClickListener
{

    private Context context;

    public BtnOnClickListener(Context context)
    {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub

        int id = v.getId();
        switch (id)
        {
        // 加密按钮事件按钮
        case R.id.translation01:
        {

            if (TextUtils.isEmpty(PlaceholderFragment.text01.getText()))
            {
                Toast.makeText(context, "请输入需要加密的内容", Toast.LENGTH_SHORT)
                        .show();
            } else
            {

                String text = PlaceholderFragment.text01.getText().toString()
                        .trim();
                Toast.makeText(context, "开始加密", Toast.LENGTH_SHORT).show();

                String text01 = new String(Hex.encodeHex(DigestUtils.md5(text)));

                PlaceholderFragment.text02.setText(text01);
                PlaceholderFragment.translationBtn02.setEnabled(true);
                PlaceholderFragment.translationBtn01.setEnabled(false);

            }
        }
            break;

        case R.id.translation02:
        {
            Toast.makeText(context, "开始解密", Toast.LENGTH_SHORT).show();
            
            PlaceholderFragment.text03.setText("MD5为不可逆加密,可尝试Base64.");
            PlaceholderFragment.translationBtn02.setEnabled(false);
            PlaceholderFragment.translationBtn01.setEnabled(true);
        }
            break;

        default:
            break;
        }
    }

}
时间: 2024-08-08 22:45:34

android 使用DigestUtilsmd5加密的方法的相关文章

C#/IOS/Android通用加密解密方法

原文:C#/IOS/Android通用加密解密方法 公司在做移动端ios/android,服务器提供接口使用的.net,用到加密解密这一块,也在网上找了一些方法,有些是.net加密了android解密不了,或者反之.下面的是三个平台都可以加密解密的方法.加密解密中用到的key="1234578";在调取方法时传值即可. C#代码 #region 跨平台加解密(c#) /// <summary> /// 对字符串进行DES加密 /// </summary> ///

深入理解Android之设备加密Device Encryption

深入理解Android之设备加密Device Encryption Android从4.4开始就支持一项功能,那就是对设备进行加密.加密自然是为了安全性考虑,由于/system目录是只读的,手机里那些存储设备分区中需要保护的就剩下/data/分区和sdcard了.显然,/data/和sdcard大量存储了用户数据(比如app运行时存储的数据),对他们进行加密非常非常有必要. Android 5.0发布后,为了Android设备在企业中的使用,设备加密这个功能默认就必须启用,但是加密这玩意还是对功

Android开发周报:Android L默认加密用户数据

Android开发周报:Android L默认加密用户数据 新闻 <iCloud前车之鉴,Android L默认开启加密功能>:iCloud 艳照风波再起,第二波女星照片流出,大量女星的裸照又开始在社交网站疯传,本次大规模的“艳照门”依然有可能是黑客利用苹果iCloud云端系统的漏洞, 在破解了女艺人们所设的简单密码后侵入并非法盗取了裸照,继而在网络论坛发布.注重保护用户的隐私,是厂商们义不容辞的责任,谷歌宣布Android L上将默认加密用户数据. <苹果出了个指南教你怎么从Andro

Android传输数据时加密详解

Android传输数据时加密详解 ONE Goal , ONE Passion ! ------–MD5加密------- MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有MD5实现.将数据(如汉字)运算为另一固定长度值,是杂凑算法的基础原理,MD5的前身有MD2.MD3和MD4 MD5算法具有以下特点: 1.压缩性:任意长度的数据,算出的MD5值长度都是固定

Android Sqlite数据库加密

Android使用的是开源的SQLite数据库,数据库本身没有加密,加密思路通常有两个: 1. 对几个关键的字段使用加密算法,再存入数据库 2. 对整个数据库进行加密 SQLite数据库加密工具: 收费工具: SSE(SQLite Encryption Extension) 免费工具: SQLCipher SQLCipher使用: SQLCipher是完全开源的软件,提供256-bit AES加密 源码编译: 1. OpenSSL编译 SQLCipher源码编译需要依赖OpenSSL提供的lib

Android模拟器设置网络代理方法

在服务器上启动模拟器的时候加了--http-proxy,但是不起作用.所以搜了下面的方法 四种方法: 一:将网络连接代理设置写入配置数据库 (适合启动模拟器无界面) 1.通过命令行或者通过双击emulatoer可执行文件,打开模拟器 2.在命令行执行adb shell 打开android的控制台 (确保环境变量设置正确,即已经把Android_SDK/tools 添加到了PATH(Linux)/path(Windows)) 3.执行 ls -l /data/data/com.android.pr

Android异步任务的使用方法

Android上面的很多操作是不能直接放在ui线程上面的.当ui线程被阻塞5秒以上的时候应用会出现未响应的对话框过.当此现象出现的时候会直接影响用户的用户体验的.所以我们需要通过方法对异步任务或者操作进行相关的处理.在这里罗列出集中相关的处理方法: 首先我们最先想到的应该是使用Thread+Handler实现非UI线程更新UI界面,即在线程执行的时候通过发送message来通过Handler来处理和更新ui. 另外一种方法就是通过异步任务:AsyncTask来进行数据的处理,通过继承AsyncT

C# MD5加密的方法和一般处理程序使用Session

1.MD5加密 string md5Str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5"); 2.一般处理程序使用Session 需要该类集成 System.Web.SessionState.IRequiresSessionState 接口,使用的时候context.Session["uName"] = uName; C# MD5加密的方法

Android获取LayoutInflater对象的方法总结

在写Android程序时,有时候会编写自定义的View,使用Inflater对象来将布局文件解析成一个View.本文主要目的是总结获取LayoutInflater对象的方法. 1.若能获取context对象,可以有以下几种方法: LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View child = inflater.inflate(R.la