2017-07-22 16 views
1

私はこれを使用しています: import com.sun.org.apache.xml.internal.security.utils.Base64; Base64文字列とバイト配列をエンコード/デコードしてdbに格納します。Javaベース64エンコード、デコード結果が異なる

私は元の文字列を取り戻すことができるかどうかを確認するために符号化と復号化をテストしています:

SecureRandom srand = new SecureRandom(); 
     byte[] randomSalt = new byte[64]; 
     srand.nextBytes(randomSalt); 
     System.out.println("rand salt bytes: " + randomSalt); // first line 
     String salt = Base64.encode(randomSalt); 
     try { 
      System.out.println(Base64.decode(salt)); // second line 
     }catch(Base64DecodingException e){ 
      System.out.println(e); 
     } 

しかし、これはプリントアウト:

rand salt bytes: [[email protected] 
[[email protected] 

なぜこれらは、同じではありません元のバイト配列を元に戻すことができますか?

答えて

2

あなたがやっていることは、実際のバイトの代わりに実際にJavaポインタを扱うことです。 これは実装する正しい方法です

byte[] bytesEncoded = Base64.encodeBase64(str .getBytes()); 
System.out.println("ecncoded value is " + new String(bytesEncoded)); 

// Decode data on other side, by processing encoded data 
byte[] valueDecoded= Base64.decodeBase64(bytesEncoded); 
System.out.println("Decoded value is " + new String(valueDecoded)); 
関連する問題