当前位置:首页 » 编程语言 » C加密java

C加密java

发布时间: 2025-02-27 05:12:11

A. des解密算法,利用C语言解密JAVA语言加密的密码。。密钥为12345678,加密后的密文为:26d086be3a3a62fc

// C 语言 DES用的是 ECB模式, 没有填充
// 因此Java端要对应, 你的明文是 liubiao 吗?
// 另外 DES已经不安全了, 如果可以改为 3DES或者 AES吧。
public class LearnDes {

public static void main(String[] args) {
try {
System.out.println(encrypt("liubiao", "12345678"));

System.out.println(decrypt("26d086be3a3a62fc", "12345678"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encrypt(String message, String key) throws Exception {
//Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance("DES/ECB/NOPADDING");

DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
//cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey );

return toHexString(cipher.doFinal(message.getBytes("UTF-8")));
}

public static String decrypt(String message, String key) throws Exception {

byte[] bytesrc = convertHexString(message);

//Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance("DES/ECB/NOPADDING");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

//cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey );

byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}

public static byte[] convertHexString(String ss) {
byte digest[] = new byte[ss.length() / 2];
for (int i = 0; i < digest.length; i++) {
String byteString = ss.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte) byteValue;
}

return digest;
}
public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}

return hexString.toString();
}
}

热点内容
电脑怎么解压文件步骤 发布:2025-09-16 18:32:10 浏览:390
编译器默认构造函数内联 发布:2025-09-16 18:30:40 浏览:259
密码忘了怎么改 发布:2025-09-16 18:29:54 浏览:160
金盾加密视频版本识别 发布:2025-09-16 18:22:02 浏览:551
二手车宝马320li17年有哪些配置 发布:2025-09-16 17:59:32 浏览:890
c语言n次方怎么输入 发布:2025-09-16 17:51:17 浏览:910
完美国际密码在哪里改 发布:2025-09-16 17:43:44 浏览:908
网盘解压包怎么打开 发布:2025-09-16 16:59:19 浏览:819
红火脚本 发布:2025-09-16 16:53:21 浏览:990
centosphp56 发布:2025-09-16 16:52:24 浏览:259