當前位置:首頁 » 編程語言 » 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();
}
}

熱點內容
linux的nohup命令 發布:2025-05-03 10:12:03 瀏覽:268
安卓手機同步通訊錄開關在哪裡 發布:2025-05-03 10:04:02 瀏覽:549
QT編譯純c代碼 發布:2025-05-03 10:04:00 瀏覽:171
我的世界伺服器領地熊家 發布:2025-05-03 10:03:53 瀏覽:98
浪潮伺服器怎麼拆硬碟 發布:2025-05-03 09:48:16 瀏覽:104
醉酒爸爸安卓版本在哪裡下載 發布:2025-05-03 09:47:22 瀏覽:54
python線程內存 發布:2025-05-03 09:38:47 瀏覽:467
解壓的玩泥 發布:2025-05-03 09:35:41 瀏覽:198
c語言哈夫曼樹 發布:2025-05-03 09:27:56 瀏覽:91
H的資料庫 發布:2025-05-03 09:21:40 瀏覽:318