如何加密和解密文件与bouncy城​​堡(DES)在netbeans java? [英] How to encrypt and decrypt files with bouncy castle (DES) in netbeans java?

查看:342
本文介绍了如何加密和解密文件与bouncy城​​堡(DES)在netbeans java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用bouncy castle(DESEngine)加密和解密文件(而不是字符串)?
我已经搜索过,但找不到帮助。

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

推荐答案

对不起,我自己解决了。这是我的代码:

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

Tesbouncy.java

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

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);
    }

}




}

这篇关于如何加密和解密文件与bouncy城​​堡(DES)在netbeans java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆