Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

public class Main {
    private static Key key = null;

    private static byte[] getEncCode(Context mContext, byte[] byteS) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, getKey(mContext));
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    private static Key getKey(Context mContext) {
        if (key == null) {
            try {
                KeyGenerator _generator = KeyGenerator.getInstance("DES");
                _generator.init(new SecureRandom(mContext.getPackageName().getBytes()));
                key = _generator.generateKey();
                _generator = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return key;
    }
}