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

时间:2018-12-21 08:46:49

标签: java encryption public-key-encryption password-encryption

下面是我的代码。 当我尝试打印密封对象时,它仅显示“ 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();
        }
}

3 个答案:

答案 0 :(得分:2)

System.out.println将始终打印toString()方法的值。在您的情况下,打印Class @ hex是Object类中的默认实现,该实现在Java中的所有类中都继承。

您可以创建一个自定义方法来打印您的对象。

通过从对象中调用getter方法并打印它们来遍历所需结果,从而提供方法定义。串联和返回也是一种选择。

答案 1 :(得分:1)

1。加密:

创建Outputstreams并使用Base64编码器获取String。

2。解密:

创建一个新的密码,输入流,并使用Base 64解码器取回原始字符串。

完整的示例(只需复制并粘贴):

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();
}
}
}

答案 2 :(得分:1)

您的sealed对象是可序列化的。因此,您可以将其写入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();
}

要使其打印更加用户友好,您可以在base64中对其进行编码:

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