Sometimes I come across an interesting, strange thing: the same block of ciphertext can be decrypted using several different keys!
Can anyone tell me what is going wrong? Thank you very much.
Please do not try to let me switch to triple DES / AES, etc., I just want to know where the problem is: the way to call the Java SDK or an error in the Java SDK?
The following is displayed on Windows 7, the same in the Linux window:
D:\>java -version java version "1.7.0_21" Java(TM) SE Runtime Environment (build 1.7.0_21-b11) Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode) D:\>java DESTest -e 12345678 abcde977 encrypted as [17fd146fa6fdbb5db667efe657dfcb60] D:\>java DESTest -d 17fd146fa6fdbb5db667efe657dfcb60 abcde977 decryted as [12345678] D:\>java DESTest -d 17fd146fa6fdbb5db667efe657dfcb60 abcde976 decryted as [12345678] D:\>java DESTest -d 17fd146fa6fdbb5db667efe657dfcb60 abcde967 decryted as [12345678] D:\>java DESTest -d 17fd146fa6fdbb5db667efe657dfcb60 abcde867 decryted as [12345678] D:\>java DESTest -d 17fd146fa6fdbb5db667efe657dfcb60 abcdf867 Exception in thread "main" java.lang.RuntimeException: javax.crypto.BadPaddingEx ception: Given final block not properly padded at DESTest.des(DESTest.java:46) at DESTest.dec(DESTest.java:31) at DESTest.main(DESTest.java:19) Caused by: javax.crypto.BadPaddingException: Given final block not properly padd ed at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811) at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676) at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314) at javax.crypto.Cipher.doFinal(Cipher.java:2087) at DESTest.des(DESTest.java:44) ... 2 more D:\>java DESTest -e 12345678 abcde976 encrypted as [17fd146fa6fdbb5db667efe657dfcb60] D:\>java DESTest -e 12345678 abcde967 encrypted as [17fd146fa6fdbb5db667efe657dfcb60] D:\>
Source:
import java.io.UnsupportedEncodingException; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; public class DESTest { public static void main(String[] args) { if (args.length < 3) { System.out.println("usage: java " + DESTest.class.getCanonicalName() + " -e|-d text key"); return; } String mode = args[0].trim(); String text = args[1].trim(); String key = args[2].trim(); try { String s = "-d".equalsIgnoreCase(mode) ? dec(text, key) : enc(text, key); System.out.println("\n" + ("-d".equalsIgnoreCase(mode) ? "decryted as [" : "encrypted as [") + s + "]"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } private static String enc(String plainText, String key) throws UnsupportedEncodingException { return new String(encHex(des(plainText.getBytes("UTF-8"), key, Cipher.ENCRYPT_MODE))); } private static String dec(String encrypted, String key) throws UnsupportedEncodingException { return new String(des(decHex(encrypted), key, Cipher.DECRYPT_MODE), "UTF-8"); } private static byte[] des(byte[] bytes, String key, int cipherMode) { final String encoding = "UTF-8"; try { DESKeySpec desKey = new DESKeySpec(key.getBytes(encoding)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey);
source share