java 8反序列化base64编码的字符串

时间:2016-09-24 09:48:53

标签: java serialization

我正在参加夺旗比赛。我坚持的一个问题涉及Java对象的反序列化。我正在查询base64编码的cookie字符串。当我解码时,我相信它是一个Java序列化对象,我想反序列化以改变它们的值并在base64中重新编码。

我对java一无所知,我试图反序列化base64解码的字节,但我认为这是错误的类型。

import java.util.Base64;
import java.util.UUID;
import java.io.UnsupportedEncodingException;
import java.io.ObjectInputStream;

public class decode {
   public static void main(String args[]){
      try {

         // Encode using basic encoder
         String base64encodedString = "mybase64encodedstring==";
         System.out.println("Base64 encoded string :" + base64encodedString);

         // Decode
         byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);

         ObjectInputStream in = new ObjectInputStream(base64decodedBytes);
         System.out.println("Deserialised data: \n" + in.readObject().toString());

         System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));


      }catch(UnsupportedEncodingException e){
         System.out.println("Error :" + e.getMessage());
      }
   }
}

我得到的错误是:

  

不兼容的类型:byte []无法转换为InputStream

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:2)

使用 ByteArrayInputStream 代替 ObjectInputStream

所以,请使用:

<example-tag>
  <p id="courses-slider">Est-ce que j'existe ?</p>

  <script>
    this.on('mount', function(){

        $('#courses-slider').flexslider({
            animation: "slide",
            prevText: "",
            nextText: "",
            itemWidth: 292,
            itemMargin: 0,
            move: 1
        }); // Courses Slider

    });
  </script>
</example-tag>
而不是这个:

InputStream in = new ByteArrayInputStream(base64decodedBytes);

答案 1 :(得分:0)

@Hrabosch说使用ByteArrayInputStream 代替。实际上,使用ByteArrayInputStream 以及

另外,如果数据是由ObjectOuputStream写的一堆字节,那么从这些字节构造一个字符串是没有意义的

喜欢这样

import java.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class Decode {
   public static void main(String args[]){
      try {

         // Encode using basic encoder
         // I assume you replace this with actual data...
         String base64encodedString = "mybase64encodedstring==";
         System.out.println("Base64 encoded string :" +    base64encodedString);

         // Decode
         byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);

         InputStream in = new ByteArrayInputStream(base64decodedBytes);
         ObjectInputStream obin = new ObjectInputStream(in);
         Object object = obin.readObject();
         System.out.println("Deserialised data: \n" + object.toString());

         // You could also try...
         System.out.println("Object class is " + object.getClass().toString());

         // Don't do this!! The original data was not a string!
         //System.out.println("Original String: " + new     String(base64decodedBytes, "utf-8"));


      }catch(ClassNotFoundException | IOException e){
         System.out.println("Error :" + e.getMessage());
   }
   }  
}