package com.*****.util; import android.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.security.Key; /** * Created by 88 on 15-10-16. */ public class DESUtil { private final static String KEY_STRING = "lijoud"; public static String encryptToken(String uid) { String encryUid = ""; try { Cipher eCipher = Cipher.getInstance("DES"); eCipher.init(Cipher.ENCRYPT_MODE, getKey()); encryUid = new String( Base64.encode( eCipher.doFinal(uid.getBytes()), Base64.DEFAULT)); } catch (Exception ex) { ex.printStackTrace(); } finally { return encryUid; } } public static String decryptToken(String encryUid) { String uid = ""; try { Cipher dCipher = Cipher.getInstance("DES"); dCipher.init(Cipher.DECRYPT_MODE, getKey()); uid = new String(dCipher.doFinal(Base64.decode(encryUid.getBytes(), Base64.DEFAULT))); } catch (Exception ex) { ex.printStackTrace(); } finally { return uid; } } private static Key getKey() { byte[] mBytes = KEY_STRING.getBytes(); byte[] bytes = new byte[8]; for (int i = 0; i < bytes.length; i++) bytes[i] = mBytes[i]; return new SecretKeySpec(bytes, "DES"); } }
上面的是由DES算法的加密揭秘,我要说的第一, key要是8个字节64位 第二。就是要封装一个
Base64。
过程,加密DES,加密64 ----->揭秘64,揭秘DES
1169

被折叠的 条评论
为什么被折叠?



