3DES,32位长秘钥加密
短信预约 -IT技能 免费直播动态提醒
一般3des加密的秘钥是一个24位的字节数组,但是很多遇到32位字符串秘钥,不知道怎么去用,其实只是经过几步转化就可以了。希望这篇文章对大家有帮助或者带来灵感
比如:
秘钥:33333333333333333333333333333333
要加密内容:06111111FFFFFFFF
加密后内容:66322DAA27A95807
java代码
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import com.raying.abs.base.logger.Logger;
public class Des3EncryptUtils {
private SecretKey securekey;
public void setKey(byte[] key) {
try {
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
securekey = keyFactory.generateSecret(dks);
} catch (Exception e) {
Logger.error(e.getMessage(), e);
}
}
public byte[] get3DesEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DESede/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, securekey);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
Logger.error(e.getMessage(), e);
} finally {
cipher = null;
}
return byteFina;
}
public byte[] get3DesDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DESede/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, securekey);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
Logger.error(e.getMessage(), e);
} finally {
cipher = null;
}
return byteFina;
}
}
public class Des3Utils {
public static String decryption(String key, String content) {
Des3EncryptUtils des = new Des3EncryptUtils();
String enKey = "";//最终解密秘钥 48位
String enContent = "";//解密内容
if(key.length() <= 32){
enKey = (key + key).substring(0, 48);
}else if(key.length() >= 48){
enKey = key.substring(0, 48);
}
if(content.length() == 16){
enContent = content;
}else{
if(content.length() > 16){
throw new RuntimeException("the encrypt content length more than 16");
}else if(content.length() < 16){
throw new RuntimeException("the encrypt content length less than 16");
}
}
des.setKey(enKey.getBytes());
byte[] get3DesDesCode = des.get3DesDesCode(HexUtils.fromString(enContent));
return HexUtils.toString(get3DesDesCode).trim();
}
public static String encryption(String key, String content) {
Des3EncryptUtils des = new Des3EncryptUtils();
String enKey = "";//最终加密秘钥48位
String enContent = "";//加密内容
if(key.length() <= 32){
enKey = (key + key).substring(0, 48);
}else if(key.length() >= 48){
enKey = key.substring(0, 48);
}
if(content.length() == 16){
enContent = content;
}else{
if(content.length() > 16){
throw new RuntimeException("the encrypt content length more than 16");
}else if(content.length() < 16){
throw new RuntimeException("the encrypt content length less than 16");
}
}
des.setKey(enKey.getBytes());
byte[] bye = des.get3DesEncCode(HexUtils.fromString(enContent));
return HexUtils.toString(bye).trim();
}
}
public class HexUtils {
private static final char[] HEXDIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
public static String toString(byte[] ba, int offset, int length) {
char[] buf = new char[length * 2];
int j = 0;
int k;
for (int i = offset; i < offset + length; i++) {
k = ba[i];
buf[j++] = HEXDIGITS[(k >>> 4) & 0x0F];
buf[j++] = HEXDIGITS[k & 0x0F];
}
return new String(buf);
}
public static String toString(byte[] ba) {
return toString(ba, 0, ba.length);
}
public static byte[] fromString(String hex) {
int len = hex.length();
byte[] buf = new byte[(len + 1) / 2];
int i = 0;
int j = 0;
if ((len % 2) == 1) {
buf[j++] = (byte) fromDigit(hex.charAt(i++));
}
while (i < len) {
buf[j++] = (byte) ((fromDigit(hex.charAt(i++)) << 4) | fromDigit(hex.charAt(i++)));
}
return buf;
}
public static int fromDigit(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
if (ch >= 'A' && ch <= 'F') {
return ch - 'A' + 10;
}
if (ch >= 'a' && ch <= 'f') {
return ch - 'a' + 10;
}
throw new IllegalArgumentException("invalid hex digit '" + ch + "'");
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341