Android DES encrypt encrypt(String property)

Here you can find the source of encrypt(String property)

Description

encrypt

Declaration

static String encrypt(String property) 

Method Source Code

//package com.java2s;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import android.util.Base64;

public class Main {
    private static final char[] PASSWORD = "enfldsgbnlsngdlksdsgm"
            .toCharArray();/*from w  w  w.  ja v  a 2 s . c om*/
    private static final byte[] SALT = { (byte) 0xde, (byte) 0x33,
            (byte) 0x10, (byte) 0x12, (byte) 0xde, (byte) 0x33,
            (byte) 0x10, (byte) 0x12, };

    static String encrypt(String property) {
        try {
            SecretKeyFactory keyFactory = SecretKeyFactory
                    .getInstance("PBEWithMD5AndDES");
            SecretKey key = keyFactory.generateSecret(new PBEKeySpec(
                    PASSWORD));
            Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
            pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(
                    SALT, 20));
            return base64Encode(pbeCipher.doFinal(property
                    .getBytes("UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private static String base64Encode(byte[] bytes) {
        // NB: This class is internal, and you probably should use another impl
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }
}

Related

  1. encode(String key, String data)
  2. desEncodeCBC(byte[] key, byte[] keyiv, byte[] data)
  3. desEncodeECB(byte[] key, byte[] data)
  4. decrypt(String property)
  5. encryptDES(String encryptString, String encryptKey)
  6. encryptDESFile(byte[] encryptdata, String encryptKey)
  7. encryptEDE(byte[] key, byte[] src)
  8. encrypt(byte[] key, byte[] src)