-1

I hava Java code that do AES encryption, and i would like to convert it so it work in python. This is a main class, i made initVector constants so it would be easier to see if it work:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.io.BufferedWriter;



public class test {
public static void main(String [] args)
{
    String ss = "pUypiz-7hJ0y_JtpKWaydp";
    String url = "";
    String toBeEncrypted = "";
    String initVector = "IqtY8jgALtjZNLM5";
    String encodedInitVector = Encryptor.encodeB64mod(initVector.getBytes());
    toBeEncrypted = "ch21979714702=put";
    ss = Encryptor.decodeB64Mod(ss);
    url = "i=" + encodedInitVector + "\n&a=" + Encryptor.encrypt(initVector, ss, toBeEncrypted);
    System.out.println(url);
}
}

And this is Encryptor class:

import android.util.Base64;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.io.IOUtils;
import java.util.*;

public class Encryptor {

public static String encrypt(String initVector, String key, String clearText) {
    try {
        byte[] value = clearText.getBytes("UTF-8");
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(Base64.decode(key, 0), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(1, skeySpec, iv);
        return encodeB64mod(cipher.doFinal(value));
    } catch (Exception ex) {
        ex.printStackTrace();
        return "";
    }
}

public static String encodeB64mod(byte[] bytes) {
    return Base64.encodeToString(bytes, 2).replace('+', '-').replace(IOUtils.DIR_SEPARATOR_UNIX, '_').replace("=", "");
}



}

I tried to make it work in python but i am doing something wrong

EDIT:

import base64
from Crypto.Cipher import AES
from Crypto import Random
raw ='ch21979714702=put'.encode('utf-8')
key = 'pUypiz-7hJ0y_JtpKWaydp' #should be conveted to hex
iv = b'IqtY8jgALtjZNLM5'
cipher = AES.new( key, AES.MODE_CBC, iv )
print (base64.b64encode( cipher.encrypt( raw ) ).decode('utf-8') )
ifandroid
  • 11
  • 7
  • This is not a code-writing service, you have to make an effort on your own first. – President James K. Polk Mar 25 '18 at 15:53
  • I added python code, i tried to make it work but no matter how i converted key i can't get 16bytes of length – ifandroid Mar 25 '18 at 15:56
  • What happens when you run the python code? – Code-Apprentice Mar 25 '18 at 16:01
  • "ValueError: Incorrect AES key length (22 bytes)" i tried to base64 decode key but then i get 11bytes for key – ifandroid Mar 25 '18 at 16:08
  • Variables are cheap (free actually), don't reuse for different things and make they to name what they hold, `ss` does not seem to name anything.. – zaph Mar 25 '18 at 16:13
  • Display and provide the variables `initVector`, `ss`, `toBeEncrypted` and add to the question. Display binary values in hex. Looking at these you should see the problem. – zaph Mar 25 '18 at 16:19
  • I can't change the "KUHMiz-7hJ0B_Jt6KWa1dg" this is my unique identifier, ss is the key in this case, i didn't change name of a variable because then i would need to change it everywere – ifandroid Mar 25 '18 at 16:23
  • I edited OP with the key in HEX, it looks like it's working but i miss last part in python – ifandroid Mar 25 '18 at 17:02
  • I tried with few different 'raw' messages and i always get last 22 character missing or wrong in base64 which is 16bytes, a am guessing that something is wrong with the last 16bytes when python tries to encrypt them – ifandroid Mar 25 '18 at 19:50

2 Answers2

1

You are requiring base64url decoding, which isn't the generic base 64 encoding: it replaces + with - and / with _. This is why the reverse replacement is performed in Java.

Here is the correct way to decode it. Please upvote it and downvote any answers that do not include the replacement characters or final padding with the = character.

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

The problem was in Python was that you need to use padding

from Crypto.Util import Padding Padding.pad(raw, 16, style='pkcs7')

https://www.pycryptodome.org/en/latest/src/util/util.html

Unlike in java in Python you need to add that you want to use padding.

ifandroid
  • 11
  • 7