Android examples for java.security:DES
encrypt Des String
//package com.java2s; import java.security.Key; import java.security.SecureRandom; import java.util.Locale; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; public class Main { public static String encryptDesString(String key, String str) { return byte2hex(getEncCode(key, str.getBytes())); }//from ww w .j a va 2 s . c o m private static String byte2hex(byte[] b) { String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length() == 1) { hs = hs + "0" + stmp; } else { hs = hs + stmp; } } return hs.toUpperCase(Locale.getDefault()); } private static byte[] getEncCode(String key, byte[] byteS) { byte[] byteFina = null; Cipher cipher; try { cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, newDesInstance(key)); byteFina = cipher.doFinal(byteS); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } private static Key newDesInstance(String strKey) { Key key = null; try { KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); keyGenerator.init(new SecureRandom(strKey.getBytes())); key = keyGenerator.generateKey(); keyGenerator = null; } catch (Exception e) { e.printStackTrace(); } return key; } }