为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Java

跟着老师一起敲打的,感觉还是很神奇哦!!!
注意啦: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人点赞

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

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消