如何在netbeans java中使用充气城堡(DES)加密和解密文件?

Posted

技术标签:

【中文标题】如何在netbeans java中使用充气城堡(DES)加密和解密文件?【英文标题】:How to encrypt and decrypt files with bouncy castle (DES) in netbeans java? 【发布时间】:2013-12-08 11:27:20 【问题描述】:

如何使用充气城堡 (DESEngine) 加密和解密文件(不是字符串)? 我之前搜索过,但找不到帮助。

【问题讨论】:

您应该向我们展示您之前实际尝试过的内容。你怎么知道这个引擎是你需要的?你想完成什么? 你为什么使用 DES 而不是像 AES 这样的安全密码? 我自己解决了。我会尽快发布我的答案。我想尝试 3DES,但对 DES 的每个部分都进行了一些修改 【参考方案1】:

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

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







【讨论】:

`嗨,我正在尝试将您的代码转换为 C#,在 BouncyCastle 程序集中没有 BlockCipher,CMIIW

以上是关于如何在netbeans java中使用充气城堡(DES)加密和解密文件?的主要内容,如果未能解决你的问题,请参考以下文章

使用充气城堡和证书私钥的数字签名

使用充气城堡验证签名

充气城堡 C# 中的 PBKDF2

验证充气城堡上的 javacard 签名 ALG_ECDSA_SHA

c#充气城堡验证签名上的InvalidCastException

我正在尝试用 tomcat 实现充气城堡,但我得到了 ClassNotFound Exception