Android DES 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 arithmetic originated from IBM research and was formally adopted by the U.S. government. It has been widely used since then. But in recent years, it has been used less and less, because DES uses 56-bit key, with modern computing power, it can be cracked within 24 hours.

DES encryption mode of use:

1. Introduction of DesUtil Constant Class

   private final static String HEX = "0123456789ABCDEF";
   private final static String TRANSFORMATION = "DES/CBC/PKCS5Padding";//DES is encryption mode CBC is working mode PKCS5 Padding is filling mode
   private final static String IVPARAMETERSPEC = "01020304";//// Initialize vector parameters, AES is 16 bytes. DES is 8 bytes.
   private final static String ALGORITHM = "DES";//DES is an encryption method
   private static final String SHA1PRNG = "SHA1PRNG";//// SHA1PRNG Strong Random Seed Algorithms, to distinguish between 4.2 versions of the call method

1. Dynamic generation of secret keys

The length cannot be less than 8 bytes because DES has a fixed format of 128 bits, or 8 bytes.

    /*
 * Generating random numbers can be regarded as dynamic key encryption and decryption keys 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 character conversion
    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 secret Key key

The first is:

    // Processing keys
    private static Key getRawKey(String key) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
        //for android
        SecureRandom sr = null;
        // In versions 4.2 or more, the way SecureRandom is retrieved has changed
        if (android.os.Build.VERSION.SDK_INT >= 17) {
            sr = SecureRandom.getInstance(SHA1PRNG, "Crypto");
        } else {
            sr = SecureRandom.getInstance(SHA1PRNG);
        }
        // for Java
        // secureRandom = SecureRandom.getInstance(SHA1PRNG);
        sr.setSeed(key.getBytes());
        kgen.init(64, sr); //DES has a fixed format of 64 bits, or 8 bytes.
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return new SecretKeySpec(raw, ALGORITHM);
    }

The second is:

    // Processing keys
    private static Key getRawKey(String key) throws Exception {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return keyFactory.generateSecret(dks);
    }

4. Encryption Implementation

 /**
     * DES Algorithms, Encryption
     *
     * @param data String to be encrypted
     * @param key  Encrypted private key, length not less than 8 bits
     * @return Encrypted byte arrays are generally used in conjunction with Base64 encoding
     */
    public static String encode(String key, String data) {
        return encode(key, data.getBytes());
    }


    /**
     * DES Algorithms, Encryption
     *
     * @param data String to be encrypted
     * @param key  Encrypted private key, length not less than 8 bits
     * @return Encrypted byte arrays are generally used in conjunction with Base64 encoding
     */
    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. Implementation of decryption

    /**
     * Get the coded value
     *
     * @param key
     * @param data
     * @return
     */
    public static String decode(String key, String data) {
        return decode(key, Base64.decode(data, Base64.DEFAULT));
    }

    /**
     * DES Algorithms, decryption
     *
     * @param data Undecrypted string
     * @param key  Decrypt the private key, not less than 8 bits in length
     * @return Decrypted byte array
     */
    public static String decode(String key, byte[] data) {
        try {
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            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 mode of DES encryption algorithm, which uses three 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 encryption. 3DES (Triple DES) is a transition encryption algorithm from DES to AES (in 1999, NIST designated 3-DES as the transition encryption standard), which is a safer variant of DES. It takes DES as the basic module, and designs the block encryption algorithm by combining group method.

DES and AES:

  At that time, I was asked to use DES encryption deep inside I refused. From the name alone, AES(Advanced Encryption Standard) advanced encryption standard has higher security than DES. In fact, AES itself is to replace DES. AES has better security, efficiency and flexibility than DES, so symmetric encryption is preferred to AES.

Keywords: less Android encoding Java

Added by pranesh on Thu, 11 Jul 2019 00:26:55 +0300