Encrypt 加密练习

来源:互联网 发布:minimax regret算法f5 编辑:程序博客网 时间:2024/04/27 15:38
public class Encrypt {    public static void main(String[] args) {        //base64编码/解码        String string="hello word";        String base64= Base64.encodeToString(string.getBytes());//加密        System.out.println(base64);        String string2=Base64.decodeToString(base64);//解密        System.out.println(string2);        Assert.assertEquals(string,string2);        //16进制编码/解码        String str="hello word";        String base16= Hex.encodeToString(str.getBytes());        System.out.println(base16);        String str2=new String(Hex.decode(base16.getBytes()));        System.out.println(str2);        Assert.assertEquals(str,str2);        //散列算法加密        String saltstr="hello word";        String salt="55555";        String md5=new Md5Hash(saltstr,salt).toString();        System.out.println(md5);        //散列两次        String md52=new Md5Hash(saltstr,salt,2).toString();        System.out.println(md52);        //shiro simpleHash        String shirostr="hello word";        String salt2="55555";        String simpltHash=new SimpleHash("SHA-1",shirostr,salt2).toString();        System.out.println(simpltHash);        //shiro 提供的DefaulthashService        DefaultHashService hashService=new DefaultHashService();        hashService.setHashAlgorithmName("SHA-521");//设置加密方式        hashService.setPrivateSalt(new SimpleByteSource("555555"));//设置私有盐        hashService.setGeneratePublicSalt(true);//生成公盐 默认false        hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());//生成公盐        hashService.setHashIterations(1);//设置散列迭代次数        HashRequest request=new HashRequest.Builder()                .setAlgorithmName("MD5")                .setSource(ByteSource.Util.bytes("hello word"))                .setSalt(ByteSource.Util.bytes("55555"))                .setIterations(2).build();        String hex=hashService.computeHash(request).toHex();        System.out.println(hex);        //AES算法        AesCipherService aesCipherService=new AesCipherService();        aesCipherService.setKeySize(128);        //生成key        Key key=aesCipherService.generateNewKey();        String text="hello word";        //加密        String encryptText=aesCipherService.encrypt(text.getBytes(),key.getEncoded()).toHex();        //解密        String text2=new String(aesCipherService.decrypt(Hex.decode(encryptText),key.getEncoded()).getBytes());        Assert.assertEquals(text,text2);    }
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 演员表演培训 小演员培训班 想当演员考什么学校 怎么可以当演员 北京演员助理招聘 演员培训班招生条件 想做演员怎么办 怎样成为演员 北京京剧院演员名单 yanyuan 怎么样才能当演员 恰同学少年的演员 花样少男少女日剧演员 演戏就变强 演戏 快穿演戏h 演戏h 演戏攻略 快穿之演戏片场h 我演戏就变强 演戏靠仙气修仙看人气 演戏不如捉鬼 怎么自学演戏 刘婷为了能演戏让导演潜 演戏的基本功 怎么判断你有演戏天赋 弟弟每天都在演戏 结果不会陪你演戏 快穿影后片场演戏h 演技派 神级演技派 演技 林宗辉演技 董力回应演技质疑 娱乐圈演技帝 娱乐圈演技帝月下蝶影 韩娱演技大师 韩娱之演技大师 娱乐圈演技派 我不止有演技 人生如戏全靠演技