Python and java use the AES encryption algorithm to encrypt strings to get encrypted strings that are different.

problem description

1, describe the details of the problem. Use the AES encryption algorithm of java and python to encrypt the same string, respectively. The encryption strings are different.
2,
2, problem description supplement 1, the variable of the two encryption, encryption key (key), offset (iv), encryption mode (CFB) are the same. The encryption string is the same.
3, problem description supplement 2, encrypted string experienced a BASE64 encryption

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)
python code:

-sharp!/usr/bin/env python
-sharp -*- coding: utf-8 -*-

from Crypto.Cipher import AES
import base64
import os

BLOCK_SIZE = 16
PADDING = "\0"
pad_it = lambda s: s+(16 - len(s)%16)*PADDING
key = b"B31F2A75FBF94099"
iv = b"1234567890123456"

-sharpaes
-sharpjavaPADDING
def encrypt_aes(sourceStr):
    generator = AES.new(key, AES.MODE_CFB, iv)
    crypt = generator.encrypt(pad_it(sourceStr).encode("utf-8"))
    cryptedStr = base64.b64encode(crypt)
    return cryptedStr

def decrypt_aes(cryptedStr):
    generator = AES.new(key, AES.MODE_CFB, iv)
    cryptedStr = base64.b64decode(cryptedStr)
    recovery = generator.decrypt(cryptedStr)
    decryptedStr = recovery.rstrip(PADDING.encode("utf-8"))
    return decryptedStr

sourceStr = "{"abc":"dfr","agh":"fdfd"}"


print(":",sourceStr)
print(":",encrypt_aes(sourceStr))
print(":",decrypt_aes(encrypt_aes(sourceStr)).decode("utf-8"))

python:

: 
: b"bYywQggIyUep1a55IRMe2Au0xGw="
: 

java:

package com.ehai.enctypt.aes;

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESEncrypt {

    /**
     * Key 26 AES-128-CBCkey16
     */
    private static String sKey = "B31F2A75FBF94099";
    private static String ivParameter = "1234567890123456";

    public static void main(String[] args) {
        String raw = "";

        try {
            String encryptRaw = AESEncrypt.encrypt(raw);
            System.out.println(":" + encryptRaw);

            String decryptRaw = AESEncrypt.decrypt(encryptRaw);

            System.out.println(":" + decryptRaw);

        } catch (Exception e) {

            e.printStackTrace();
        }

    }

    public static String encrypt(String sSrc) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// CBCiv
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        // return new BASE64Encoder().encode(encrypted);//BASE64
        return new String(Base64.getEncoder().encode(encrypted), "UTF-8");
    }

    // 
    public static String decrypt(String sSrc) throws Exception {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            // byte[] encrypted1 = new
            // BASE64Decoder().decodeBuffer(sSrc);//base64
            byte[] encrypted1 = Base64.getDecoder().decode(sSrc);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

}



java:
:
:bRfM0cD63D96il8ZeXhJEw==
:

what result do you expect? What is the error message actually seen?

expect java and python to get the same encryption and decryption results.

Mar.10,2022

AES is indeed a complex and efficient encryption algorithm. Let me help you analyze the reasons for the different encryption results.
in fact, the code is fine, the key problem is to fill in ( padding ) algorithm and segment size ( segment size ).

first of all, ciphertext feedback mode (CFB) mode is a stream-based encryption scheme. In order to make stream ciphers that can handle arbitrary bit errors, CFB usually sets a segment size to indicate how many bytes of plaintext will be processed each time when encrypting plaintext through AES. For details of this process, please see .

according to this stackoverflow: python-and-decrypt-in-java-with-aes-cfb" rel=" nofollow noreferrer > Encrypt in python and decrypt in Java with AES-CFB , the PyCrypto module of
Python defaults to segment_size is 8 , and Java defaults to segment_size to 128 .
so make Python get the same encryption result as Java. Python and Java must use the same segment_size . Suppose we adapt Python to Java, using segment_size=128 , then we can set it as follows:

  

java has corresponding file stream encryption, should it be python?
the problem encountered at present is that java encrypts the file in the form of a file stream. How does python decrypt the encrypted file in the form of a file stream?

my understanding of file stream encryption is to read files with a fixed block size each time, but java file stream encryption does not seem to be like this

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3623a-4102e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3623a-4102e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?