java 单des算法_Java最简单的DES加密算法实现案例

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加密算法实现案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持萬仟网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值