[Android] Des/3des/aes Encryption method

Source: Internet
Author: User

DES supports 8-bit encryption decryption, 3Des supports 24-bit, and AES supports 32-bit. 3Des is the DES algorithm to do three times. The number of bits is in bytes byte, not bits.

3Des is divided 24 bits into 3 groups, the first set of eight bits for encryption, the second set of 8 bits for decryption, and the third set of 8 bits for encryption, so if the secret key is 123456781234567812345678 (3 group 1-8), it is equivalent to a 12345678 des encryption. For example: The first time with 12345678 key to 123 encryption to get "ldifudf0iew=", and then use the second set of 12345678 to decrypt it (reverse encryption process), get 123, the third time encryption to get "ldifudf0iew=".


There are three different ways of encrypting code:

byte temp[] = new byte[number of digits];

Secretkey Des_key = new Secretkeyspec (temp, "cryptographic algorithm");

The encryption algorithm name corresponds to: Aes Des Desede

You can implement different encryption methods by changing these two places. Encryption method is not the same as the bottom, DES is the original programming 2 into a string of 01, and then the cipher to do the operation, Exchange what position.

<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); } @Overridepublic void OnClick (View v) {String key_str = Des_key.gettext (). toString (); Textutils.isempty (KEY_STR)) {try {//des no matter how short it is, it becomes eight bits 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 only supports eightBit 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);//Set cipher for encryption byte[] bytes = cipher.dofinal ( Src_str.getbytes ("UTF-8"));d es_dst.settext (base64.encodetostring (bytes, base64.default)); </span><pre Name= "Code" class= "java" ><span style= "font-size:18px;" >//is a binary byte array encoded with BASE64 </span>
}break;case r.id.btn_decode:string dst_str = Des_dst.gettext (). toString (); Textutils.isempty (DST_STR)) {Cipher.init (Cipher.decrypt_mode, Des_key);//Set cipher to decrypt byte[] bytes = cipher.dofinal ( Base64.decode (Dst_str, Base64.default));//is a binary byte array des_src.settext encoded with Base64 (new String (bytes, "UTF-8"));} Break;}} catch (Exception e) {e.printstacktrace ();}}}}


layout:

<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:orien        tation= "vertical" > <edittext android:id= "@+id/des_key" android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content" android:hint= "secret key"/> <edittext android:id= "@+id/des_src" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:hint= "original"/> <Ed Ittext android:id= "@+id/des_dst" android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "android:hint=" ciphertext "/> <button android:id=" @+id/btn_encode "android:layout_width=" Wrap_co Ntent "android:layout_height=" wrap_content "android:text=" Encrypt "/> <button android:id=" @+id/bt  N_decode "      Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "decryption"/></L Inearlayout></span>





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Android] Des/3des/aes Encryption method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.