Base64S

# Algorithm

Encrypting

1. Calculate the difference in length of byte array len(key) - len(src). If the result is a negative value, it is treated as 0.
2. XOR the source and key byte arrays in order and stores into a newly-allocated byte array.
3. Stores the result of (1) in the last byte.
4. Encode the byte array of (3) into a newly-allocated byte array using the Base64 encoding scheme.

Decrypting

Manipulate in the opposite way.

# Examples

Normal Base64

Source Text (UTF-8) a b c
Source octets 97 (0x61) 98 (0x62) 99 (0x63)
Bit pattern 0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 0 1 1
Index 24 22 9 35
Base64-encoded Y W J j
Encoded octets 89 (0x59)
01011001
87 (0x57)
01010111
74 (0x4A)
01001010
106 (0x6A)
01101010

Base64S : src = abc / key = xyz (src.length = key.length)

Source Text (UTF-8) a b c
Octets 97 (0x61) 98 (0x62) 99 (0x63)
Bit pattern 0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 0 1 1
Key Text (UTF-8) x y z
Octets 120 (0x78)121 (0x79)122 (0x7A)
Bit pattern 0 1 1 1 1 0 0 0 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 0
Bitwise op.   97 XOR 120 = 25 97 XOR 121 = 24 99 XOR 122 = 25 key.len - src.len
Bit pattern 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0                        
Output Index 6 17 44 25 0 0 Padding Padding
Base64-encoded G R s Z A A = =
Encoded octets 71 (0x47)
01000111
82 (0x52)
01010010
115 (0x73)
01110011S
90 (0x5A)
01011010
65 (0x41)
01000001
65 (0x41)
01000001
61 (0x3D)
00111101
61 (0x3D)
00111101

Base64S : src = abcde / key = xyz (src.length > key.length)

Source Text (UTF-8) a b c d e
Octets 97 (0x61) 98 (0x62) 99 (0x63) 100 (0x64) 101 (0x65)
Bit pattern 0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 0 1 1 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0 1
Key Text (UTF-8) x y z x y
Octets 120 (0x78) 121 (0x79) 122 (0x7A) 120 (0x78) 121 (0x79)
Bit pattern 0 1 1 1 1 0 0 0 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 1 1
Bitwise op.   97 XOR 120 = 25 97 XOR 121 = 24 99 XOR 122 = 25 100 XOR 120 = 28 101 XOR 121 = 28 key.len - src.len
Bit pattern 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0
Output Index 6 17 44 25 7 1 48 0
Base64-encoded G R s Z H B w A
Encoded octets 71 (0x47)
01000111
82 (0x52)
01010010
115 (0x73)
01110011
90 (0x5A)
01011010
72 (0x48)
01001000
66 (0x42)
01000010
119 (0x77)
01110111
65 (0x41)
01000001

Base64S : src = ab / key = xyz (src.length < key.length)

Source Text (UTF-8) a b Padding bits
Octets 97 (0x61) 98 (0x62)
Bit pattern 0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1
Key Text (UTF-8) x y z
Octets 120 (0x78)121 (0x79)122 (0x7A)
Bit pattern 0 1 1 1 1 0 0 0 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 0
Bitwise op.   97 XOR 120 = 25 97 XOR 121 = 24 255 XOR 122 = 133 len(key) - len(src)
Bit pattern 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0                        
Output Index 6 17 46 5 0 16 Padding Padding
Base64-encoded G R u F A Q = =
Encoded octets 71 (0x47)
01000111
82 (0x52)
01010010
117 (0x75)
01000110
70 (0x46)
01000110
65 (0x41)
01000001
81 (0x51)
01010001
61 (0x3D)
00111101
61 (0x3D)
00111101

# Implementation

Java, JavaScript, Python, PowerShell, VBA implementations are available on GitHub.

Apache Maven

JAR direct download
base64s-1.1.1.jar (4,050 bytes / Compiled with JDK8)
SHA-1: 335e423f284cda1ba95cdb9a860c28776708e001
SHA-256: 306cdf48b74afa30462eecd6abc84f6b0c6625a6aa1fa8cbc0d224f24dc4cd6d

# Notice

Base64S is not intended to be used where secrecy is of any concern.

x
The Base 64 Alphabet IDX BIN ENC IDX BIN ENC IDX BIN ENC IDX BIN ENC 0 000000 A 16 010000 Q 32 100000 g 48 110000 w 1 000001 B 17 010001 R 33 100001 h 49 110001 x 2 000010 C 18 010010 S 34 100010 i 50 110010 y 3 000011 D 19 010011 T 35 100011 j 51 110011 z 4 000100 E 20 010100 U 36 100100 k 52 110100 0 5 000101 F 21 010101 V 37 100101 l 53 110101 1 6 000110 G 22 010110 W 38 100110 m 54 110110 2 7 000111 H 23 010111 X 39 100111 n 55 110111 3 8 001000 I 24 011000 Y 40 101000 o 56 111000 4 9 001001 J 25 011001 Z 41 101001 p 57 111001 5 10 001010 K 26 011010 a 42 101010 q 58 111010 6 11 001011 L 27 011011 b 43 101011 r 59 111011 7 12 001100 M 28 011100 c 44 101100 s 60 111100 8 13 001101 N 29 011101 d 45 101101 t 61 111101 9 14 001110 O 30 011110 e 46 101110 u 62 111110 + 15 001111 P 31 011111 f 47 101111 v 63 111111 / (pad) =