java-des数据加密

des是数据加密一种算法,有3个参数:key,data,模式。key:是8个字节64位,data:是加密的数据,加密文件不太年夜,否者截取或者错误,模式:是加密息争密。下面在java中怎么利用des文本加密,我写的经验对你进修java有帮忙的话,给我投票、点赞或者保藏!

http://v.youku.com/v_show/id_XMzk3OTM0OTg3Ng==.html

1单标的目的加密

1加密口令

1编码和图片对象彼此转换

东西/原料

  • netbeans8.1

方式/步调

  1. 1

    新建一个java项目,项目标名称为javades。

  2. 2

    打开这个javades文件,在此中界说一个静态方式getkey,把des算法的key的8的字节生当作。

     private static SecretKey getkey(byte[] key){

            try {

                DESKeySpec ks=new DESKeySpec(key);

                SecretKeyFactory  kf=SecretKeyFactory.getInstance("des");

                SecretKey sk=kf.generateSecret(ks);

                return sk;

            } catch (InvalidKeyException | InvalidKeySpecException | NoSuchAlgorithmException ex) {

                Logger.getLogger(Javades.class.getName()).log(Level.SEVERE, null, ex);

            }

            

            return null;

        }

  3. 3

    界说一个解密方式:

     private static byte[] decrypt(byte[]data,String key){

            

            try {

                SecretKey sk=getkey(key.getBytes());

                Cipher ch=Cipher.getInstance("des");

                ch.init(Cipher.DECRYPT_MODE, sk);

                return ch.doFinal(data);

            } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {

                Logger.getLogger(Javades.class.getName()).log(Level.SEVERE, null, ex);

            }

            

            return null;

        }

  4. 4

    界说加密方式:

        try {

                SecretKey sk=getkey(key.getBytes());

                Cipher ch = Cipher.getInstance("des");

                ch.init(Cipher.ENCRYPT_MODE, sk);

                return ch.doFinal(data);

            } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {

                Logger.getLogger(Javades.class.getName()).log(Level.SEVERE, null, ex);

            }

               

            return null;

        }

  5. 5

    在main方式中加密一个字符串息争密一个字符串:

     String key="12345678";

           String data="abc123456890";

           System.err.println("data:"+data);

           byte[] jmdata=Javades.encrypt(data.getBytes(), key);

           System.err.println(Arrays.toString(jmdata));

           jm=Javades.decrypt(jm, pw);

           System.err.println(new String(jm));

  6. 6

    运行项目,成果把字符串des加密,还原字符串当作功。

  7. 7

    这个在对文件加密:

    String key="12345678";

            try {

                 File fi=new File("c:/data.txt");

                InputStream input = new FileInputStream(fi);

       

                byte[] data = new byte[input.available()];

                input.read(data);

                System.err.println("data:"+data);

                 byte[] jmdata=Javades.encrypt(data, key);

                System.err.println("加密数据:"+Arrays.toString(jmdata));

                byte[] jmjm=Javades.decrypt(jmdata, key);

                System.err.println("解密数据:"+new String(jmjm,"UTF-8"));

            } catch (FileNotFoundException ex) {

                Logger.getLogger(Javades.class.getName()).log(Level.SEVERE, null, ex);

            } catch (IOException ex) {

                Logger.getLogger(Javades.class.getName()).log(Level.SEVERE, null, ex);

            }

  8. 8

    运行项目,对文件byte[]的数据加密息争密当作功。

  • 发表于 2018-12-24 00:00
  • 阅读 ( 881 )
  • 分类:电脑网络

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
联系我们:uytrv@hotmail.com 问答工具