Java大数据加密的方式,最好有源码,求教

sinat_15556943 2014-12-02 03:32:00
Java大数据加密的方式,最好有源码
...全文
373 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianfang 2014-12-04
  • 打赏
  • 举报
回复
        /**
         * 加密
         * 
         * @param content 需要加密的内容
         * @param password  加密密码
         * @return
         */
        public static byte[] encrypt(String content, String password) {
                try {           
                        KeyGenerator kgen = KeyGenerator.getInstance("AES");
                        kgen.init(128, new SecureRandom(password.getBytes()));
                        SecretKey secretKey = kgen.generateKey();
                        byte[] enCodeFormat = secretKey.getEncoded();
                        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
                        Cipher cipher = Cipher.getInstance("AES");// 创建密码器
                        byte[] byteContent = content.getBytes("utf-8");
                        cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
                        byte[] result = cipher.doFinal(byteContent);
                        return result; // 加密
                } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                        e.printStackTrace();
                } catch (InvalidKeyException e) {
                        e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                        e.printStackTrace();
                } catch (BadPaddingException e) {
                        e.printStackTrace();
                }
                return null;
        }
        /**解密
         * @param content  待解密内容
         * @param password 解密密钥
         * @return
         */
        public static byte[] decrypt(byte[] content, String password) {
                try {
                         KeyGenerator kgen = KeyGenerator.getInstance("AES");
                         kgen.init(128, new SecureRandom(password.getBytes()));
                         SecretKey secretKey = kgen.generateKey();
                         byte[] enCodeFormat = secretKey.getEncoded();
                         SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");            
                         Cipher cipher = Cipher.getInstance("AES");// 创建密码器
                        cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
                        byte[] result = cipher.doFinal(content);
                        return result; // 加密
                } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                        e.printStackTrace();
                } catch (InvalidKeyException e) {
                        e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                        e.printStackTrace();
                } catch (BadPaddingException e) {
                        e.printStackTrace();
                }
                return null;
        }
sinat_15556943 2014-12-03
  • 打赏
  • 举报
回复
引用 1 楼 tianfang 的回复:
大数据加密主要采用对称密钥方法,常用AES,DES,3DES javax.crypto.Cipher https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html 支持多种加密方法,大数据时候推荐用AES,注意padding参数的设置 jdk8支持处理器的AES指令加速
有没有demo啊
冥王之锤 2014-12-02
  • 打赏
  • 举报
回复
打个带密码的压缩包。
tianfang 2014-12-02
  • 打赏
  • 举报
回复
大数据加密主要采用对称密钥方法,常用AES,DES,3DES javax.crypto.Cipher https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html 支持多种加密方法,大数据时候推荐用AES,注意padding参数的设置 jdk8支持处理器的AES指令加速

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧