package desjiami;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
/**
* DES加密工具,文件中共有?个方法,加密、解密
*
*/
public class DesUtil {
private String Algorithm = "DES";
private KeyGenerator keygen;
private SecretKey deskey;
private Cipher c;
private byte[] cipherByte; public DesUtil() {
init();
} public void init() {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
keygen = KeyGenerator.getInstance(Algorithm);
deskey = keygen.generateKey();
c = Cipher.getInstance(Algorithm);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (NoSuchPaddingException ex) {
ex.printStackTrace();
}
} /**
* ? String ?行加密
*
* @param str
* 要加密的数据
* @return 返回加密后的 byte 数?
*/
public byte[] createEncryptor(String str) {
try {
c.init(Cipher.ENCRYPT_MODE, deskey);
cipherByte = c.doFinal(str.getBytes());
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
}
return cipherByte;
} /**
* ? Byte 数??行解密
*
* @param buff
* 要解密的数据
* @return 返回加密后的 String
*/
public String createDecryptor(byte[] buff) {
try {
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(buff);
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
}
return (new String(cipherByte));
}
}
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
/**
* DES加密工具,文件中共有?个方法,加密、解密
*
*/
public class DesUtil {
private String Algorithm = "DES";
private KeyGenerator keygen;
private SecretKey deskey;
private Cipher c;
private byte[] cipherByte; public DesUtil() {
init();
} public void init() {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
keygen = KeyGenerator.getInstance(Algorithm);
deskey = keygen.generateKey();
c = Cipher.getInstance(Algorithm);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (NoSuchPaddingException ex) {
ex.printStackTrace();
}
} /**
* ? String ?行加密
*
* @param str
* 要加密的数据
* @return 返回加密后的 byte 数?
*/
public byte[] createEncryptor(String str) {
try {
c.init(Cipher.ENCRYPT_MODE, deskey);
cipherByte = c.doFinal(str.getBytes());
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
}
return cipherByte;
} /**
* ? Byte 数??行解密
*
* @param buff
* 要解密的数据
* @return 返回加密后的 String
*/
public String createDecryptor(byte[] buff) {
try {
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(buff);
} catch (java.security.InvalidKeyException ex) {
ex.printStackTrace();
} catch (javax.crypto.BadPaddingException ex) {
ex.printStackTrace();
} catch (javax.crypto.IllegalBlockSizeException ex) {
ex.printStackTrace();
}
return (new String(cipherByte));
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货