java des加密能不能指定key,而不是生成 我想在本地把数据加密,然后上传到服务器解密,想使用des算法,但是java中能不能指定固定key,而不是生成key吗?加密java服务器算法数据 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 当然可以指定key,Base64,我用的都是自己指定key啊 可以指定key,来个实例:public class DesUtils { private static String strDefaultKey = "national"; private Cipher encryptCipher = null; private Cipher decryptCipher = null; public static String byteArr2HexStr(byte[] arrB) { int iLen = arrB.length; StringBuffer sb = new StringBuffer(iLen * 2); for (int i = 0; i < iLen; i++) { int intTmp = arrB[i]; while (intTmp < 0) { intTmp = intTmp + 256; } if (intTmp < 16) { sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); } public static byte[] hexStr2ByteArr(String strIn) { byte[] arrB = strIn.getBytes(); int iLen = arrB.length; byte[] arrOut = new byte[iLen / 2]; for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2); arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); } return arrOut; } public DesUtils() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException { this(strDefaultKey); } public DesUtils(String strKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { Security.addProvider(new com.sun.crypto.provider.SunJCE()); Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES"); decryptCipher.init(Cipher.DECRYPT_MODE, key); } public byte[] encrypt(byte[] arrB) throws IllegalBlockSizeException, BadPaddingException { return encryptCipher.doFinal(arrB); } public String encrypt(String strIn) throws IllegalBlockSizeException, BadPaddingException { return byteArr2HexStr(encrypt(strIn.getBytes())); } public byte[] decrypt(byte[] arrB) throws IllegalBlockSizeException, BadPaddingException { return decryptCipher.doFinal(arrB); } public String decrypt(String strIn) throws IllegalBlockSizeException, BadPaddingException { return new String(decrypt(hexStr2ByteArr(strIn))); } private Key getKey(byte[] arrBTmp) { byte[] arrB = new byte[8]; for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { arrB[i] = arrBTmp[i]; } Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); return key; }} 菜鸟问题:java中的方法内参数如何互相调用 并于weblogic的并发及最大连接限制 对数据库进行增删改和获得表中所有信息,方法调用怎么就不对啊! struts2里 用JavaScript传递参数 怎么搞啊 struts2 session 问题 Mysql问题(10055) 程序员一年多的困惑? struts问题急(forward 问题, 在线) help!!!求关于JMX方面的资料 请问在sun网站上的什么地方可以下载j2ee? 我怎么找不到呢? 谁有soap1.1 的webservice 的demo啊 spring 前途如何?
private Cipher encryptCipher = null;
private Cipher decryptCipher = null; public static String byteArr2HexStr(byte[] arrB) {
int iLen = arrB.length;
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
while (intTmp < 0) {
intTmp = intTmp + 256;
}
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
} public static byte[] hexStr2ByteArr(String strIn) {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length; byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
} public DesUtils() throws InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
this(strDefaultKey);
} public DesUtils(String strKey) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
} public byte[] encrypt(byte[] arrB) throws IllegalBlockSizeException,
BadPaddingException {
return encryptCipher.doFinal(arrB);
} public String encrypt(String strIn) throws IllegalBlockSizeException,
BadPaddingException {
return byteArr2HexStr(encrypt(strIn.getBytes()));
} public byte[] decrypt(byte[] arrB) throws IllegalBlockSizeException,
BadPaddingException {
return decryptCipher.doFinal(arrB);
} public String decrypt(String strIn) throws IllegalBlockSizeException,
BadPaddingException {
return new String(decrypt(hexStr2ByteArr(strIn)));
} private Key getKey(byte[] arrBTmp) {
byte[] arrB = new byte[8]; for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
}