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.