将字符串从客户端传输到服务器时的垃圾值

时间:2012-04-14 07:21:36

标签: java xml base64

这是我在客户端的字符串:

<?xml version="1.0" encoding="UTF-8"?><samlp
:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" AssertionConsum
erServiceURL="http://172.16.32.160:8080/ration/consumer.jsp" ID="3dece98b-f89d-4
b59-b7ed-5f278aa56eb0" IssueInstant="2012-04-14T12:47:36" ProtocolBinding="urn:o
asis:names:tc:SAML:2.0:bindings:HTTP-POST" Version="2.0" destination="http://172
.16.32.160:8080/ration/EFORMS/service1level1.html" level="level1"><saml:Issuer x
mlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">http://172.16.32.160:8080/rati
on/SProvider.jsp</saml:Issuer><samlp:NameIDPolicy AllowCreate="true" Format="urn
:oasis:names:tc:SAML:2.0:nameid-format:unspecified"/><samlp:RequestedAuthnContex
t Comparison="exact"/><saml:AuthnContextClassRef xmlns:saml="urn:oasis:names:tc:
SAML:2.0:assertion">urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTran
sport</saml:AuthnContextClassRef><Signature xmlns="http://www.w3.org/2000/09/xml
dsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/
xml-exc-c14n#"/><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rs
a-sha1"/><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2
000/09/xmldsig#enveloped-signature"/></Transforms><DigestMethod Algorithm="http:
//www.w3.org/2001/04/xmlenc#sha256"/><DigestValue>ZrvAtwVXohf0JrjGzWlBt9gt9SNESE
Id6lZuU4iOZkg=</DigestValue></Reference></SignedInfo><SignatureValue>M62OPXOZ8nR
zIDjFcTgBTnYSzZdrjwHL6p93wbpSYHZgBOUD4EZgt47GhtFAVU3oVga5VLt5yK+j
hBvEVtaj2/bKIvFfXDpnOuXKNlsKtdu140iMUMdxRvnMyh8enx77YhUlSl2VCQ0NnYnk3gs4H8QE
qmeorwXsthtjUMTKqAw=</SignatureValue></Signature></samlp:AuthnRequest>


我用傻瓜代码对其进行了编码:

byte[] q3=Base64.encodeBase64(xmlString.getBytes("utf-8"));
        String xmlString1=new String(q3,"utf-8");

在服务器中,我首先使用以下内容进行解码:

byte[] decoded = Base64.decodeBase64(result.getBytes("utf-8"));
                String result10=new String(decoded,"utf-8");


结果包含以下内容:

<?xml version="1.0" encoding="UTF-8"?><samlp:
AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" AssertionConsume
rServiceURL="http://172.16.32.160:8080/ration/consumer.jsp" ID="3dece98b-f89d-4b
59-b7ed-5f278aa56eb0" IssueInstant="2012-04-14T12:47:36" ProtocolBinding="urn:oa
sis:names:tc:SAML:2.0:bindings:HTTP-POST" Version="2.0" destination="http://172.
16.32.160:8080/ration/EFORMS/service1level1.html" level="level1"><saml:Issuer xm
lns:saml="urn:oasis:names:tc:SAML:2.0:assertion">http://172.16.32.160:8080/ratio
n/SProvider.jsp</saml:Issuer><samlp:NameIDPolicy AllowCreate="true" Format="urn:
oasis:names:tc:SAML:2.0:nameid-format:unspecified"/☼∟?[[∟♫??\]Y\?↓Y►]]→←???↓►??\
↑\?\??H?^↑X??∟?[[♫?]]→←???↓^↔►?↑\??Y?▲←[←???[[☼H?\????\?\??[Y\?↑??SS♫???♫?\??\?→
[???\????\?\??[Y\?↑??SS♫???♫?X??↑\??\?↑\????¶∟??↓X?↓Y§∟?[??←?☼♂??[[♫?]]→←???↓^↔►
?↑\??Y??¶?Y?]↔\?H▲←[←??H?↔↔∟♫???????????♀♀♂?♫K?←[↓∟?Y???¶?Y?Y↕[???►?[??X?[→^?]→[
?Y]→►[↓??]→←OH?↔↔∟♫???????????♀♀K?L♂?←[♂Y^↑?X?M←????6?v?↨GW&T?WF??B♦▬?v?&?F???&?
GG♥???wwr?s2??&r?#♥♥☻?♥?????G6?r7'6↕?6?‼↕"???&VfW&V?6R♣U$??"#??G&▬?6f?&?3??G&▬?6
f?&?♦▬?v?&?F???&?GG♥???wwr?s2??&r?#♥♥☻?♥?????G6?r6V?fV??♠VB?6?v?↨GW&R"????G&▬?6f
?&?3??F?vW7D?WF??B♦▬?v?&?F???&?GG♥???wwr?s2??&r?#♥♥↕?♥B????V?276?‼#Sb"??◄?????Y?
???i??♣??Ya????)?↔?]?   ????M9§M§%??i?T?=i????◄?????Y?????I?????????M?????%???<S
ignatureValue>M62OPXOZ8nRzIDjFcTgBTnYSzZdrjwHL6p93wbpSYHZgBOUD4EZgt47GhtFAVU3oVg
a5VLt5yK+j
hBvEVtaj2/bKIvFfXDpnOuXKNlsKtdu140iMUMdxRvnMyh8enx77YhUlSl2VCQ0NnYnk3gs4H8QE
qmeorwXsthtjUMTKqAw=</SignatureValue></Signature></samlp:AuthnRequest><br>


正如你所看到的,你在中间部分插入了垃圾值。但是文档的其余部分没有任何字符。为什么这部分单独有垃圾价值? 的修改
我使用以下代码发送xmlString。

String reqString = "http://172.16.32.160:8080/saml/IDProvider"+"?SAMLRequest=" + xmlString1;

        resp.sendRedirect(reqString);


在我使用的服务器中

String result=req.getParameter("SAMLRequest");
                byte[] decoded = Base64.decodeBase64(result.getBytes("utf-8"));
                String result10=new String(decoded,"utf-8");

2 个答案:

答案 0 :(得分:2)

原则上它是正确的;你也可以做(如你所知):

发送:

byte[] q3=Base64.encodeBase64(xmlString.getBytes("utf-8"));
String xmlString1=new String(q3,"US-ASCII");

接收:

byte[] decoded = Base64.decodeBase64(result.getBytes("US-ASCII"));
String result10=new String(decoded,"utf-8");

我认为这是转移:Base64给出了A-z0-9和+以及/。你可能需要替换+和/。此外,可能会生成空格/换行符,也必须进行处理。在转移期间,空间可能变为+。

不太可能的错误是将标题中的内容长度设置为原始大小(6/8)而不是编码长度。

如果查看编码结果,您可能会自己看到它。编码到正确的内容+一个字符。

答案 1 :(得分:0)

您遗漏了一个重要部分:您向服务器发送xmlString1的代码,以及服务器构成result字符串的位置。

这可能是问题所在。当然,该代码将决定您的编码和解码是否合适/正确。