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

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

时间:2017-06-02 11:41:16      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:save   字节   sch   style   ide   三次   protect   数组   iss   

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。然后和密文做运算,交换什么什么位置。

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无论长了短了。都变成八位
				// AES 长度变为128 new SecretKeySpec(temp, "Aes");
				// 3Des 长度变为24 new SecretKeySpec(temp, "Desced");
				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);
						byte[] bytes = cipher
								.doFinal(src_str.getBytes("UTF-8"));
						// 是用Base64编码的二进制字节数组
						des_dst.setText(Base64.encodeToString(bytes,
								Base64.DEFAULT));
					}
					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);
						// 是用Base64编码的二进制字节数组
						byte[] bytes = cipher.doFinal(Base64.decode(dst_str,
								Base64.DEFAULT));
						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加密方式

标签:save   字节   sch   style   ide   三次   protect   数组   iss   

原文地址:http://www.cnblogs.com/gavanwanggw/p/6931865.html

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