欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java最简单的DES加密算法实现案例

程序员文章站 2023-12-20 10:16:58
base64.java package com.mstf.des; import java.io.unsupportedencodingexception...

base64.java

package com.mstf.des;
 
import java.io.unsupportedencodingexception;
 
/**
 * base64编码/解码
 * @author ceet
 *
 */
public class base64 {
 
  public static string encode(string data) {
    return new string(encode(data.getbytes()));
  }
 
  public static string decode(string data) {
    try {
      return new string(decode(data.tochararray()),"utf-8");
    } catch (unsupportedencodingexception e) {
      e.printstacktrace();
      return null;
    } 
  }
 
  private static char[] alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="
      .tochararray();
 
  private static byte[] codes = new byte[256];
 
  static {
    for (int i = 0; i < 256; i++) {
      codes[i] = -1;
    }
    for (int i = 'a'; i <= 'z'; i++) {
      codes[i] = (byte) (i - 'a');
    }
 
    for (int i = 'a'; i <= 'z'; i++) {
      codes[i] = (byte) (26 + i - 'a');
    }
    for (int i = '0'; i <= '9'; i++) {
      codes[i] = (byte) (52 + i - '0');
    }
    codes['+'] = 62;
    codes['/'] = 63;
  }
 
  public static char[] encode(byte[] data) {
    char[] out = new char[((data.length + 2) / 3) * 4];
    for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
      boolean quad = false;
      boolean trip = false;
 
      int val = (0xff & (int) data[i]);
      val <<= 8;
      if ((i + 1) < data.length) {
        val |= (0xff & (int) data[i + 1]);
        trip = true;
      }
      val <<= 8;
      if ((i + 2) < data.length) {
        val |= (0xff & (int) data[i + 2]);
        quad = true;
      }
      out[index + 3] = alphabet[(quad ? (val & 0x3f) : 64)];
      val >>= 6;
      out[index + 2] = alphabet[(trip ? (val & 0x3f) : 64)];
      val >>= 6;
      out[index + 1] = alphabet[val & 0x3f];
      val >>= 6;
      out[index + 0] = alphabet[val & 0x3f];
    }
    return out;
  }
 
  public static byte[] decode(char[] data) {
    int templen = data.length;
    for (int ix = 0; ix < data.length; ix++) {
      if ((data[ix] > 255) || codes[data[ix]] < 0) {
        --templen;
      }
    }
    int len = (templen / 4) * 3;
    if ((templen % 4) == 3) {
      len += 2;
    }
    if ((templen % 4) == 2) {
      len += 1;
 
    }
    byte[] out = new byte[len];
 
    int shift = 0;
    int accum = 0;
    int index = 0;
 
    for (int ix = 0; ix < data.length; ix++) {
      int value = (data[ix] > 255) ? -1 : codes[data[ix]];
 
      if (value >= 0) {
        accum <<= 6;
        shift += 6;
        accum |= value;
        if (shift >= 8) {
          shift -= 8;
          out[index++] = (byte) ((accum >> shift) & 0xff);
        }
      }
    }
 
    if (index != out.length) {
      throw new error("miscalculated data length (wrote " + index
          + " instead of " + out.length + ")");
    }
 
    return out;
  }
}

desutil.java

package com.mstf.des;
 
import java.security.key;
import java.security.securerandom;
 
import javax.crypto.cipher;
import javax.crypto.keygenerator;
 
/**
 * des对称算法(加密/解密)
 *
 * @author ceet
 *
 */
public class desutil {
 
  private key key;
 
  public desutil(string strkey) {
    setkey(strkey);
  }
 
  public void setkey(string strkey) {
    try {
      keygenerator generator = keygenerator.getinstance("des");
      generator.init(new securerandom(strkey.getbytes())); // 根据参数生成key
      this.key = generator.generatekey();
    } catch (exception e) {
      e.printstacktrace();
    }
  }
 
  public string encrypt(string source) {
    return encrypt(source, "utf-8");
  }
 
  public string decrypt(string encrypteddata) {
    return decrypt(encrypteddata, "utf-8");
  }
 
  public string encrypt(string source, string charset) {
    string encrypt = null;
    try {
      byte[] ret = encrypt(source.getbytes(charset));
      encrypt = new string(base64.encode(ret));
    } catch (exception e) {
      e.printstacktrace();
      encrypt = null;
    }
    return encrypt;
  }
 
  public string decrypt(string encrypteddata, string charset) {
    string descrypteddata = null;
    try {
      byte[] ret = descrypt(base64.decode(encrypteddata.tochararray()));
      descrypteddata = new string(ret, charset);
    } catch (exception e) {
      e.printstacktrace();
      descrypteddata = null;
    }
    return descrypteddata;
  }
 
  private byte[] encrypt(byte[] primarydata) {
    try {
      cipher cipher = cipher.getinstance("des"); // cipher对象实际完成加密操作
      cipher.init(cipher.encrypt_mode, this.key); // 用密钥初始化cipher对象(加密)
 
      return cipher.dofinal(primarydata);
    } catch (exception e) {
      e.printstacktrace();
      return null;
    }
  }
 
  private byte[] descrypt(byte[] encrypteddata) {
    try {
      cipher cipher = cipher.getinstance("des"); // cipher对象实际完成解密操作
      cipher.init(cipher.decrypt_mode, this.key); // 用密钥初始化cipher对象(解密)
 
      return cipher.dofinal(encrypteddata);
    } catch (exception e) {
      e.printstacktrace();
      return null;
    }
  }
 
  public static void main(string[] args) {
    string code = "ceet";
    desutil desutil = new desutil("key");
    string encrypt = desutil.encrypt(code);
    string decrypt = desutil.decrypt(encrypt);
    system.out.println("原内容:" + code);
    system.out.println("加密:" + encrypt);
    system.out.println("解密:" + decrypt);
  }
}

以上这篇java最简单的des加密算法实现案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: