MD5, Base64, Des/3des/ades plus decryption in Android

Source: Internet
Author: User

MD5 Digest algorithm:

<span style= "FONT-SIZE:18PX;" > main code: String s = Edit.gettext (). toString ();    if (Textutils.isempty (s)) {        Toast.maketext (getactivity (), "Digest data is not nullable", Toast.length_short). Show ();    } else {        try {            //Get a digest algorithm            messagedigest instance = messagedigest.getinstance ("MD5");            Byte[] Digest = instance.digest (S.getbytes ("UTF-8"));            StringBuilder builder = new StringBuilder ();            for (byte b:digest) {                builder.append (String.Format ("%2x", B & 0xFF));            }            String replace = builder.tostring (). Replace (', ' 0 '). toUpperCase ();            Text.settext (replace);        } catch (NoSuchAlgorithmException e) {            e.printstacktrace ();        } catch (Unsupportedencodingexception e) {            E.printstacktrace ();}}}    </span>




Base64 Plus decryption:

<span style= "FONT-SIZE:18PX;" > main code: public void OnClick (View v) {switch (V.getid ()) {case R.id.base64_encode:string src_s = sr            C.gettext (). toString ();            if (Textutils.isempty (src_s)) {Toast.maketext (getactivity (), "Original code cannot be empty", Toast.length_short). Show (); } else {try {String s = base64.encodetostring (Src_s.getbytes ("UTF-8"), Base64.defa                    ULT);                Rlt.settext (s);                } catch (Unsupportedencodingexception e) {e.printstacktrace ();        }} break;            Case R.id.base64_decode:string rlt_s = Rlt.gettext (). toString ();            if (Textutils.isempty (rlt_s)) {Toast.maketext (getactivity (), "Encoding is not nullable", Toast.length_short). Show ();                } else {byte[] decode = Base64.decode (rlt_s, Base64.default);  try {src.settext (new String (decode, "UTF-8"));              } catch (Unsupportedencodingexception e) {e.printstacktrace ();    }} break; }}</span>



Des plus decryption:


<span style= "FONT-SIZE:18PX;"    > main code: public void OnClick (view view) {String key_str = Key.gettext (). toString ();    LOG.I ("key_str-----", key_str+ "----");    if (key_str==null) {Toast.maketext (getactivity (), "Key not Nullable", Toast.length_short). Show ();            } else {try {byte[] bytes = key_str.getbytes ("UTF-8");            Byte[] keys = new BYTE[32];            System.arraycopy (bytes, 0, keys, 0, Math.min (bytes.length, keys.length)); In addition to decryption, the key object used, the length of the first parameter byte array must conform to the requirements of the second parameter algorithm.

Secretkey Secretkey = new Secretkeyspec (keys, "Aes"); Add decryption, use the tool object, tool algorithm and key algorithm must be the same Cipher Cipher = Cipher.getinstance ("Aes"); Switch (View.getid ()) {case R.id.des_encode:string src_str = Src.gettext (). toString (); if (src_str==null) {Toast.maketext (getactivity (), "The original text is not NULL when encrypting", Toast.length_sho RT). Show (); } else {//Initialize to Cryptographic mode Cipher.init (Cipher.encrypt_mode, Secretkey); byte[] afinal = cipher.dofinal (Src_str.getbytes ("UTF-8")); Rlt.settext (Base64.encodetostring (afinal, Base64.default)); } break; Case R.id.des_decode:string rlt_str = Rlt.gettext (). toString (); if (rlt_str==null) {Toast.maketext (getactivity (), "decrypted. Ciphertext cannot be empty ", toast.length_short). ShoW (); } else {cipher.init (Cipher.decrypt_mode, Secretkey); byte[] afinal = cipher.dofinal (Base64.decode (Rlt_str, Base64.default)); Src.settext (New String (afinal, "UTF-8")); } break; }} catch (Unsupportedencodingexception e) {e.printstacktrace (); } catch (NoSuchAlgorithmException e) {e.printstacktrace (); } catch (Nosuchpaddingexception e) {e.printstacktrace (); } catch (InvalidKeyException e) {e.printstacktrace (); } catch (Badpaddingexception e) {e.printstacktrace (); } catch (Illegalblocksizeexception e) {e.printstacktrace (); }}}</span>



Source code Address:

http://download.csdn.net/detail/lzq520210/9419823

MD5, Base64, Des/3des/ades plus decryption in Android

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.