码迷,mamicode.com
首页 > 移动开发 > 详细

[android]DES/3DES/AES加密方式

时间:2015-07-01 16:08:33      阅读:607      评论:0      收藏:0      [点我收藏+]

标签:

DES 支持8位加密解密,3Des支持24位,Aes支持32位。3Des是Des算法做三次。位数的单位是字节byte,不是bits。

3Des是把24位分成3组,第一组八位用来加密,第二组8位用于解密,第三组8位用于加密,所以,如果秘钥为123456781234567812345678(3组1-8),则相当于做了一次12345678的Des加密。例如:第一次用12345678秘钥对123进行加密得到 "LDiFUdf0iew=",然后用第二组的12345678对其进行解密(逆向加密过程),得到了123,第三次又一次加密得到 "LDiFUdf0iew="。


三种加密方式代码里不同的地方:

byte temp[] = new byte[位数];

SecretKey des_key = new SecretKeySpec(temp, "加密算法");

加密算法名对应的是:Aes Des Desede

改了这两个地方就可以实现不同加密方式。加密方式底层不一样,DES是把原文编程2进制的一串01,然后和密文做运算,交换什么什么位置。

<span style="font-size:18px;">public class MainActivity extends Activity implements OnClickListener{

	private EditText des_key,des_src,des_dst;
	private Button btn_encode,btn_decode;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        des_key = (EditText) findViewById(R.id.des_key);
        des_src = (EditText) findViewById(R.id.des_src);
        des_dst = (EditText) findViewById(R.id.des_dst);
        btn_encode = (Button) findViewById(R.id.btn_encode);
        btn_decode = (Button) findViewById(R.id.btn_decode);
        btn_encode.setOnClickListener(this);
        btn_decode.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		String key_str = des_key.getText().toString();
		if(!TextUtils.isEmpty(key_str)){
			try {
				//DES不管长了短了,都变成八位
				byte temp[] = new byte[8];
				byte b[] = key_str.getBytes("UTF-8"); 
				System.arraycopy(b, 0, temp, 0, Math.min(b.length, temp.length));
				//Des只支持八位
				SecretKey des_key = new SecretKeySpec(temp, "Des");
				Cipher cipher = Cipher.getInstance("Des");
				switch (v.getId()) {
				case R.id.btn_encode:
					String src_str = des_src.getText().toString();
					if(!TextUtils.isEmpty(src_str)){
						cipher.init(Cipher.ENCRYPT_MODE, des_key);//设置Cipher为加密
						byte[] bytes = cipher.doFinal(src_str.getBytes("UTF-8"));
						des_dst.setText(Base64.encodeToString(bytes, Base64.DEFAULT));</span><pre name="code" class="java"><span style="font-size:18px;">//是用Base64编码的二进制字节数组</span>
}break;case R.id.btn_decode:String dst_str = des_dst.getText().toString();if(!TextUtils.isEmpty(dst_str)){cipher.init(Cipher.DECRYPT_MODE, des_key);//设置Cipher为解密byte[] bytes = cipher.doFinal(Base64.decode(dst_str, Base64.DEFAULT));//是用Base64编码的二进制字节数组des_src.setText(new String(bytes,"UTF-8"));}break;}} catch (Exception e) {e.printStackTrace();} }}}



布局:

<span style="font-size:18px;"><LinearLayout 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:orientation="vertical" >

    <EditText
        android:id="@+id/des_key"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="秘钥" />

    <EditText
        android:id="@+id/des_src"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="原文" />

    <EditText
        android:id="@+id/des_dst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="密文" />

    <Button
        android:id="@+id/btn_encode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加密" />

    <Button
        android:id="@+id/btn_decode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="解密" />

</LinearLayout></span>


技术分享



版权声明:本文为博主原创文章,未经博主允许不得转载。

[android]DES/3DES/AES加密方式

标签:

原文地址:http://blog.csdn.net/bless2015/article/details/46710413

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!