天天看點

DES、3DES、AES加密方式

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);  

    }  

    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);  

                        byte[] bytes = cipher.dofinal(base64.decode(dst_str,  

                        des_src.settext(new string(bytes, "utf-8"));  

                }  

            } 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="秘鑰" />  

        android:id="@+id/des_src"  

        android:hint="原文" />  

        android:id="@+id/des_dst"  

        android:hint="密文" />  

    <button  

        android:id="@+id/btn_encode"  

        android:text="加密" />  

        android:id="@+id/btn_decode"  

        android:text="解密" />  

</linearlayout></span>  

DES、3DES、AES加密方式

特别說明:尊重作者的勞動成果,轉載請注明出處哦~~~http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt165

繼續閱讀