简体   繁体   中英

How to print SealedObject encrypted data without file I/O?

Below is my code. when I try to print sealed object it only displays "javax.crypto.SealedObject@34dac684"

private void encryptUserCodes(List<UserCode> userCodes) {

        try {
            // generate a secret key using the DES algorithm
            key = KeyGenerator.getInstance("DES").generateKey();
            ecipher = Cipher.getInstance("DES");
            dcipher = Cipher.getInstance("DES");
            // initialize the ciphers with the given key
            ecipher.init(Cipher.ENCRYPT_MODE, key);
            dcipher.init(Cipher.DECRYPT_MODE, key);
            // create a sealed object
            SealedObject sealed = new SealedObject((Serializable) userCodes, ecipher);
            //PRINT SEALED OBJECT HERE
        }
        catch(Exception e){
            e.printStackTrace();
        }
}

System.out.println will always print value of toString() method. In your case printing Class@hex is default implementation in Object class which gets inherited in all classes in java.

You can create a custom method to print the your object.

Provide method definition with traversing the desire result by calling getter methods from your object and print them. Concatenation and return is also an option.

1. Encrypt:

Create Outputstreams and use Base64 Encoder to get the String.

2. Decrypt:

Create a new Cipher, Inputstreams and use Base 64 Decoder to get back your original String.

Fully working example (just copy and paste):

import javax.crypto.SecretKey;
import javax.crypto.KeyGenerator;
import javax.crypto.Cipher;
import javax.crypto.SealedObject;
import java.io.Serializable;

import java.io.ByteArrayOutputStream;
import javax.crypto.CipherOutputStream;
import java.io.ObjectOutputStream;

import java.io.ByteArrayInputStream;
import javax.crypto.CipherInputStream;
import java.io.ObjectInputStream;

import java.util.Base64;

public class MyClass {
    public static void main(String args[]) {
        OtherClass myObject = new OtherClass();
        myObject.print();
    }
}

// you can add other public classes to this editor in any order
class OtherClass
{
public void print() {


 try {
       String userCodes = "Test123";
        // generate a secret key using the DES algorithm
        SecretKey key = KeyGenerator.getInstance("DES").generateKey();
        Cipher ecipher = Cipher.getInstance("DES");
        // initialize the ciphers with the given key
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        // create a sealed object
        SealedObject sealed = new SealedObject((Serializable) userCodes, ecipher);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CipherOutputStream cipherOutputStream = new CipherOutputStream(
            outputStream, ecipher);

    ObjectOutputStream oos = new ObjectOutputStream(cipherOutputStream);
    oos.writeObject( sealed );
    cipherOutputStream.close();

    byte[] values = outputStream.toByteArray();

    String base64encoded = Base64.getEncoder().encodeToString(values);
    System.out.println(base64encoded);

    // decrypt
    Cipher fcipher = Cipher.getInstance("DES");
    fcipher.init(Cipher.DECRYPT_MODE, key);

    ByteArrayInputStream istream = new ByteArrayInputStream(Base64.getDecoder().decode(base64encoded));
    CipherInputStream cipherInputStream = new CipherInputStream(istream, fcipher);
    ObjectInputStream inputStream = new ObjectInputStream(cipherInputStream);
    SealedObject sealdedObject = (SealedObject) inputStream.readObject();
    System.out.println(sealdedObject.getObject(key));

}
catch(Exception e){
    e.printStackTrace();
}
}
}

Your sealed object is serializable. Thus you can write it to ObjectOutputStream:

try(ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) {
    out.writeObject(sealed);
    byte [] bytes =  bos.toByteArray();
    System.out.println(bytes);
} catch (IOException e) {
    e.printStackTrace();
}

To print it more user friendly, you can encode it in base64:

String base64encoded = Base64.getEncoder().encodeToString(bytes);
System.out.println(base64encoded);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM