DES加密工具

本工具采用的是DES SHA1PRNG 加密,如有兴趣,读者可由此自定义其他加密工具。具体可看代码注释

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * DESUtils 先采用BASE64Encoder对字符串进行处理,避免字符问题。
 * 再采用des 标准的SHA1PRNG加密方法进行加密
 */
public class DESUtils {
    private static Key key;
    private static String KEY_STR = "webapp";//加密秘钥,请自定义值
    private static String  charset = "UTF-8";
    private static String type = "DES";  //加密标准
    private static String  method="SHA1PRNG"; //加密方法
    private static Cipher cipher; //用于加解密操作的对象

    //初始化key  cipher
    static {
        try {
            KeyGenerator generator = KeyGenerator.getInstance(type); //获取秘钥对象
            SecureRandom secureRandom = SecureRandom.getInstance(method);//获取加密算法的强随机数对象
            secureRandom.setSeed(KEY_STR.getBytes());//根据key_str生成强随机数
            generator.init(secureRandom);//初始化秘钥对象
            key = generator.generateKey();//生成key
            generator = null;
            cipher= Cipher.getInstance(type);//获取cipher
            cipher.init(Cipher.ENCRYPT_MODE, key);//根据key初始化cipher
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
    }

    //获取加密后的字符串
    public static String getEncryptString(String str) {
        BASE64Encoder base64encoder = new BASE64Encoder();
        byte[] bytes = new byte[0];
        try {
            bytes = str.getBytes(charset);
            byte[] doFinal = new byte[0];
            doFinal = cipher.doFinal(bytes);
            return base64encoder.encode(doFinal);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    //获取加密后的字符串 将字符串解密
    public static String getDecryptString(String str) {
        BASE64Decoder base64decoder = new BASE64Decoder();
        try {
            byte[] bytes = base64decoder.decodeBuffer(str);
            byte[] doFinal = cipher.doFinal(bytes);
            return new String(doFinal, charset);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

<center>Coding Blog     Github Blog </center>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,002评论 25 709
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,026评论 3 119
  • 其实,我们的口号是:浪里个浪! 有空多出去走走呵,除了生存,我们还有生活。在路上,有不一样的风景,有不一样的人事,...
    wit小学生阅读 261评论 0 0
  • 快过年啦,2017年鸡年,我要卖一年的鸡,单身的我,跟鸡处上了! 祝福每位在我微信上的朋友鸡年能大鸡大利,事业搞...
    王冠淋阅读 294评论 0 0
  • 有些人一旦分开就是永不相见
    早已气定神闲阅读 198评论 0 0