ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-如何使用ElGamal加密/解密文本文件

2019-11-03 01:00:53  阅读:167  来源: 互联网

标签:java elgamal


我正在尝试使用ElGamal对文本文件进行加密和解密,但似乎无法使其正常工作.
我有一组范围从1kb-1mb的文本文件,并且我使用512位作为密钥大小.我已经知道,就像RSA一样,ELGamal不能加密的值超过其模数,因此,作为我的初始解决方案,我决定将每个文件分成多个块(小于其模数),以便能够对其进行加密幸运的是,这些解决方案适用于加密.我的问题是,当我尝试对其进行解密时,生成的输出不是我期望看到的实际输出.我不知道是什么原因造成的,我真的需要在几天内找到解决方案.

为了清楚起见,我将向您展示一些代码片段.

我已经用以下代码生成了密钥对

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ElGamal", "BC";
keyGen.initialize(512);

我通过致电进行加密

public static void encryptFile(String srcFileName, String destFileName, PublicKey key) throws Exception
{
    encryptDecryptFile(srcFileName,destFileName, key, Cipher.ENCRYPT_MODE);
}

我通过调用解密

public static void decryptFile(String srcFileName, String destFileName, PrivateKey key) throws Exception
{
    encryptDecryptFile(srcFileName,destFileName, key, Cipher.DECRYPT_MODE);
}

这是cryptoDecryptFile(..)方法的定义

public static void encryptDecryptFile(String srcFileName, String destFileName, Key key, int cipherMode) throws Exception
    {
        OutputStream outputWriter = null;
        InputStream inputReader = null;
        try
        {
            Cipher cipher = Cipher.getInstance("ElGamal/None/NoPadding", "BC"");
            String textLine = null;
    //buffer(my chunks) depends wether it is encyption or decryption
            byte[] buf = (cipherMode == Cipher.ENCRYPT_MODE? new byte[50] : new byte[64]);
            int bufl;
            // init the Cipher object for Encryption...
            cipher.init(cipherMode, key);

            // start FileIO
            outputWriter = new FileOutputStream(destFileName);
            inputReader = new FileInputStream(srcFileName);
            while ( (bufl = inputReader.read(buf)) != -1)
            {
                byte[] encText = null;
                if (cipherMode == Cipher.ENCRYPT_MODE)
                {
                      encText = encrypt(copyBytes(buf,bufl),(PublicKey)key);
                }
                else
                {
                    if (_log.isDebugEnabled())
                    {
                        System.out.println("buf = " + new String(buf));
                    }
                    encText = decrypt(copyBytes(buf,bufl),(PrivateKey)key);
                }
                outputWriter.write(encText);
                if (_log.isDebugEnabled())
                {
                    System.out.println("encText = " + new String(encText));
                }
            }
            outputWriter.flush();

        }
        catch (Exception e)
        {
            _log.error(e,e);
            throw e;
        }
        finally
        {
            try
            {
                if (outputWriter != null)
                {
                    outputWriter.close();
                }
                if (inputReader != null)
                {
                    inputReader.close();
                }
            }
            catch (Exception e)
            {
                // do nothing...
            } // end of inner try, catch (Exception)...
        }
    }

对于copyBytes:

public static byte[] copyBytes(byte[] arr, int length)
{
    byte[] newArr = null;
    if (arr.length == length)
    {
        newArr = arr;
    }
    else
    {
        newArr = new byte[length];
        for (int i = 0; i < length; i++)
        {
            newArr[i] = (byte) arr[i];
        }
    }
    return newArr;
}

对于entpt(…)

    public static byte[] encrypt(byte[] text, PublicKey key) throws Exception
{
    byte[] cipherText = null;
    try
    {

        Cipher cipher = Cipher.getInstance("ElGamal/None/NoPadding", "BC"");
        if (_log.isDebugEnabled())
        {
            _log.debug("\nProvider is: " + cipher.getProvider().getInfo());
            _log.debug("\nStart encryption with public key");
        }

        // encrypt the plaintext using the public key
        cipher.init(Cipher.ENCRYPT_MODE, key);
        cipherText = cipher.doFinal(text);
    }
    catch (Exception e)
    {
        _log.error(e, e);
        throw e;
    }
    return cipherText;
}

并解密(..)

   public static byte[] decrypt(byte[] text, PrivateKey key) throws Exception
    {
        byte[] dectyptedText = null;
        try
        {
            // decrypt the text using the private key
            Cipher cipher = Cipher.getInstance("ElGamal/None/NoPadding", "BC"");
              cipher.init(Cipher.DECRYPT_MODE, key);
            dectyptedText = cipher.doFinal(text);
        }
        catch (Exception e)
        {
            _log.error(e, e);
            throw e;
        }
        return dectyptedText;

    }

Original code by Aviran Mondo

我认为这就是您所需要的,只是告诉我是否要查看完整的源代码.
谢谢,

解决方法:

这与您的代码不太相关,但是尝试将固定宽度的块大小的块密码转换为仅可将输入分成多个块并进行加密即可在流上工作的块密码在密码上并不安全他们每个人.如果执行此操作,则实际上是在进行美化的单字母替换密码,其中每个“字符”都是一个块宽.这使攻击者可以恢复输入结构的一部分,从而破坏了通常从这些加密原语中获得的保证.例如,请参见this Wikipedia discussion of this particular mode of encryption,并查看它如何加密Linux企鹅Tux.加密的图像可立即使您看到输入的结构.

如果要使用像ElGamal这样的分组密码来加密文本流,则应使用更复杂的结构,例如cipher block chaining (CBC)counter mode (CTR),在合理大小的输入上,它们被证明具有加密安全性.如果您使用这些模式之一,攻击者将很难尝试破坏您的安全性.

对于没有太多实质性的代码,我深表歉意,但老实说,在尝试调试此加密系统之前,值得备份并选择一个强大的加密系统.否则,您将得到一个聪明的攻击者可能挫败的系统.

标签:java,elgamal
来源: https://codeday.me/bug/20191102/1995252.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有