简体   繁体   English

如何在没有文件I / O的情况下打印SealedObject加密的数据?

[英]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" 当我尝试打印密封对象时,它仅显示“ 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. System.out.println将始终打印toString()方法的值。 In your case printing Class@hex is default implementation in Object class which gets inherited in all classes in java. 在您的情况下,打印Class @ hex是Object类中的默认实现,该实现在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. 通过从对象中调用getter方法并打印它们来提供遍历所需结果的方法定义。 Concatenation and return is also an option. 串联和返回也是一种选择。

1. Encrypt: 1.加密:

Create Outputstreams and use Base64 Encoder to get the String. 创建Outputstreams并使用Base64编码器获取String。

2. Decrypt: 2.解密:

Create a new Cipher, Inputstreams and use Base 64 Decoder to get back your original String. 创建一个新的密码输入流,并使用Base 64 Decoder返回原始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. 您的sealed对象是可序列化的。 Thus you can write it to ObjectOutputStream: 因此,您可以将其写入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: 为了更友好地打印它,可以在base64中对其进行编码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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