📄 des.java
字号:
package com.wireless.sms.sgip.http.global;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import com.wireless.sms.pub.util.Base64;
public class DES {
/**
* DES 加密算法
* @param data 处理数据
* @param key 密钥
* @param mode =0加密 =1解密
* @return
* @throws Exception
*/
public static byte[] des(String data, String key, int mode) {
if (mode == 0) {
mode = Cipher.ENCRYPT_MODE;
} else {
mode = Cipher.DECRYPT_MODE;
}
try {
Cipher cipher = Cipher.getInstance("DES");
SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "DES");
Key deskey = spec;
cipher.init(mode, deskey);
return crypt(new ByteArrayInputStream(data.getBytes()), cipher);
} catch (Exception e) {
e.printStackTrace();
return "".getBytes();
}
}
/**
* DES 加密算法
* @param data 处理数据
* @param key 密钥
* @param mode =0加密 =1解密
* @return
* @throws Exception
*/
public static byte[] des(byte[] data, byte[] key, int mode) {
if (mode == 0) {
mode = Cipher.ENCRYPT_MODE;
} else {
mode = Cipher.DECRYPT_MODE;
}
try {
Cipher cipher = Cipher.getInstance("TripleDES");
SecretKeySpec spec = new SecretKeySpec(key, "TripleDES");
Key deskey = spec;
cipher.init(mode, deskey);
return crypt(new ByteArrayInputStream(data), cipher);
} catch (Exception e) {
e.printStackTrace();
return "".getBytes();
}
}
/**
* Uses a cipher to transform the bytes in an input stream and sends the
* transformed bytes to an output stream.
*
* @param in
* @param cipher
* @return
* @throws IOException
* @throws GeneralSecurityException
*/
private static byte[] crypt(InputStream in, Cipher cipher)
throws IOException, GeneralSecurityException {
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int inLength = 0;
boolean more = true;
while (more) {
inLength = in.read(inBytes);
if (inLength == blockSize) {
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
// System.out.println(outLength);
} else
more = false;
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
// System.out.println(outBytes.length);
out.write(outBytes);
return (out.toByteArray());
}
public static void main(String[] args) {
try {
String sKey = "2005HLJLT1222SXIT1750zzYbgU908eL";
sKey = new String(Base64.decode(sKey.getBytes()));
SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes(),
"TripleDES");
Cipher cipher = Cipher.getInstance("TripleDES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] cleartext = "1234$13300000000$ABC$2005-12-22 15:29:27".getBytes();
byte[] encrypted = cipher.doFinal(cleartext, 0, cleartext.length);
// StringBuffer sb = new StringBuffer();
// for (int j = 0; j < encrypted.length; j++) {
// int k = encrypted[j];
// if (k < 0)
// k += 256;
// sb.append("%" + Integer.toHexString(k).toUpperCase());
// }
// System.out.println("result:" + sb.toString());
//
for(int i=0; i<encrypted.length; i++) {
System.out.print(encrypted[i] + ",");
}
System.out.println("\n" + new String(Base64.encode(encrypted)));
} catch (Exception e) {
System.err.println("Caught exception " + e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -