为了账号安全,请及时绑定邮箱和手机立即绑定
首页 手记 java实现Base64~~~~继续加油!!!

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

2017.01.19 20:04 2275浏览

跟着老师一起敲打的,感觉还是很神奇哦!!!
注意啦: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 class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="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));
    }

}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
手记
粉丝
2
获赞与收藏
98

关注TA,一起探索更多经验知识

同主题相似文章浏览排行榜

风间影月说签约讲师

51篇手记,涉及Java、MySQL、Redis、Spring等方向

进入讨论

Tony Bai 说签约讲师

152篇手记,涉及Go、C、Java、Python等方向

进入讨论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消