继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java实现Base64~~~~继续加油!!!

qq_Sunshine暖阳_0
关注TA
已关注
手记 11
粉丝 2
获赞 98

跟着老师一起敲打的,感觉还是很神奇哦!!!
注意啦:sun.misc.BASE64Encoder找不到jar包的解决方法
1.右键项目-》属性-》Java bulid path-》jre System Library-》access rules-》resolution选择accessible,下面填上** 点击确定即可!!!
2.
在MyEclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示:
Access restriction: The type BASE64Decoder is not accessible due to restriction on required library C:\Program
files\java\jre6\lib\rt.jar
Access restriction : The constructor BASE64Decoder() is not accessible due to restriction on required library C:\Program files\java\jre6\lib\rt.jar

解决方案1(推荐):
只需要在project build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
解决方案2:
Windows -> Preferences -> Java -> Compiler -> Errors/Warnings ->
Deprecated and trstricted API -> Forbidden reference (access rules): -> change to warning

import java.io.IOException;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jcajce.provider.symmetric.ARC4.Base;
import org.bouncycastle.util.encoders.Base64Encoder;
import sun.security.*;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
public class loveBase64 {
    private static String src="immoc security base64";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //jdkbase64();
        //commonsCodesBase64();
        bouncyCastleBase64();
    }
    public static void jdkbase64(){

        try {
            BASE64Encoder encoder=new BASE64Encoder(); 
        String encode=encoder.encode(src.getBytes());
        System.out.println("encode:"+encode);
        BASE64Decoder decoder=new BASE64Decoder(); 
        System.out.println("decode:"+new String(decoder.decodeBuffer(encode)));

        } catch (IOException e) {
            // TODO: handle exception
        }
    }

    public static void commonsCodesBase64(){
        byte[] encodeBytes=Base64.encodeBase64(src.getBytes());
        System.out.println("encode:"+new String(encodeBytes));
        byte[] decodeBytes=Base64.decodeBase64(encodeBytes);
        System.out.println("decode:"+new String(decodeBytes));
    }

    public static void bouncyCastleBase64(){
       byte[] encodeBytes=org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
       System.out.println("encode:"+new String(encodeBytes));
       byte[] decodeBytes=org.bouncycastle.util.encoders.Base64.decode(encodeBytes);
       System.out.println("decode:"+new String(decodeBytes));
    }

}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP