Base64 encryption

xiaoxiao2021-03-05  28

/ ** a standard base64 encoder / decoder. * @author s.weeden, njferrier * / public class base64 {/ ** decode a base 64 encoded string. *

string to byte conversion * This method uses a naive String to byte interpretation, it simply gets each * char of the String and calls it a byte. *

Since we should be dealing with Base64 encoded Strings that is a reasonable * assumption. *

end of data * we don't try to stop the conve h we find the "=" end of data padding char. * We Simply Add ZERO BYTES to THE Unencode Buffer. * / public static string decode (String Encode) {StringBuffer SB = new stringbuffer (); intumeturns; // Work Out How long to loop for. if (Encoded.Length ()% 3 == 0 ) @Turns = Encoded.Length (); else maxTurns = Encoded.Length () (3- (encoded.length () % 3)); // Tells US WHETER TO INCLUDE The Char in The Unencode Boolean Skip; // The Unencode Buffer Byte [] Unenc = New Byte [4]; BYTE B; For (INT i = 0, J = 0; I

'and' / 'IF (B> = ​​65 && B <91) Unenc [J] = (BYTE) (B-65); ELSE IF (B> = ​​97 && B <123) Unenc [J] = (Byte ) (B-71); ELSE IF (B> = ​​48 && B <58) Unenc [J] = (Byte) (B 4); ELSE IF (B == ' ') Unenc [J] = 62; Else if (b == '/') Unenc [j] = 63; // if we find "=" = "=" We're not realding with this now else if (b == '=') Unenc [j] = 0; Else {char C = (char) b; if (c == '/ n' || c == '/ r' || c == '|| c ==' / t ' ) Skip = true; else // could throw an exception he? It's input we don't understand. } // overce the array has boiled convert the bytes back INTO Chars if (! Skip && j == 4) {// shift the 6 bit bytes Into a single 4 octet word int res = (unenc [0] << 18) (Unenc [1] << 12) (Unenc [2] << 6) Unenc [3]; BYTE C; INT K = 16;

// shift each actens down to read it as char and add to stringbuffer while (k> = 0) {c = (byte) (RES >> K); if (c> 0) sb.append ((char) C) K- = 8;} // reset J and the unencode buffer j = 0; unenc [0] = 0; Unenc [1] = 0; Unenc [2] = 0; Unenc [3] = 0;}} return sb.toString ();} / ** encode plaintext data to a base 64 string * @param plain the text to convert If plain is longer than 76 characters this method * returns null (see RFC2045) * @return the encoded text.. (or Null if String Was Longer Than 76 Chars). * / Public Static String Encode (String PLA) IN) {if (plain.Length ()> 76) Return Null; intumeturns; stringbuffer sb = new stringbuffer (); // the encode buffer Byte [] ENC = new byte [3]; boolean end = false; for INT i = 0, J = 0;! End; i ) {char _ch = plain.charat (i); if (i == plain.length () - 1) end = true; ENC [J ] = (Byte) Plain.Charat (i); if (j ==

3 || End) {int res; // this is a bit inefficient at the end // Worth it for the skill decrease in code size? Res = (ENC [0] << 16) (ENC [1] < <8) ENC [2]; int b; int lowestbit = 18- (j * 6); for (int Toshift = 18; toshift> = lowestbit; toshift- = 6) {b = res >>> Toshift; b & = 63; if (b> = 0 && B <26) sb.append (b 65)); if (b> = 26 && b <52) sb.append ((char) (B 71) )); If (b> = 52 && b <62) sb.append ((char) (b-4)); IF (b == 62) sb.append (' '); if (b == 63) sb.append ('/'); if (sb.Length ()% 76 == 0) sb.append (' / n ');

} // Now set the endcha} {ix integral input (ie: less than 24 bits) {ix (j == 1) sb.append ("==") ; If (j == 2) sb.append ('=');} ENC [0] = 0; ENC [1] = 0; ENC [2] = 0; j = 0;}} return sb.toString }}

/ * The rc2045 conversion chart, 0 A 17 R 34 I 51Z 1 B 18 S 35 J 52 0 2 C 19 T 36 K 53 1 3 D 20 U 37 L 54 2 4 E 21 V 38 M 55 3 5 f 22 W 39 N 56 4 6 G 23 x 40 O 57 5 7 H 24 y 41 P 58 6 8 i 25 Z 42 Q 59 7 9 J 26 A 43 R 60 8 10 K 27 B 44 S 61 9 11 L 28 C 45 T 62 12 m 29 D 46 U 63/13 N 30 E 47 V 14 o 31 f 48 W (pad) = 15 p 32 g 49 x 16 q 33 h 50 Yascii Chart

Dec HEX Char Dec HEX Char Dec HEX Char Dec HEX Char ---------------------------------- --- --------- 0 0 NUL 32 20 64 40 @ 96 60 `1 1 SOH 33 21! 65 41 A 97 61 A 2 2 STX 34 22" 66 42 B 98 62 B 3 3 ETX 35 23 # 67 43 C 99 63 C 44 EOT 36 24 $ 68 44 D 100 64 D 5 5 ENQ 37 25% 69 45 E 101 65 E 6 6 ACK 38 26 & 70 46 F 102 66 F 7 7 Bel 39 27 '71 47 G 103 67 G 8 8 BS 40 28 (72 48 H 104 68 H 9 9 TAB 41 29) 73 49 I 105 69 I 10 A LF 42 2A * 74 4A J 106 6A J 11 B VT 43 2B

转载请注明原文地址:https://www.9cbs.com/read-36365.html

New Post(0)