Android platform des IV encryption and decryption essays

Source: Internet
Author: User
Tags asymmetric encryption

Android platform des IV encryption and decryption essays

A good memory is better than a bad pen, so I started blogging. On the one hand to deepen their understanding, two to the back of beginners less detours, no matter how difficult, some things may not understand the depth, welcome you master guidance and spit Groove!

Des encryption has been in contact many times, but always easy to forget, and server interaction, the addition of inconsistencies can be decrypted after the success but the head is garbled led to a small pit for a while, recorded here ~

According to Baidu Encyclopedia and its own understanding, DES is a symmetric encryption algorithm based on 56-bit key, that is, both sides of the key needs to be consistent, this does not consider why the use of higher security AES or asymmetric encryption method, such as RSA, etc. about the key space is small, You can use the derived algorithm 3DES of des to encrypt. Des algorithm is the 64-bit plaintext input block into 64-bit cipher output block, so here need BASE64 codec tool class, encryption requires 3 parameters (Key, Data, mode) mode is encryption or decryption, the others do not explain, the comments are written more clearly.

Here is the encryption and decryption method:

<span style= "FONT-SIZE:18PX;" >public class Encryptutils {<span style= "white-space:pre" ></span>public static String Encryptdes ( String encryptstring, String encryptkey) <span style= "White-space:pre" ></span>throws Exception {<span Style= "White-space:pre" ></span>//returns the Cipher object that implements the specified transformation <span style= "White-space:pre" ></span> "algorithm /mode/fill "<span style=" White-space:pre "></span>cipher Cipher = cipher.getinstance (" des/cbc/pkcs5padding ") ; <span style= "White-space:pre" ></span>//creates a Deskeyspec object that uses 8-byte keys as the key content of the DES key. <span style= "White-space:pre" ></span>deskeyspec deskeyspec = new Deskeyspec (encryptKey.getBytes ("UTF-8" ); <span style= "White-space:pre" ></span>//returns the Secretkeyfactory object that transforms the secret key of the specified algorithm.  <span style= "White-space:pre" ></span>secretkeyfactory keyfactory = SecretKeyFactory.getInstance ( "DES"); <span style= "White-space:pre" ></span>//generates Secretkey objects based on the provided key. <span style= "White-space:pre "></span>secretkey Secretkey = Keyfactory.generatesecret (desKeySpec); <span style=" White-space:pre "></span>//constructs a Ivparameterspec object using the bytes in IV as an IV. Copy the contents of the buffer to prevent subsequent modifications. <span style= "White-space:pre" ></span>ivparameterspec IV = new Ivparameterspec (EncryptKey.getBytes ()); <span style= "White-space:pre" ></span>//initializes this Cipher with a key and a set of algorithm parameters; Cipher: Encryption, decryption, key wrapper, or key unpacking, depending on the value of Opmode. <span style= "White-space:pre" ></span>cipher.init (Cipher.encrypt_mode, SecretKey, iv); <span style= " White-space:pre "></span>//encryption simultaneously decodes to a string return <span style=" White-space:pre "></span>return new string (Base64.encode (cipher.dofinal (Encryptstring<span style= "White-space:pre" ></span>.getbytes ("UTF-8")) ); <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>public static string Decryptdes (String decodestring, String decodekey) throws Exception {<span style= "White-space:pre" >& Lt;/span>//is constructed using the specified key Iv<span style= "White-space:pre" ></span>ivparameterspec IV = new IVPARAMETERSPEC ( Decodekey.getbytes ()); <span style= "White-space:pre" ></span>//constructs a key based on the given byte array and the specified algorithm.  <span style= "White-space:pre" ></span>secretkeyspec skeyspec = new Secretkeyspec ( Decodekey.getbytes (), "DES"), <span style= "White-space:pre" ></span>//returns the Cipher object that implements the specified transformation <span style= " White-space:pre "></span>cipher Cipher = cipher.getinstance (" des/cbc/pkcs5padding "); <span style=" White-space:pre "></span>//decryption initialization <span style=" White-space:pre "></span>cipher.init ( Cipher.decrypt_mode, Skeyspec, iv) <span style= "White-space:pre" ></span>//decode return <span style= " White-space:pre "></span>byte[] Bytemi = Base64.decode (Decodestring.tochararray ()); <span style=" White-space:pre "></span>byte decrypteddata[] = cipher.dofinal (Bytemi); <span style=" White-space:pre " ></span>return new String (decrypteddata);<Span style= "White-space:pre" ></span>}}</span> 

Several errors need to be explained below:

Java.security.InvalidAlgorithmParameterException:IV must be 8 bytes long.
Java.security.InvalidKeyException:key Too Short

Both of these errors are caused by the length of the key, but the official says the key is a 56-bit length, which is a bit less clear, but you just have to remember that key must be 8 bits long! This place also please master enlighten!

IV vector: The main role is to prevent tampering, this place if inconsistent will cause the data head solution is garbled, and back normal.


BASE64 codec Tool class

<span style= "FONT-SIZE:18PX;" >public class BASE64 {static public char[] encode (byte[] data) {char[] out = new char[((Data.length + 2)/3) * 4];for (int i = 0, index = 0; i < data.length; i + = 3, index + = 4) {Boolean quad = False;boolean Trip = False;int val = (0xFF & (int) data[i]); Val <<= 8;if ((i + 1) < Data.leng Th) {val |= (0xFF & (int) Data[i + 1]); trip = true;} Val <<= 8;if ((i + 2) < Data.length) {val |= (0xFF & (int) Data[i + 2]); quad = true;} Out[index + 3] = alphabet[(quad? (Val & 0x3F): (+)];val >>= 6;out[index + 2] = alphabet[(trip? (Val & 0x3F): (+)];val >>= 6;out[index + 1] = alphabet[val & 0x3f];val >>= 6;out[index + 0] = Alphabe T[val & 0x3F];} return out;} Static public byte[] Decode (char[] data) {int len = ((data.length + 3)/4) * 3;IF (data.length > 0 && data[dat A.length-1] = = ')--len;if (data.length > 1 && data[data.length-2] = = ')--len;byte[] out = new Byte[len] ; INT shift = 0;int Accum = 0;int index = 0;for (int ix = 0; ix < data.length; ix++) {int value = Codes[data[ix] & 0xF F];if (value >= 0) {accum <<= 6;shift + = 6;accum |= value;if (Shift >= 8) {shift-= 8;out[index++] = (byte) (( Accum >> Shift) & 0xFF);}}} if (Index! = out.length) throw new Error ("miscalculated data length!"); return out;} Static private char[] Alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=". ToCharArray (); Static private byte[] Codes = new Byte[256];static {for (int i = 0; i < i++) Codes[i] = -1;for (int i = ' A '; i < = ' Z '; i++) Codes[i] = (byte) (i-' a '), for (int i = ' a '; I <= ' z '; i++) codes[i] = (byte) (+ I-' a '), for (int i = ' 0 '; I &l t;= ' 9 '; i++) Codes[i] = (byte) (0 '); codes[' + '] = 62;codes['/'] = 63;}} </span>

Encrypted decryption using





Android platform des IV encryption and decryption essays

Related Article

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.