java-如何使用ElGamal加密/解密文本文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何使用ElGamal加密/解密文本文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4311字,纯文字阅读大概需要7分钟。
内容图文
我正在尝试使用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加密/解密文本文件全部内容,希望文章能够帮你解决java-如何使用ElGamal加密/解密文本文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。