Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. decryption with AES

decryption with AES

Scheduled Pinned Locked Moved Java
cryptography
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mesho
    wrote on last edited by
    #1

    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()];

    L 1 Reply Last reply
    0
    • M mesho

      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()];

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      mesho wrote:

      for(int k=0;k<265;k++)

      Are you sure this should be 265?

      M 1 Reply Last reply
      0
      • L Lost User

        mesho wrote:

        for(int k=0;k<265;k++)

        Are you sure this should be 265?

        M Offline
        M Offline
        mesho
        wrote on last edited by
        #3

        sorry, it was a typing error what about my exception?any idea? :((

        L 1 Reply Last reply
        0
        • M mesho

          sorry, it was a typing error what about my exception?any idea? :((

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          M 1 Reply Last reply
          0
          • L Lost User

            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.

            M Offline
            M Offline
            mesho
            wrote on last edited by
            #5

            yes i don't think that the problem is with encrypted data how about reading encrypted data, and key? is there any mistake?

            L 1 Reply Last reply
            0
            • M mesho

              yes i don't think that the problem is with encrypted data how about reading encrypted data, and key? is there any mistake?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups