decryption with AES
-
hi everybody :) i'm trying to write a program that uses "brute force" to decrypt an encrypted message (using AES), where I only have the first 96-bits of the 128-bit secret key when i complie i got this exception: Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded:confused: my code:
public static byte[] encrypt (byte[] plainText , SecretKeySpec spc) throws NoSuchAlgorithmException,GeneralSecurityException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE,spc); byte[] cipherText = cipher.doFinal( plainText); return cipherText ; } public static byte[] decrypt (byte[] cipherText , SecretKeySpec spec) throws NoSuchAlgorithmException,GeneralSecurityException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, spec); byte[] plainText = cipher.doFinal(cipherText); return plainText ; } public static void readfile(RandomAccessFile file, byte[] bytes) throws Exception { file.seek(0); int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=file.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } } public static void main(String[] args) throws Exception { try { // 1: open cipher file RandomAccessFile cipherfile = new RandomAccessFile( "ciphertext2.dat", "r" ); // read cipher text byte[] bytes = new byte[(int)cipherfile.length()]; readfile(cipherfile,bytes); // 3: RandomAccessFile file = new RandomAccessFile( "partial-key.dat", "r" ); byte[] keybytes = new byte[(int)file.length()+4]; readfile(file,keybytes); for(int i=0;i<256;i++) { keybytes[12]=(byte)i; for(int j=0;j<256;j++) { keybytes[13]=(byte)j; for(int k=0;k<265;k++) { keybytes[14]=(byte)k; for(int l=0;l<265;l++) { keybytes[15]=(byte)l; SecretKeySpec key = new SecretKeySpec(keybytes, "AES"); byte[] plain=new byte[(int)cipherfile.length()];
-
hi everybody :) i'm trying to write a program that uses "brute force" to decrypt an encrypted message (using AES), where I only have the first 96-bits of the 128-bit secret key when i complie i got this exception: Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded:confused: my code:
public static byte[] encrypt (byte[] plainText , SecretKeySpec spc) throws NoSuchAlgorithmException,GeneralSecurityException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE,spc); byte[] cipherText = cipher.doFinal( plainText); return cipherText ; } public static byte[] decrypt (byte[] cipherText , SecretKeySpec spec) throws NoSuchAlgorithmException,GeneralSecurityException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, spec); byte[] plainText = cipher.doFinal(cipherText); return plainText ; } public static void readfile(RandomAccessFile file, byte[] bytes) throws Exception { file.seek(0); int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=file.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } } public static void main(String[] args) throws Exception { try { // 1: open cipher file RandomAccessFile cipherfile = new RandomAccessFile( "ciphertext2.dat", "r" ); // read cipher text byte[] bytes = new byte[(int)cipherfile.length()]; readfile(cipherfile,bytes); // 3: RandomAccessFile file = new RandomAccessFile( "partial-key.dat", "r" ); byte[] keybytes = new byte[(int)file.length()+4]; readfile(file,keybytes); for(int i=0;i<256;i++) { keybytes[12]=(byte)i; for(int j=0;j<256;j++) { keybytes[13]=(byte)j; for(int k=0;k<265;k++) { keybytes[14]=(byte)k; for(int l=0;l<265;l++) { keybytes[15]=(byte)l; SecretKeySpec key = new SecretKeySpec(keybytes, "AES"); byte[] plain=new byte[(int)cipherfile.length()];
-
mesho wrote:
sorry, it was a typing error
Are you still getting the same error after correcting this? It looks like your encrypted data may be incorrect.
-
yes i don't think that the problem is with encrypted data how about reading encrypted data, and key? is there any mistake?
mesho wrote:
i don't think that the problem is with encrypted data
That is what the Padding Exception seems to be saying! You may need to look at the encryption side to see how the data looks at the end and then make certain that you reconstruct it in the same way during the decryption phase. Obviously if your key is wrong this may also affect the result.