Código: Seleccionar todo
import java.util.Random;
public class BASE64Cipher {
Random rand = new Random();
private static final String base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static final int splitLinesAt = 76;
public static byte[] zeroPad(int length, byte[] bytes) {
byte[] padded = new byte[length];
System.arraycopy(bytes, 0, padded, 0, bytes.length);
return padded;
}
public static String generateString(Random rng, String characters, int length)
{
char[] text = new char[length];
for (int i = 0; i < length; i++)
{
text[i] = characters.charAt(rng.nextInt(characters.length()));
}
return new String(text);
}
public static String splitLines(String string) {
String lines = "";
for (int i = 0; i < string.length(); i += splitLinesAt) {
lines += string.substring(i, Math.min(string.length(), i + splitLinesAt));
lines += "\r\n";
}
return lines;
}
public static String decrypt64(String hash)
{
String d = new String(decode(hash));
return d;
}
public static byte[] decode(String string)
{
if (string == null) return null;
byte data[] = string.getBytes();
return decode(data);
}
public final static byte[] decode(byte[] data)
{
int tail = data.length;
while (data[tail-1] == '=') tail--;
byte dest[] = new byte[tail - data.length/4];
for (int idx = 0; idx <data.length; idx++)
{
if (data[idx] == '=') data[idx] = 0;
else if (data[idx] == '/') data[idx] = 63;
else if (data[idx] == '+') data[idx] = 62;
else if (data[idx] >= '0' && data[idx] <= '9')
data[idx] = (byte)(data[idx] - ('0' - 52));
else if (data[idx] >= 'a' && data[idx] <= 'z')
data[idx] = (byte)(data[idx] - ('a' - 26));
else if (data[idx] >= 'A' && data[idx] <= 'Z')
data[idx] = (byte)(data[idx] - 'A');
}
int sidx, didx;
for (sidx = 0, didx=0; didx < dest.length-2; sidx += 4, didx += 3)
{
dest[didx] = (byte) ( ((data[sidx] << 2) & 255) |
((data[sidx+1] >>> 4) & 3) );
dest[didx+1] = (byte) ( ((data[sidx+1] << 4) & 255) |
((data[sidx+2] >>> 2) & 017) );
dest[didx+2] = (byte) ( ((data[sidx+2] << 6) & 255) |
(data[sidx+3] & 077) );
}
if (didx < dest.length)
{
dest[didx] = (byte) ( ((data[sidx] << 2) & 255) |
((data[sidx+1] >>> 4) & 3) );
}
if (++didx < dest.length)
{
dest[didx] = (byte) ( ((data[sidx+1] << 4) & 255) |
((data[sidx+2] >>> 2) & 017) );
}
return dest;
}
public static String encode64(String string) {
String encoded = "";
byte[] stringArray;
try {
stringArray = string.getBytes("UTF-8");
} catch (Exception ignored) {
stringArray = string.getBytes();
}
int paddingCount = (3 - (stringArray.length % 3)) % 3;
stringArray = zeroPad(stringArray.length + paddingCount, stringArray);
for (int i = 0; i < stringArray.length; i += 3) {
int j = ((stringArray[i] & 0xff) << 16) +
((stringArray[i + 1] & 0xff) << 8) +
(stringArray[i + 2] & 0xff);
encoded = encoded + base64code.charAt((j >> 18) & 0x3f) +
base64code.charAt((j >> 12) & 0x3f) +
base64code.charAt((j >> 6) & 0x3f) +
base64code.charAt(j & 0x3f);
}
return splitLines(encoded.substring(0, encoded.length() -
paddingCount) + "==".substring(0, paddingCount));
}
}