Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualHelloSkitty

Posted 24 September 2012 - 09:36 PM

I am trying to use the javax.crypto java library to encrypt and decrypt strings of text using the Advanced Encryption Standard.  Unfortunately, I seem to be doing something very wrong, as I get the error "Given Final Block Not Properly Padded".  And I do not know what this means.

A code snippet is here:

public String encode(String s) {
  String encodingKey="sixteencharacter";
  String result="DEFAULT STRING TO SEND";
  try {SecretKeySpec key = new SecretKeySpec(encodingKey.getBytes(), "AES");
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
  cipher.init(Cipher.ENCRYPT_MODE, key);
  result = new String(cipher.doFinal(s.getBytes()));
  } catch (Exception e) {System.out.println("ERROR WHILE ENCODING");}
  return result;
}

public String decode(String s) {
  String decodingKey="sixteencharacter";
  String result="DEFAULT STRING TO SEND";
  try {SecretKeySpec key = new SecretKeySpec(decodingKey.getBytes(), "AES");
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
  cipher.init(Cipher.DECRYPT_MODE, key);
  result = new String(cipher.doFinal(s.getBytes()));
  } catch (Exception e) {System.out.println("ERROR WHILE DECODING");e.printStackTrace();}
  return result;
}

And when I try to call a line like, say,

String s=decode(encode("WELCOME:BOB"));

I get the error.

Other Strings I have tried that do not work are "WELCOME:ALICE", "WELCOME:NEMO".  However, I have also found strings that do decode properly, such as "WELCOME:HI", "WELCOME:DAVE", "WELCOME:SQUARE", "WELCOME:NOBODY".

I do not know why some strings work and some don't.  I modeled the decoding method to mirror the encoding, so I am at a loss to explain any of these results.

Any help is appreciated!

#1HelloSkitty

Posted 24 September 2012 - 09:32 PM

I am trying to use the javax.crypto java library to encrypt and decrypt strings of text.  Unfortunately, I seem to be doing something very wrong, as I get the error "Given Final Block Not Properly Padded".  And I do not know what this means.

A code snippet is here:

public String encode(String s) {
  String encodingKey="sixteencharacter";
  String result="DEFAULT STRING TO SEND";
  try {SecretKeySpec key = new SecretKeySpec(encodingKey.getBytes(), "AES");
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
  cipher.init(Cipher.ENCRYPT_MODE, key);
  result = new String(cipher.doFinal(s.getBytes()));
  } catch (Exception e) {System.out.println("ERROR WHILE ENCODING");}
  return result;
}

public String decode(String s) {
  String decodingKey="sixteencharacter";
  String result="DEFAULT STRING TO SEND";
  try {SecretKeySpec key = new SecretKeySpec(decodingKey.getBytes(), "AES");
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
  cipher.init(Cipher.DECRYPT_MODE, key);
  result = new String(cipher.doFinal(s.getBytes()));
  } catch (Exception e) {System.out.println("ERROR WHILE DECODING");e.printStackTrace();}
  return result;
}

And when I try to call a line like, say,

String s=decode(encode("WELCOME:BOB"));

I get the error.

Other Strings I have tried that do not work are "WELCOME:ALICE", "WELCOME:NEMO".  However, I have also found strings that do decode properly, such as "WELCOME:HI", "WELCOME:DAVE", "WELCOME:SQUARE", "WELCOME:NOBODY".

I do not know why some strings work and some don't.  I modeled the decoding method to mirror the encoding, so I am at a loss to explain any of these results.

Any help is appreciated!

PARTNERS