- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 ElGamal 加密和解密文本文件以进行研究,但似乎无法使其正常工作。我有一组 1kb - 1mb 的文本文件,我使用 512 位作为我的 key 大小。我已经知道,就像 RSA 一样,ELGamal 不能加密超过其模数的值,因此作为我的初始解决方案,我决定将每个文件分成 block (小于其模数)以便我能够加密它幸运的是,这些解决方案适用于加密。我的问题是,当我试图解密它时,生成的输出不是我期望看到的实际输出。我不知道我的问题的原因是什么,我真的需要在几天内找到解决方案。
为了清楚起见,我将向您展示我的一些代码片段。
我用下面的代码生成了我的 key 对
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);
}
这是 encryptDecryptFile(..) 方法的定义
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)...
}
}
对于复制字节:
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;
}
对于加密(...)
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
我想这就是您所需要的,如果您想查看完整的源代码,请告诉我。谢谢,
最佳答案
这与您的代码不太相关,但是尝试将具有固定宽度 block 大小的 block 密码转换为可以通过将输入拆分为流来工作的 block 密码在密码学上并不安全 block 并加密它们中的每一个。如果你这样做,你实际上是在做一个美化的单字母替换密码,其中每个“字符”都是一个 block 宽。这允许攻击者恢复您输入的部分结构,这破坏了您通常从这些加密原语中获得的保证。例如,参见 this Wikipedia discussion of this particular mode of encryption看看它是如何加密 Tux the Linux Penguin 的。加密后的图像可以立即让您看到输入的结构。
如果您想使用像 ElGamal 这样的 block 密码来加密文本流,您应该使用更复杂的结构,例如 cipher block chaining (CBC)或 counter mode (CTR) ,这在合理大小的输入上是可证明的加密安全的。如果您使用其中一种模式,攻击者将很难尝试破坏您的安全。
对于您的代码没有更多内容可说,我深表歉意,但老实说,我认为在尝试调试此代码之前,值得备份并选择一个强大的加密系统。否则,您最终会得到一个聪明的攻击者可以破坏的系统。
关于java - 如何使用 ElGamal 加密/解密文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4894882/
我有一个简单的 pyparsing 构造,用于提取日志消息的部分内容。看起来像这样 log_line = 时间戳 + task_info + Suppress(LineEnd()) 此结构可以很好地解
我想定义一个函数 scaryDict(),它接受一个参数(textfile)并返回 textfile 中的单词按字母顺序排列,基本上生成字典但不打印任何一个或两个字母的单词。 这是我目前所拥有的……不
我正在尝试弄清楚如何包含对外部数据文件(文本形式)的引用,我希望通过 Web Start (JNLP) 与我的应用程序一起分发该文件。筛选 JNLP 结构的文档,我发现您可以包含对 JAR、nativ
我尝试将 Java 程序从 Eclipse 导出到 .jar 文件,但遇到了问题。它运行良好,但由于某种原因它没有找到它应该从中获取数据的文本文件。如果有人能帮忙解决这个问题,我将非常感激。 最佳答案
在过去的 20 个小时里,我试图解决以下问题,所以在开始考虑跳出窗外之前我想,我最好在这里寻求帮助: I have a text file with following content: ID 1 T
今天我试图删除一个简单文本文件中的重复行,例如: input (list.txt): hello hello try output (list.txt): try 我尝试使用 Notepad++ 删除
我将一个文本文件添加到我的项目中,如下路径所示: Myproject/WebPages/stopwords.txt 图片: http://s7.postimg.org/w65vc3lx7/Untitl
所以我在我的程序上工作,现在我无法找到解决方案。我需要在 fext 文件中替换更多的符号,目前程序只将“TIT”替换为代码“*245$a”,如果我想用同样的方式替换其他字母,程序不会改变。有人知道如何
这是一个非常简单的问题,但无论我看哪里,我都会得到不同的答案(这是因为它在 c++0x 中已经改变还是将要改变?): 在 C++ 中,我如何从一个文本文件中读取两个数字并将它们输出到另一个文本文件中?
我有一个 C++ 项目应该添加 到每一行的开头和到每一行的末尾。这适用于普通英文文本,但我有一个中文文本文件,我想这样做,但它不起作用。我通常使用 .txt 文件,但为此我必须使用 .rtf 来保存中
所以我的驱动看起来像这样: #include "problem2.h" #include "problem1.h" #include "problem3.h" #include #include
我有一个包含字符串标识符的 ascii 数字文本文件(>50k 行),可以将其视为数据 vector 的集合。根据用户输入,应用程序在运行时只需要这些数据 vector 之一。 据我所知,我有 3 个
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Any decent text diff/merge engine for .NET ? 我有两个文本文件,
我正在尝试将对话选择器中的唤醒时间和 sleep 时间记录到这样的文本文件中,但是对方法 commitToFile2 的调用不会 append 文本文件“savedData.txt”。 我知道这段代码
我开发了一个 android webview 并尝试在单击 webview 中的链接时下载生成的数据:文本文件。 webView.setDownloadListener(new Downloa
我在一个文本文件中有 250 张图像/天 4000*3000 像素。 file '/home/user/camdata/nonseqdata.jpg' file '/home/user/camdata
我曾多次尝试将此配置文件转换为多维数组,这意味着我必须读取 config.txt 文件,然后必须将其转换为多维数组。我需要帮助或一些建议。 配置文件: id=www session.timeout=1
我正在尝试使用 sublime text 3 打开文件,我想用光标在具体行号处打开它。 我一直在查subl --help但我找不到混凝土线的选择。因此我只是使用:subl filename 有没有办法
我想在我的应用程序中快速显示一个大文本文件的内容,而不是将整个文件加载到内存中。 其他人是怎么做的? Total Commander是一个很棒的工具,它有一个很棒的内部查看器可以做到这一点。无论文件有
我是一名优秀的程序员,十分优秀!