ph4r05/Whitebox-crypto-AES-java

serialization not works!

membrown opened this issue · 2 comments

hello

I use this code for generating and then serializing the AES white box instance

System.out.println("generate");
Generator gEnc = new Generator();
Generator gDec = new Generator();
Random rand = new Random();

// External encoding is needed, at least some, generate identities
ExternalBijections extc = new ExternalBijections();
gEnc.generateExtEncoding(extc, 0);

// at first generate pure table AES implementation
gEnc.setUseIO04x04Identity(false);
gEnc.setUseIO08x08Identity(false);
gEnc.setUseMB08x08Identity(false);
gEnc.setUseMB32x32Identity(false);

gDec.setUseIO04x04Identity(false);
gDec.setUseIO08x08Identity(false);
gDec.setUseMB08x08Identity(false);
gDec.setUseMB32x32Identity(false);

// Generate AES for encryption
gEnc.generate(true, AEShelper.testVect128_key, 16, extc);
AES AESenc = gEnc.getAESi();

// Generate AES for decryption
gDec.generate(false, AEShelper.testVect128_key, 16, extc);
AES AESdec = gDec.getAESi();


//********************************
try {

    FileOutputStream fileOut
            = new FileOutputStream("AESenc.ser");
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(AESenc);
    out.close();
    fileOut.close();
    System.out.printf("Serialized data is saved in AESenc.ser");
} catch (IOException i) {
    i.printStackTrace();
}

try {
    FileOutputStream fileOut
            = new FileOutputStream("AESdec.ser");
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(AESdec);
    out.close();
    fileOut.close();
    System.out.printf("Serialized data is saved in AESdec.ser");
} catch (IOException i) {
    i.printStackTrace();
}

but when read file and de-serialize objects encrypt/decrypt not work properly
this is code

Generator gEnc = new Generator();
Generator gDec = new Generator();
Random rand = new Random();

// External encoding is needed, at least some, generate identities
ExternalBijections extc = new ExternalBijections();
gEnc.generateExtEncoding(extc, 0);

// at first generate pure table AES implementation
gEnc.setUseIO04x04Identity(false);
gEnc.setUseIO08x08Identity(false);
gEnc.setUseMB08x08Identity(false);
gEnc.setUseMB32x32Identity(false);

gDec.setUseIO04x04Identity(false);
gDec.setUseIO08x08Identity(false);
gDec.setUseMB08x08Identity(false);
gDec.setUseMB32x32Identity(false);

AES AESenc ;
AES AESdec ;

try {
    FileInputStream fileIn = new FileInputStream("AESenc.ser");
    ObjectInputStream in = new ObjectInputStream(fileIn);
    AESenc = (AES) in.readObject();
    in.close();
    fileIn.close();
} catch (IOException i) {
    i.printStackTrace();
    return;
} catch (ClassNotFoundException c) {
    System.out.println("AESenc.ser not found");
    c.printStackTrace();
    return;
}

try {
    FileInputStream fileIn = new FileInputStream("AESdec.ser");
    ObjectInputStream in = new ObjectInputStream(fileIn);
    AESdec = (AES) in.readObject();
    in.close();
    fileIn.close();
} catch (IOException i) {
    i.printStackTrace();
    return;
} catch (ClassNotFoundException c) {
    System.out.println("AESenc.ser not found");
    c.printStackTrace();
    return;
}

//################################
gEnc.setAESi(AESenc);
gDec.setAESi(AESdec);

.
.
.
// Encrypt
state.transpose();
gEnc.applyExternalEnc(state, extc, true);
AESenc.crypt(state);
gEnc.applyExternalEnc(state, extc, false);

    System.out.println("Enc(plaintext_test): \n" + state);
    //assertEquals("Cipher output mismatch", true, state.equals(cipher));

    // Decrypt
    state.transpose();
    System.out.println("T(Enc(plaintext_test)): \n" + state);
    gDec.applyExternalEnc(state, extc, true);
    AESdec.crypt(state);
    gDec.applyExternalEnc(state, extc, false);
    System.out.println("Dec(T(Enc(plaintext_test))): \n" + state.toString());
    System.out.println(new String(state.getStateCopy()));

but not work
please help me.

@membrown Could you share with us what is the status of your issue and if you solve the issue?

@membrown @AmrAshraf you can try it with this PR.