How to encrypt and decrypt files with bouncy castle (DES) in netbeans java?

﹥>﹥吖頭↗ 提交于 2019-12-08 13:02:16

问题


How can I encrypt and decrypt files (not strings) with bouncy castle (DESEngine)? I have searched before but could not find help.


回答1:


Sorry, I have solved this by myself. Here is my code:

Tesbouncy.java

    package tesbouncy;

    import org.bouncycastle.crypto.*;
    import org.bouncycastle.crypto.engines.*;
    import org.bouncycastle.crypto.modes.*;
    import org.bouncycastle.crypto.paddings.*;
    import org.bouncycastle.crypto.params.*;

    public class Tesbouncy {

        BlockCipher engine = new DESEngine();

        public byte[] Encrypt(String keys, byte[] plainText) {
            byte[] key = keys.getBytes();
            byte[] ptBytes = plainText;
            BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
            cipher.init(true, new KeyParameter(key));
            byte[] rv = new byte[cipher.getOutputSize(ptBytes.length)];
            int tam = cipher.processBytes(ptBytes, 0, ptBytes.length, rv, 0);
            try {
                cipher.doFinal(rv, tam);
            } catch (Exception ce) {
                ce.printStackTrace();
            }
            return rv;
        }

        public byte[] Decrypt(String key2, byte[] cipherText) {
            byte[] key = key2.getBytes();
            BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
            cipher.init(false, new KeyParameter(key));
            byte[] rv = new byte[cipher.getOutputSize(cipherText.length)];
            int tam = cipher.processBytes(cipherText, 0, cipherText.length, rv, 0);
            try {
                cipher.doFinal(rv, tam);
            } catch (Exception ce) {
                ce.printStackTrace();
            }
            return rv;
        }

    }

Main.java

 package tesbouncy;


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import javax.crypto.Cipher;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.spec.SecretKeySpec;



    public class Main {
 /**
 * @param args the command line arguments
 */



public static void main(String[] args) throws Exception {

    File file = new File("sql/design.jpg");     
            File file2 = new File("sql/design2.jpg"); 
    try {
        FileInputStream imageInFile = new FileInputStream(file);
        byte imageData[] = new byte[(int)file.length()];
        imageInFile.read(imageData);

                     Tesbouncy esource = new Tesbouncy();
    String key = "12345678";
    byte[] enc = esource.Encrypt(key, imageData);
        byte[] imageByteArray = enc;
        FileOutputStream imageOutFile = new FileOutputStream("sql/design2.jpg");
        imageOutFile.write(imageByteArray);

        imageInFile.close();
        imageOutFile.close();



                    FileInputStream imageInFile2 = new FileInputStream(file2);
        byte imageData2[] = new byte[(int)file2.length()];
        imageInFile2.read(imageData2);





                    Tesbouncy esource2 = new Tesbouncy();
    String key2 = "12345678";
    byte[] cad2 = imageData2;
    byte[] keyb2 = key2.getBytes();

    byte[] des2 = esource2.Decrypt(key2, cad2);
    byte[] imageByteArray2 = des2;
        FileOutputStream imageOutFile2 = new FileOutputStream("sql/design3.jpg");
        imageOutFile2.write(imageByteArray2);

        imageInFile2.close();
        imageOutFile2.close();

                    System.out.println("Image Successfully restore!");

    } catch (FileNotFoundException e) {
        System.out.println("Image not found" + e);



    } catch (IOException ioe) {
        System.out.println("Exception while reading the Image " + ioe);
    }

}




}


来源:https://stackoverflow.com/questions/20452880/how-to-encrypt-and-decrypt-files-with-bouncy-castle-des-in-netbeans-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!