字符串的加密与解密

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值