generate DES Key - Android java.security

Android examples for java.security:DES

Description

generate DES Key

Demo Code


//package com.java2s;
import android.util.Log;

import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;

public class Main {
    private static String TAG = "CryptoHelper";
    protected static String desAlgorithm = "DES";

    public static String generateDESKey() {
        try {//from  ww w  . j  a  v a2 s.co  m
            String str = toHexString(KeyGenerator.getInstance(desAlgorithm)
                    .generateKey().getEncoded());
            return str;
        } catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
            Log.e(TAG,
                    "generateDESKey(): "
                            + localNoSuchAlgorithmException.toString());
        }
        return null;
    }

    public static String toHexString(byte[] paramArrayOfByte) {
        StringBuffer localStringBuffer = new StringBuffer();
        for (int i = 0;; i++) {
            if (i >= paramArrayOfByte.length) {
                return localStringBuffer.toString();
            }
            localStringBuffer.append(Integer.toHexString(
                    256 + (0xFF & paramArrayOfByte[i])).substring(1));
        }
    }
}

Related Tutorials