加密解密

本文介绍了一种使用Java实现的DES加密与解密的方法。通过具体的代码示例,展示了如何利用DESKeySpec和SecretKeyFactory生成密钥,以及Cipher类进行数据的加密和解密过程。同时,还提供了Base64编码和解码的功能,以便于加密后的数据可以方便地在网络中传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.ByteArrayOutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
* ���ܽ���
*
* @author weichangyong
*/
public class EncryUtil
{
private final static BASE64Encoder base64encoder = new BASE64Encoder();
private final static BASE64Decoder base64decoder = new BASE64Decoder();
private final static String encoding = "UTF-8";

/**
* �����ַ�
*/
public static String encry(String str)
{
String result = str;
if (str != null && str.length() > 0)
{
try
{
byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding));
result = base64encoder.encode(encodeByte);
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}

/**
* �����ַ�
*/
public static String unEncry(String str)
{
String result = str;
if (str != null && str.length() > 0)
{
try
{
byte[] encodeByte = base64decoder.decodeBuffer(str);
byte[] decoder = EncryUtil.symmetricDecrypto(encodeByte);
result = new String(decoder, encoding);
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}

/**
*
* @param byteSource
* @return ������ܵ����
* @throws Exception
*/
public static byte[] symmetricEncrypto(byte[] byteSource) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
int mode = Cipher.ENCRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = { 1, 9, 8, 2, 0, 8, 2, 1 };
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
return result;
}
catch (Exception e)
{
throw e;
}
finally
{
baos.close();
}
}

/**
*
* @param byteSource
* @return ������ܵ����
* @throws Exception
*/
public static byte[] symmetricDecrypto(byte[] byteSource) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
int mode = Cipher.DECRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = { 1, 9, 8, 2, 0, 8, 2, 1 };
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
return result;
}
catch (Exception e)
{
throw e;
}
finally
{
baos.close();
}
}

public static void main(String[] args)
{
String test = "15727364097250141";

String re = encry(test);

System.out.println(re);

String a = "8iNYkkN fT/oAQtCc52mpvgcdQ8jEpSU";

if(a.indexOf(" ")>0)
{
a = a.replace(' ', '+');
}

String un = unEncry(a);

System.out.println(un);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值