3

So this is my decrypt function:

def decrypt(value):
    key = b'1111111111111111'

    cipher = AES.new(key, AES.MODE_ECB)
    msg = cipher.decrypt(base64.b64decode(value))

    return msg

And i think I am missing something here, because when i give it a string to decrypt it also adds some strange characters :

Example : hey\r\r\r\r\r\r\r\r\r\r\r\r\r

What am i doing wrong?

P.S. I am using google app engine and this lib from Crypto.Cipher import AES

EDIT :

private static final String ALGO = "AES";
private static final byte[] keyValue = new byte[] { '1', '1', '1', '1', '1', '1', '1', '1', '1','1', '1', '1','1','1','1','1' };

public static String encrypt(String Data) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
     //   String encryptedValue = new BASE64Encoder().encode(encVal);

        byte[] decoded = Base64.encodeBase64(encVal);

        return (new String(decoded, "UTF-8") + "\n");
    }

private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
    }
Teshte
  • 624
  • 1
  • 7
  • 26

3 Answers3

0

how bout just doing

return msg.strip()

that will get rid of all those \r's

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 1
    problem is that the special characters depend. they are `\b` or `\u00006` or '\r'. The problem is that this AES alg needs to have the input value to be a multiple of 16. And the values that are not, are completed with these special characters. And probably there is a mistake in my decryption alg – Teshte Jun 20 '13 at 19:20
0

My bad that i didn't search for an answer enough

Here is a link with the answer : Encrypt & Decrypt using PyCrypto AES 256

Thanks anyway

Community
  • 1
  • 1
Teshte
  • 624
  • 1
  • 7
  • 26
  • Don't point to another answer to answer your own question. I think your question is still valid, even though another similar question exists (which however uses zero padding). The question you are pointing to is about something different, so that one is not a duplicate either. So in this case a good answer is probably required - if it was a dupe or a localized issue then it should be deleted. – Maarten Bodewes Jun 22 '13 at 19:38
0

As Java defaults to "PKCS#5Padding" it is pretty safe to assume you have to deal with those padding bytes (one to blocksize - in this case 16 - bytes are always padded). The Java "PKCS#5Padding" padding mode is actually PKCS#7 padding, as PKCS#5 only specifies padding for 8 byte blocks (e.g. DES mode encryption).

So this means you have to look for PKCS#5 padding or PKCS#7 padding. A quick search shows this answer.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263