zoukankan      html  css  js  c++  java
  • Java DES 加解密("DES/EBC/NoPadding")

    private static final Cipher DES_CIPHER;
    
    static {
        try {
            DES_CIPHER = Cipher.getInstance("DES/ECB/NoPadding");
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            throw Throwables.propagate(e);
        }
    }
    
    public static String encryptDES(String encryptString, String encryptKey) {
        try {
            DES_CIPHER.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(IcbcConstant.ENCODING_GBK), "DES"));
            byte[] encryptedData = DES_CIPHER.doFinal(encryptString.getBytes(IcbcConstant.ENCODING_GBK));
            return new String(encryptedData, IcbcConstant.ENCODING_GBK);
        } catch (Throwable e) {
            throw Throwables.propagate(e);
        }
    }
    
    public static String decryptDES(String decryptString, String decryptKey) {
        try {
            DES_CIPHER.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(IcbcConstant.ENCODING_GBK), "DES"));
            byte decryptedData[] = DES_CIPHER.doFinal(decryptString.getBytes(IcbcConstant.ENCODING_GBK));
            return new String(decryptedData, IcbcConstant.ENCODING_GBK);
        } catch (Throwable e) {
            throw Throwables.propagate(e);
        }
    }

    nopadding模式下的补位,此例为补空格。

     private static byte[] formatData(String str) {
            int yu = str.length() % 8;
            if (yu == 0) {
                return str.getBytes(Charset.forName(Constants.Charset));
            }
            int size = 8 - yu;
            byte[] arr = new byte[str.length() + size];
            byte[] data = str.getBytes(Charset.forName(Constants.Charset));
            int i = 0;
            for (; i < data.length; i++) {
                arr[i] = data[i];
            }
            for (int j = 0; j < size; j++, i++) {
                arr[i] = ' ';
            }
            return arr;
        }
    
        private static byte[] formatByte(byte[] arr) {
            int i = 0;
            for (; i < arr.length; i++) {
                if ((char) arr[i] == ' ') {
                    break;
                }
            }
            byte[] result = new byte[i];
            System.arraycopy(arr, 0, result, 0, i);
            return result;
        }
  • 相关阅读:
    过滤器--起步阶段
    常用指令-起步阶段
    模型和控制器-起步阶段
    指令介绍-起步阶段
    AngularJS简介-起步阶段
    c++之helloworld与命名空间
    c++ 之重要性
    一个没曾摸透的程序
    linux中什么是文件结构体?
    很全的linux网络编程技巧
  • 原文地址:https://www.cnblogs.com/frankyou/p/8351818.html
Copyright © 2011-2022 走看看