Python 官方文档:入门教程 => 点击学习
一般3Des加密的秘钥是一个24位的字节数组,但是很多遇到32位字符串秘钥,不知道怎么去用,其实只是经过几步转化就可以了。希望这篇文章对大家有帮助或者带来灵感比如:秘钥:33333333333333333333333333333333要加密
一般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 + "'");
}
}
--结束END--
本文标题: 3DES,32位长秘钥加密
本文链接: https://lsjlt.com/news/187855.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0