Encryption des encryption _android of Android data

Source: Internet
Author: User
Tags base64 decrypt md5 encryption random seed stringbuffer

Preface:

A colleague consulted me about the implementation of Android DES Encryption, a simple implementation, today to sum up.

Several other encryption methods:
RSA encryption of Android data encryption
AES encryption of Android data encryption
Des encryption of Android data encryption
MD5 encryption of Android data encryption
BASE64 coding algorithm of Android data encryption
The safe hashing algorithm of SHA for android Data encryption

DES encryption Introduction:
Des is a symmetric encryption algorithm, the so-called symmetric encryption algorithm is: Encryption and decryption using the same key algorithm. Des encryption algorithms are derived from IBM's research,
It was later formally adopted by the United States Government, and began to circulate widely, but in recent years it has been used less and more, because Des uses a 56-bit key to modern computing power,
Can be cracked within 24 hours.

DES encryption Usage:

1.) Desutil Constant Class Introduction

  Private final static String HEX = "0123456789ABCDEF";
  Private final static String transformation = "des/cbc/pkcs5padding";//des is encrypted. CBC is working mode pkcs5padding is fill mode
  private Final static String Ivparameterspec = "01020304";////initialization vector parameter, AES to 16bytes. DES is 8bytes.
  Private final static string algorithm = "DES";//des is the encryption method
  private static final String sha1prng = "sha1prng";////sha1pr NG strong random seed algorithm, to distinguish more than 4.2 version of the call method

1.) Dynamic generation of secret key

Length cannot be less than 8 bytes because des has a fixed format of 128bits, or 8bytes.

  
 * * Generate random number, can be used as dynamic key encryption and decryption key must be consistent, otherwise it will not be
 decrypted
  /public static String GenerateKey () {
    try {
      SecureRandom localsecurerandom = securerandom.getinstance (sha1prng);
      byte[] Bytes_key = new BYTE[20];
      Localsecurerandom.nextbytes (Bytes_key);
      String Str_key = Tohex (Bytes_key);
      return str_key;
    } catch (Exception e) {
      e.printstacktrace ();
    }
    return null;
  }

  Binary turn character public
  static String Tohex (byte[] buf) {
    if (buf = = null) return
      "";
    StringBuffer result = new StringBuffer (2 * buf.length);
    for (int i = 0; i < buf.length i++) {
      Appendhex (result, buf[i));
    return result.tostring ();
  }

  private static void Appendhex (StringBuffer sb, byte b) {
    sb.append (Hex.charat ((b >> 4) & 0x0f)). Append (HEX . CharAt (b & 0x0f));
  

3.) Two ways of handling key keys

First type:

  Processing of keys
  private static key Getrawkey (String key) throws Exception {
    Keygenerator KGen = Keygenerator.getinstan CE (algorithm);
    For Android
    securerandom sr = null;
    In more than 4.2 versions, the SecureRandom acquisition has changed
    if (Android.os.Build.VERSION.SDK_INT >=) {
      sr = Securerandom.getinstance (sha1prng, "Crypto");
    } else {
      sr = securerandom.getinstance (SHA1PRNG);
    }
    For Java
    //SecureRandom = Securerandom.getinstance (sha1prng);
    Sr.setseed (Key.getbytes ());
    Kgen.init (, SR); Des fixed format is 64bits, that is, 8bytes.
    Secretkey skey = Kgen.generatekey ();
    Byte[] raw = skey.getencoded ();
    return new Secretkeyspec (raw, algorithm);
  

The second type:

  Processing of keys
  private static key Getrawkey (String key) throws Exception {
    Deskeyspec dks = new Deskeyspec (key.getbyte s ());
    Secretkeyfactory keyfactory = secretkeyfactory.getinstance (algorithm);
    Return Keyfactory.generatesecret (DKS);
  }

4.) Encryption Implementation

 /**
   * des algorithm, encryption
   *
   @param data to be encrypted string
   * @param key encryption private key, length can not be less than 8 bits
   * @return encrypted byte array, generally combined with BASE64 encoding
   */Public
  static string encode (string key, String data) {return
    encode (key, data.getbytes ());
  }


  /**
   * des algorithm, encryption
   *
   @param data to be encrypted string
   * @param key encryption private key, length can not be less than 8 bits
   * @return encrypted byte array, General associative BASE64 encoding using
  /public static string encode (string key, byte[] data) {
    try {
      Cipher Cipher = Cipher . getinstance (transformation);
      Ivparameterspec IV = new Ivparameterspec (Ivparameterspec.getbytes ());
      Cipher.init (Cipher.encrypt_mode, Getrawkey (key), iv);
      byte[] bytes = cipher.dofinal (data);
      Return base64.encodetostring (bytes, base64.default);
    } catch (Exception e) {return
      null;
    }
  }

5.) Decryption Implementation

  /**
   * Gets the encoded value
   * *
   @param key
   * @param data
   * @return/Public
  static string decode (string Key, String data) {return
    decode (key, Base64.decode (data, Base64.default));
  }

  /**
   * des algorithm, decryption
   *
   @param data to decrypt the string
   * @param key to decrypt the private key, length can not be less than 8 bits
   * @return decrypted byte array
   */< C17/>public static string decode (string key, byte[] Data {
    try {
      Cipher Cipher = cipher.getinstance (Transforma tion);
      Ivparameterspec IV = new Ivparameterspec (Ivparameterspec.getbytes ());
      Cipher.init (Cipher.decrypt_mode, Getrawkey (key), iv);
      byte[] Original = cipher.dofinal (data);
      String originalstring = new String (original);
      return originalstring;
    } catch (Exception e) {return
      null;
    }
  }

Des knowledge Extension: 3DES

3DES is a pattern of DES encryption algorithms that uses 3 64-bit keys to encrypt data three times. Data Encryption Standard (DES) is a long-standing encryption standard in the United States, which uses symmetric key cryptography. 3DES (i.e. Triple DES) is the DES encryption algorithm for the transition to AES (in 1999, NIST designated 3-des as the Transitional Encryption Standard), a more secure variant of DES. It takes des as the basic module, and designs the packet encryption algorithm by the combination grouping method.

Des compared with AES:

Was asked to use DES Encryption in the depths of my heart I was refused. Only from the name of the AES (Advanced encryption Standard) Advanced encryption standards, security is higher than DES, in fact, the appearance of AES itself is to replace DES, AES has better security, efficiency, flexibility than DES, So symmetric encryption is preferred by AES.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.