Java简单加密算法

Java简单加密算法

一切由Base64推敲

import java.io.UnsupportedEncodingException;
import java.util.Base64;

//简单加密
public class Base64Strategy {
        static String append = "昊天锤";
        //加密方法1
        public String enCode(String password) throws UnsupportedEncodingException {
               //格式:Base64获取加密器.加密(加密的内容),注意字符串转byte或者byte转字符串需要设置编码统一
               byte[] encode = Base64.getEncoder().encode(password.getBytes("UTF-8"));
               return new String(encode,"Utf-8");
        }

        //加密方法2
        public String superEncode(String password) throws UnsupportedEncodingException {
                //将一个固定串追加到密码后,解密成字符串时使用string的replace方法用空串取代追加的字符串
                byte[] encode = Base64.getEncoder().encode((password+append).getBytes("UTF-8"));
                return new String(encode,"UTF-8");
        }

        //解密方法,对加密后的字符串明文解密
        public String decode(String encode) throws UnsupportedEncodingException {
                //格式:Base64获取加密器.加密(加密的内容),注意字符串转byte或者byte转字符串需要设置编码统一
                byte[] decode = Base64.getDecoder().decode(encode.getBytes("UTF-8"));
                return new String(decode,"UTF-8");
        }

        public static void main(String[] args) throws UnsupportedEncodingException {
                String password = "123456";
                Base64Strategy base64Strategy = new Base64Strategy();

                System.out.println("法1加密:"+base64Strategy.enCode(password));
                System.out.println("法1解密:"+base64Strategy.decode(base64Strategy.enCode(password)));

                System.out.println("法2加密:"+base64Strategy.superEncode(password));
                System.out.println("法1解密:"+base64Strategy.decode(base64Strategy.superEncode(password)).
                        replace(append,""));

        }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fire king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值