Java 3desede加解密_求大神给出个.NET实现的JAVA DESede加解密过程

package packages;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class Utils

{

public static String base64Encode(byte[] data)

{

if (data == null)

return null;

return Base64.encodeBase64String(data);

}

public static byte[] des3Encrypt(String encryptKey, byte[] metaData)

{

if ((encryptKey == null) || (metaData == null)) return null;

try

{

KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");

SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");

secureRandom.setSeed(encryptKey.getBytes());

keyGenerator.init(112, secureRandom);

SecretKeySpec secretKeySpec = new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "DESede");

Cipher cipher = Cipher.getInstance("DESede");

cipher.init(1, secretKeySpec);

return cipher.doFinal(metaData);

}

catch (Exception e)

{

}

return null;

}

public static byte[] base64Decode(String data)

{

if (data == null) return null;

return Base64.decodeBase64(data);

}

public static byte[] des3Decrypt(String decryptKey,byte[] cipherByte)

{

if ((decryptKey == null) || (cipherByte == null)) return null;

try

{

SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");

secureRandom.setSeed(decryptKey.getBytes());

KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");

keyGenerator.init(112, secureRandom);

byte[] key = keyGenerator.generateKey().getEncoded();

SecretKeySpec secretKeySpec = new SecretKeySpec(key, "DESede");

Cipher cipher = Cipher.getInstance("DESede");

cipher.init(2, secretKeySpec);

return cipher.doFinal(cipherByte);

}

catch (Exception e)

{

}

return null;

}

//加密

public String EncryptForDes(String encryptKey,String metaData)

{

String res = "";

try

{

byte[] decryData = des3Encrypt(encryptKey,metaData.getBytes());

res = base64Encode(decryData);

}

catch(Exception e)

{

}

return res;

}

//解密

public String DecryptForDes(String encryptKey,String metaData)

{

String res = "";

try

{

byte[] encryData = base64Decode(metaData);

byte[] decryData = des3Decrypt(encryptKey,encryData);

res = new String(decryData);

}

catch(Exception e)

{

}

return res;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值