How to Encrypt or Decrypt a File in Java?

后端 未结 2 1058
不思量自难忘°
不思量自难忘° 2020-12-13 23:07

I want to encrypt and decrypt a file in java, i had read this url http://www-users.york.ac.uk/~mal503/lore/pkencryption.htm and i got two files namely public Security certif

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 23:20

    Here is the Simplest Piece of Code for encrypting a document with any Key(Here Random Key)

    randomKey = randomKey.substring(0, 16);
            keyBytes = randomKey.getBytes();
            key = new SecretKeySpec(keyBytes, "AES");
            paramSpec = new IvParameterSpec(iv);
            ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            in = new FileInputStream(srcFile);
            out = new FileOutputStream(encryptedFile);
            out = new CipherOutputStream(out, ecipher);
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            in.close();
            out.flush();
            out.close();
    

提交回复
热议问题