· 7 years ago · Apr 06, 2018, 11:12 AM
1$key = substr(sha1("m",true),0,8); //8-byte key
2$iv = mcrypt_encrypt (MCRYPT_DES,$key,hex2bin("0000000000000000"),MCRYPT_MODE_ECB); //get Iv
3print(bin2hex($iv)."n"); // "b5872289d3c49605"
4$plain_text = "MZ4aXMCMO/TQAsZ2bYwagw==";
5$plain_text = base64_decode($plain_text);
6$cipher_text = mcrypt_decrypt (MCRYPT_DES, $key, $plain_text, MCRYPT_MODE_CFB, $iv);
7print("nThe ciphertext: ".$cipher_text);
8print("nThe expected : "."489B4F2ADD728755");
9
10public static void desCipherDecrypt(String key, String encodedString) throws Exception {
11 byte [] keyByte = Arrays.copyOf(DigestUtils.sha1(key),8);
12 byte [] encodedByte = Base64.decodeBase64(encodedString);
13 byte [] iv = DatatypeConverter.parseHexBinary("B5872289D3C49605"); //same as Iv in php
14
15 AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
16 Cipher desCipherDecrypt;
17
18 desCipherDecrypt = Cipher.getInstance("DES/CFB/NoPadding");
19 DESKeySpec dks = new DESKeySpec(keyByte);
20 SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
21 SecretKey desKey = skf.generateSecret(dks);
22 desCipherDecrypt.init(Cipher.DECRYPT_MODE, desKey,paramSpec);
23 byte[] cryptByte = desCipherDecrypt.doFinal(encodedByte);
24
25 System.out.println("resultBytes: "+Arrays.toString(cryptByte)+"n");
26 System.out.println("resultHex: "+DatatypeConverter.printHexBinary(cryptByte)+"n");
27 System.out.println("Decryption ended");
28 }
29...
30desCipherDecrypt("m", "MZ4aXMCMO/TQAsZ2bYwagw==");