当前位置: 技术问答>java相关
关于BASE64 编码解密的问题,高分求解
来源: 互联网 发布时间:2015-06-27
本文导语: 在下想做一个上传文件的程序。 从 java.io.InputStream 输入流中读取数据,但此时的数据是BASE64编码格式的,如何能够将输入流中的数据解密呢? 我也找过一些BASE64的类,但都无法从输入流中转化数据。 请指教。谢谢...
在下想做一个上传文件的程序。
从 java.io.InputStream 输入流中读取数据,但此时的数据是BASE64编码格式的,如何能够将输入流中的数据解密呢?
我也找过一些BASE64的类,但都无法从输入流中转化数据。
请指教。谢谢。
从 java.io.InputStream 输入流中读取数据,但此时的数据是BASE64编码格式的,如何能够将输入流中的数据解密呢?
我也找过一些BASE64的类,但都无法从输入流中转化数据。
请指教。谢谢。
|
import java.io.*;
import java.util.*;
/**
* This class implements a FilterInputStream that reads base64-encoded
* data (as defined in RFC1521) from another InputStream and decodes
* it while reading.
*/
public class Base64InputStream extends FilterInputStream {
private int surplus;
private int decoded;
private int lastRead = 0;
/**
* The constructor for the decoding input stream.
*
* @param in the underlying input stream
*/
public Base64InputStream (InputStream in) {
super (in);
decoded = 0;
}
/**
* This method always return 0, as we really can't be sure about the
* number of decoded bytes available without doing the decoding. Imaging
* an underlying input stream from which several whitespace characters
* can still be read. Thus calling available() on the underlying stream
* returns a number greater than zero. But whitespace decodes to nothing,
* so we can't derive a reliable number for available from he underlying
* stream.
*/
public int available () throws IOException {
return 0;
}
/**
* Always returns false, since marking is not supported.
*/
public boolean markSupported () {
return false;
}
/**
* Reads a single byte. Returns -1 if the input stream is exhausted.
*/
public int read () throws IOException {
byte b[] = new byte[1];
int res = read (b);
if (res 0 && lastRead >= 0) {
int b = decodeByte ();
if (b = 3 && lastRead >= 0) {
int grp0 = 0, grp1 = 0, grp2 = 0, grp3 = 0;
int gotCnt = 0;
while (gotCnt 0 && lastRead >= 0) {
int b = decodeByte ();
if (b 0 && pos == off)
return -1;
return pos - off;
}
private int decodeByte () throws IOException {
int b;
switch (decoded) {
case 0:
do {
lastRead = in.read ();
if (lastRead 0 && pos 2) & 0x3f)];
encBuf[1] = (byte)charCodes[((b0 & 0x3) 4) & 0xf)];
encBuf[2] = (byte)charCodes[((b1 & 0xf) 6) & 0x3)];
encBuf[3] = (byte)charCodes[b2 & 0x3f];
out.write (encBuf);
encoded = 0;
col += 4;
if (col >= 76) {
out.write ('n');
col = 0;
}
pos += 3;
}
// encode rest
while (pos > 2) & 0x3f];
surplus = ((b & 0x3) 4) & 0xf)];
surplus = ((b & 0xf) > 6) & 0x3)];
encBuf[3] = (byte)charCodes[b & 0x3f];
out.write (encBuf);
encoded = 0;
col += 4;
if (col >= 76) {
out.write ('n');
col = 0;
}
break;
}
}
/**
* This method is to write all buffered data to its destination.
* With a base64 encoding there arises a problem: if an encoding character
* isn't completly known yet because its value depends partially
* on the last byte written and partially on the next byte to be
* written, then flush can't write it without assuming that there
* will be no more bytes coming.
*
* The behaviour for flush can therefore
* be controlled by the flag finishOnFlush when the stream is created.
* If the flag is set, flush will assume that no more data is to be
* written after flush has been called, thus effectivly finishing the
* stream of encoded data. This behaviour is especially useful, when
* the Base64OutputStream is created with an underlying output stream
* that contains mixed
* content. In this case, we cannot call close on the Base64OutputStream
* because this would make the underlying OutputStream unusable as well.
*
* @see org.tsx.mnl.io.base64.Base64OutputStream
* @see java.io.OutputStream#flush
*/
public void flush () throws IOException {
if (finishOnFlush)
doFlush ();
}
private void doFlush () throws IOException {
switch (encoded) {
case 1:
encBuf[2] = (byte)'=';
case 2:
encBuf[3] = (byte)'=';
out.write (encBuf);
col += 4;
break;
}
if (col > 0) {
out.write ('n');
col = 0;
}
}
/**
* This method finishes the encoded stream and calls close on the
* underlying output stream.
*/
public void close () throws IOException {
doFlush ();
out.close ();
}
}
import java.util.*;
/**
* This class implements a FilterInputStream that reads base64-encoded
* data (as defined in RFC1521) from another InputStream and decodes
* it while reading.
*/
public class Base64InputStream extends FilterInputStream {
private int surplus;
private int decoded;
private int lastRead = 0;
/**
* The constructor for the decoding input stream.
*
* @param in the underlying input stream
*/
public Base64InputStream (InputStream in) {
super (in);
decoded = 0;
}
/**
* This method always return 0, as we really can't be sure about the
* number of decoded bytes available without doing the decoding. Imaging
* an underlying input stream from which several whitespace characters
* can still be read. Thus calling available() on the underlying stream
* returns a number greater than zero. But whitespace decodes to nothing,
* so we can't derive a reliable number for available from he underlying
* stream.
*/
public int available () throws IOException {
return 0;
}
/**
* Always returns false, since marking is not supported.
*/
public boolean markSupported () {
return false;
}
/**
* Reads a single byte. Returns -1 if the input stream is exhausted.
*/
public int read () throws IOException {
byte b[] = new byte[1];
int res = read (b);
if (res 0 && lastRead >= 0) {
int b = decodeByte ();
if (b = 3 && lastRead >= 0) {
int grp0 = 0, grp1 = 0, grp2 = 0, grp3 = 0;
int gotCnt = 0;
while (gotCnt 0 && lastRead >= 0) {
int b = decodeByte ();
if (b 0 && pos == off)
return -1;
return pos - off;
}
private int decodeByte () throws IOException {
int b;
switch (decoded) {
case 0:
do {
lastRead = in.read ();
if (lastRead 0 && pos 2) & 0x3f)];
encBuf[1] = (byte)charCodes[((b0 & 0x3) 4) & 0xf)];
encBuf[2] = (byte)charCodes[((b1 & 0xf) 6) & 0x3)];
encBuf[3] = (byte)charCodes[b2 & 0x3f];
out.write (encBuf);
encoded = 0;
col += 4;
if (col >= 76) {
out.write ('n');
col = 0;
}
pos += 3;
}
// encode rest
while (pos > 2) & 0x3f];
surplus = ((b & 0x3) 4) & 0xf)];
surplus = ((b & 0xf) > 6) & 0x3)];
encBuf[3] = (byte)charCodes[b & 0x3f];
out.write (encBuf);
encoded = 0;
col += 4;
if (col >= 76) {
out.write ('n');
col = 0;
}
break;
}
}
/**
* This method is to write all buffered data to its destination.
* With a base64 encoding there arises a problem: if an encoding character
* isn't completly known yet because its value depends partially
* on the last byte written and partially on the next byte to be
* written, then flush can't write it without assuming that there
* will be no more bytes coming.
*
* The behaviour for flush can therefore
* be controlled by the flag finishOnFlush when the stream is created.
* If the flag is set, flush will assume that no more data is to be
* written after flush has been called, thus effectivly finishing the
* stream of encoded data. This behaviour is especially useful, when
* the Base64OutputStream is created with an underlying output stream
* that contains mixed
* content. In this case, we cannot call close on the Base64OutputStream
* because this would make the underlying OutputStream unusable as well.
*
* @see org.tsx.mnl.io.base64.Base64OutputStream
* @see java.io.OutputStream#flush
*/
public void flush () throws IOException {
if (finishOnFlush)
doFlush ();
}
private void doFlush () throws IOException {
switch (encoded) {
case 1:
encBuf[2] = (byte)'=';
case 2:
encBuf[3] = (byte)'=';
out.write (encBuf);
col += 4;
break;
}
if (col > 0) {
out.write ('n');
col = 0;
}
}
/**
* This method finishes the encoded stream and calls close on the
* underlying output stream.
*/
public void close () throws IOException {
doFlush ();
out.close ();
}
}
|
这是用法:
import java.io.*;
class Test {
public static void main(String[] args) {
try {
Base64OutputStream enc
= new Base64OutputStream (System.out, false);
OutputStreamWriter out = new OutputStreamWriter (enc);
String s = "Hello world!nn";
s = s + "While normally the short message above is sufficientn";
s = s + "for a test, we need more text in order to get anothern";
s = s + "line of output.n";
out.write (s, 0, s.length());
enc.setFinishOnFlush (true);
out.flush ();
FileInputStream data = new FileInputStream ("data.base64");
InputStream dec = new Base64InputStream (data);
System.out.print ((char)dec.read ());
byte[] b = new byte[16];
int cnt;
while ((cnt = dec.read (b)) >= 0)
System.out.print (new String (b, 0, cnt));
System.out.print ("Besides the base64 data, input contains: ");
while ((cnt = data.read ()) >= 0)
System.out.print ((char)cnt);
} catch (Exception e) {
System.out.println ("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
import java.io.*;
class Test {
public static void main(String[] args) {
try {
Base64OutputStream enc
= new Base64OutputStream (System.out, false);
OutputStreamWriter out = new OutputStreamWriter (enc);
String s = "Hello world!nn";
s = s + "While normally the short message above is sufficientn";
s = s + "for a test, we need more text in order to get anothern";
s = s + "line of output.n";
out.write (s, 0, s.length());
enc.setFinishOnFlush (true);
out.flush ();
FileInputStream data = new FileInputStream ("data.base64");
InputStream dec = new Base64InputStream (data);
System.out.print ((char)dec.read ());
byte[] b = new byte[16];
int cnt;
while ((cnt = dec.read (b)) >= 0)
System.out.print (new String (b, 0, cnt));
System.out.print ("Besides the base64 data, input contains: ");
while ((cnt = data.read ()) >= 0)
System.out.print ((char)cnt);
} catch (Exception e) {
System.out.println ("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。