java版3des加密程序,可与php兼容

代码:

import java.io.UnsupportedEncodingException;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

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

public class DESCoder
{
private static BASE64Encoder base64 = new BASE64Encoder();
private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };
//private static String strkey = "W9qPIzjaVGKUp7CKRk/qpCkg/SCMkQRu"; // 字节数必须是8的倍数
private static String strkey = "01234567890123456789012345678912";
public static String desEncrypt(String input) throws Exception
{

BASE64Decoder base64d = new BASE64Decoder();
DESedeKeySpec p8ksp = null;
p8ksp = new DESedeKeySpec(base64d.decodeBuffer(strkey));
Key key = null;
key = SecretKeyFactory.getInstance("DESede").generateSecret(p8ksp);

input = padding(input);
byte[] plainBytes = (byte[])null;
Cipher cipher = null;
byte[] cipherText = (byte[])null;

plainBytes = input.getBytes("UTF8");
cipher = Cipher.getInstance("DESede/CBC/NoPadding");
SecretKeySpec myKey = new SecretKeySpec(key.getEncoded(), "DESede");
IvParameterSpec ivspec = new IvParameterSpec(myIV);
cipher.init(1, myKey, ivspec);
cipherText = cipher.doFinal(plainBytes);
return removeBR(base64.encode(cipherText));

}

public static String desDecrypt(String cipherText) throws Exception
{

BASE64Decoder base64d = new BASE64Decoder();
DESedeKeySpec p8ksp = null;
p8ksp = new DESedeKeySpec(base64d.decodeBuffer(strkey));
Key key = null;
key = SecretKeyFactory.getInstance("DESede").generateSecret(p8ksp);

Cipher cipher = null;
byte[] inPut = base64d.decodeBuffer(cipherText);
cipher = Cipher.getInstance("DESede/CBC/NoPadding");
SecretKeySpec myKey = new SecretKeySpec(key.getEncoded(), "DESede");
IvParameterSpec ivspec = new IvParameterSpec(myIV);
cipher.init(2, myKey, ivspec);
byte[] output = removePadding(cipher.doFinal(inPut));

return new String(output, "UTF8");

}

private static String removeBR(String str) {
StringBuffer sf = new StringBuffer(str);

for (int i = 0; i < sf.length(); ++i)
{
if (sf.charAt(i) == '\n')
{
sf = sf.deleteCharAt(i);
}
}
for (int i = 0; i < sf.length(); ++i)
if (sf.charAt(i) == '\r')
{
sf = sf.deleteCharAt(i);
}

return sf.toString();
}

public static String padding(String str)
{
byte[] oldByteArray;
try
{
oldByteArray = str.getBytes("UTF8");
int numberToPad = 8 - oldByteArray.length % 8;
byte[] newByteArray = new byte[oldByteArray.length + numberToPad];
System.arraycopy(oldByteArray, 0, newByteArray, 0, oldByteArray.length);
for (int i = oldByteArray.length; i < newByteArray.length; ++i)
{
newByteArray[i] = 0;
}
return new String(newByteArray, "UTF8");
}
catch (UnsupportedEncodingException e)
{
System.out.println("Crypter.padding UnsupportedEncodingException");
}
return null;
}
public static byte[] removePadding(byte[] oldByteArray)
{
int numberPaded = 0;
for (int i = oldByteArray.length; i >= 0; --i)
{
if (oldByteArray[(i - 1)] != 0)
{
numberPaded = oldByteArray.length - i;
break;
}
}

byte[] newByteArray = new byte[oldByteArray.length - numberPaded];
System.arraycopy(oldByteArray, 0, newByteArray, 0, newByteArray.length);

return newByteArray;
}

public static void main(String args[])
{
try {
String desstr = DESCoder.desEncrypt("1qaz2ws");
String pstr = DESCoder.desDecrypt(desstr);
System.out.println("plainText:1qaz2ws");
System.out.println("Encode:"+desstr);
System.out.println("Decode:"+pstr);
} catch (Exception e) {
e.printStackTrace();
}
}
}

运行结果:
plainText:1qaz2ws
Encode:0GsXgYA8BuM=
Decode:1qaz2ws

和PHP的一样,呵呵。转载地址http://hi.baidu.com/farmerluo/blog/item/2d613a87ed0a782dc65cc397.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值