base64解密java
Ⅰ java base64解密乱码问题 代码如下:
private String getPictureString() {
String upload = "";
try {
FileInputStream in = new FileInputStream(fileName);
byte[] ba = new byte[in.available()];
in.read(ba);
in.close();
upload = new String(android.util.Base64.encode(ba,
android.util.Base64.DEFAULT));
} catch (FileNotFoundException e) {
LogUtil.e(e.getMessage(), e);
} catch (IOException e) {
LogUtil.e(e.getMessage(), e);
}
return upload;
}
这个是加密
解密就是
encode换成decode
upload=newString(android.util.Base64.decode(ba,
android.util.Base64.DEFAULT));
Ⅱ 关于java中BASE64解码算法
让我们再来看一个实际的例子,加深印象!
转换前 10101101 10111010 01110110
转换后 00101011 00011011 00101001 00110110
十进制 43 27 41 54
对应码表中的值 r b p 2
将第一个字符右移2位得00101011, 得第一个目标字符00101011
将第一个字符左移4位得11010000,第二个字符右移4位的00001011相加得第二个目标字符11011011
将第二个字符左移2位得11101000,第三个字符右移6位的00000001相加的第三个目标字符11101001
第四个目标字符就是01110110
然后让各个目标字符与0x3F进行and位操作,让最高的两位为零。
Ⅲ Java中用Base64编程的文件批量加密解密工具程序代码
/** * BASE64解密 * * @param key * @return * @throws Exception */
public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } /** * BASE64加密 * * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); }
Ⅳ java中如何用base64解码图片,并返回图片,不保存。
给你发个我以前的工具类吧、
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ImageChange {
/**
* 从path这个地址获取一张图片然后转为base64码
* @param imgName 图片的名字 如:123.gif(是带后缀的)
* @param path 123.gif图片存放的路径
* @return
* @throws Exception
*/
public static String getImageFromServer(String imgName,String path)throws Exception{
BASE64Encoder encoder = new sun.misc.BASE64Encoder();
File f = new File(path+imgName);
if(!f.exists()){
f.createNewFile();
}
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "gif", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
}
/**
* 将一个base64转换成图片保存在 path 文件夹下 名为imgName.gif
* @param base64String
* @param path 是一个文件夹路径
* @param imgName 图片名字(没有后缀)
* @throws Exception
*/
public static String savePictoServer(String base64String,String path,String imgName)throws Exception{
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream s = new ByteArrayInputStream(bytes1);
BufferedImage bi1 =ImageIO.read(s);
Date timeCur = new Date();
SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");
SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
String strYY = fmtYY.format(timeCur);
String strMM = fmtMM.format(timeCur);
String strDD = fmtDD.format(timeCur);
String realPath = path+"/"+strYY+"/"+strMM+"/"+strDD;
File dir=new File(realPath);
if(!dir.exists()){
dir.mkdirs();
}
String fileName=path+"\\"+strYY+"\\"+strMM+"\\"+strDD +"\\"+imgName+".gif";
File w2 = new File(fileName);//可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动
return fileName;
}
public static void main(String[] args) throws Exception {
System.out.println(getImageFromServer("001001.gif","d:"));
}
}
Ⅳ java:关于base64编码求解
首先base64是对字节码进行再次编码,所以要先把你这个字符转成二进行码(不过对于英文字母来说,转成的二进制码都是一样的,就没关系了)。'a'对应的二进制码为01000001(65),而base64是每6位编成一个字符(base64字符),则把此二进制码分成2段,第一段是010000,第二段是01(0000不足位补0),这两个不是一样的吗,值为16,对应第17个大写字母(0对应A),那自然是两个Q了,而base64编码至少编出4个字符(因为给3个字符来编码,一个6位,3个24位,6位一编码,正好编出4个,反过来自然是4个解码成3个)。另外这个编码过程本来就是它的原理,原理图如下,只要明白了原理才知道倒底怎么编。
Ⅵ 在Java中如何进行BASE64编码和解码
importsun.misc.BASE64Encoder;
importsun.misc.BASE64Decoder;
//将s进行BASE64编码
publicstaticStringgetBASE64(Strings){
if(s==null)returnnull;
return(newsun.misc.BASE64Encoder()).encode(s.getBytes());
}
//将BASE64编码的字符串s进行解码
(Strings){
if(s==null)returnnull;
BASE64Decoderdecoder=newBASE64Decoder();
try{
byte[]b=decoder.decodeBuffer(s);
returnnewString(b);
}catch(Exceptione){
returnnull;
}
}