base64で暗号化と復号化ツール:
輸入java.io.ByteArrayOutputStreamを; パブリック クラスBase64Util { プライベート 静的 文字 [] base64EncodeChars = 新しい 文字 [] { 'A'、 'B'、 'C'、 'D' 、 'E'、 'F'、 'G'、 'H'、「I 」、 'J'、 'K'、 'L'、 'M'、 'N'、 'O'、 'P'、 'Q' 、 R ''、 'S'、 'T'、 'U'、 'V'、 'W'、 'X'、 'Y'、 'Z'、 ''、 'B'、 'C'、 'D' 、 'E'、 'F'、 'G'、「H 」、 'I'、 'J'、 'K'、 'L'、 'M'、 'N'、 'O'、 'P'、 'Q'、 'R'、 'S'、 'T'、 'U'、 'V'、 'W'、 'X'、 'Y'、 'Z'、 '0'、 '1'、 '2'、 ' 3' 、 '4'、 '5'、 '6'、 '7'、 '8'、 '9'、 '+'、 '/' 、}。 プライベート 静的 バイト [] base64DecodeChars = 新しい バイト [] {-1、-1、-1、-1、-1 、 -1、-1、-1、-1、-1、-1、-1、-1 、-1、-1、-1、-1、-1、-1、-1、-1、-1 、 -1、-1、-1、-1、-1、-1、-1、 - 1、-1、-1、-1、-1、-1、-1、-1、-1、-1 、 -1、-1、-1、-1、62、-1、-1、 - 1、63、52、53、54、55、56、57、58、59 、 60、61、-1、-1、-1、-1、-1、-1、-1、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、-1 、 -1、-1、-1、-1、-1、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、-1、-1、-1 、 -1、-1 }。 / ** *暗号化 * * @paramのデータ * @return * / パブリック 静的文字エンコード(バイト[]データ){ StringBufferのSB = 新しいStringBufferを(); int型のlen = data.length。 int型私= 0 ; int型B1、B2、B3; 一方、(I < {LEN) B1 =データ[I ++]&0xffで、 もし(I == LEN){ sb.append(base64EncodeChars [B1 >>> 2 ])。 sb.append(base64EncodeChars [(B1&を0x3)<< 4 ])。 sb.append( "==" ); 休憩; } B2 =データ[I ++]&0xffで、 もし(I == LEN){ sb.append(base64EncodeChars [B1 >>> 2 ])。 sb.append(base64EncodeChars [((B1&0×03)<< 4 ) |((B2&0xF0が)>>> 4 )])。 sb.append(base64EncodeChars [(B2&0x0Fの)<< 2 ])。 sb.append( "=" )。 休憩; } B3 =データ[I ++]&0xffで、 sb.append(base64EncodeChars [B1 >>> 2 ])。 sb.append(base64EncodeChars [((B1&0×03)<< 4 ) |((B2&0xF0が)>>> 4 )])。 sb.append(base64EncodeChars [((B2&0x0Fの)<< 2 ) |((B3&0xc0から)>>> 6 )])。 sb.append(base64EncodeChars [B3&0x3fを])。 } リターンsb.toString(); } / ** *復号化 * * @Param サイクル * @return * / パブリック 静的 バイト[]デコード(文字列str){ バイト []データ= str.getBytes()。 int型のlen = data.length。 ByteArrayOutputStream BUF = 新しいByteArrayOutputStream(LEN); int型私= 0 ; int型B1、B2、B3、B4。 一方、(I < LEN){ 行います{ B1 = base64DecodeChars [データ[I ++ ]。 } 一方、(I <LEN && B1 == -1 )。 もし(B1 == -1 ){ ブレーク。 } やります{ B2 = base64DecodeChars [データ[I ++ ]。 } 一方、(I <LEN && B2 == -1 )。 もし(B2 == -1 ){ ブレーク。 } buf.write((int型)((B1 << 2)|((B2&0x30から)>>> 4 ))); やります{ B3 =データ[I ++ ]。 場合(B3 == 61 ){ 戻り)(buf.toByteArrayします。 } B3 = base64DecodeChars [B3]。 } 一方、(I <LEN && B3 == -1 )。 もし(B3 == -1 ){ ブレーク。 } buf.write((int型)(((B2&0x0Fの)<< 4)|((B3&0x3cの)>>> 2 ))); やります{ B4 =データ[I ++ ]。 場合(B4 == 61 ){ 戻り)(buf.toByteArrayします。 } B4 = base64DecodeChars [B4]。 } 一方、(I <LEN && B4 == -1 )。 もし(B4 == -1 ){ ブレーク。 } ((buf.write int型)(((B3&0x03の)<< 6)| B4を)); } リターンbuf.toByteArray(); }
}