簡體   English   中英

如何在沒有文件I / O的情況下打印SealedObject加密的數據?

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

下面是我的代碼。 當我嘗試打印密封對象時,它僅顯示“ 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將始終打印toString()方法的值。 在您的情況下,打印Class @ hex是Object類中的默認實現,該實現在Java中的所有類中都繼承。

您可以創建一個自定義方法來打印您的對象。

通過從對象中調用getter方法並打印它們來提供遍歷所需結果的方法定義。 串聯和返回也是一種選擇。

1.加密:

創建Outputstreams並使用Base64編碼器獲取String。

2.解密:

創建一個新的密碼輸入流,並使用Base 64 Decoder返回原始String。

完整的示例(只需復制和粘貼):

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

您的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM