- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我对加密还很陌生,我正在尝试让逐行加密器工作;我需要能够在应用程序运行期间将加密的行附加到文件,而不是只是一个大的加密所有内容并保存。不过,我玩得很开心。这是我的加密器,在我自己尝试多次失败后无耻地被盗:
class Encryption
{
private static readonly byte[] SALT = new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };
public static byte[] Encrypt(byte[] plain, string password)
{
MemoryStream memoryStream;
CryptoStream cryptoStream;
Rijndael rijndael = Rijndael.Create();
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, SALT);
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(plain, 0, plain.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
return memoryStream.ToArray();
}
public static byte[] Decrypt(byte[] cipher, string password)
{
MemoryStream memoryStream;
CryptoStream cryptoStream;
Rijndael rijndael = Rijndael.Create();
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, SALT);
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);
cryptoStream.Write(cipher, 0, cipher.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
return memoryStream.ToArray();
}
}
这是一个虚拟函数,展示了我是如何尝试的:
private void EncryptFile(string filepath, string outputPath, string password) { FileInfo fileInfo = new FileInfo(filepath); string filename = fileInfo.Name; string fullpath = outputPath + "\\" + filename; BinaryWriter writer = new BinaryWriter(File.OpenWrite(fullpath), Encoding.ASCII); /// Two methods that I've attempted here: /// 1. The desired method: encrypt line by line - I assumed I'd be able to generate /// multiple blocks of data and decrypt them later. This isn't working //string[] lines = File.ReadAllLines(filepath); /// 2. Just read the whole thing and encrypt and write it in one swoop. string line = File.ReadAllText(filepath); //foreach(string line in lines) { byte[] bytes = Encoding.ASCII.GetBytes(line); byte[] encoded = Encryption.Encrypt(bytes, password); writer.Write(encoded); writer.Flush(); } writer.Close(); } private void DecryptFile(string filepath, string outputPath, string password) { FileInfo fileInfo = new FileInfo(filepath); string filename = fileInfo.Name; string fullpath = outputPath + "\\" + filename; StreamWriter writer = new StreamWriter(fullpath, false, Encoding.UTF8); byte[] bytes = File.ReadAllBytes(filepath); /// Here is the method that's working at the moment for decrypting; just /// grab all the data and decrypt it on one swoop. byte[] decrypted = Encryption.Decrypt(bytes, password); string s = Encoding.ASCII.GetString(decrypted); writer.Write(s); writer.Flush(); /// I've tried a number of things here to decrypt line by line, /// none of which work. This crashes with an issue about the padding /// being invalid. /* int index = 0; int count = 32; while (index
我不完全确定我应该做什么了。我一直在四处寻找事物并阅读在线示例,但它们似乎都是如何加密整个文件或仅加密一段数据并且除了立即再次解密之外什么都不做。逐行写应该怎么处理?
最佳答案
我不会为您编程,而是给您一个您可以实现的方案。
如果您有逐行加密,我想您也希望能够逐行解密。请注意,“线”对于计算机来说是一个相当不便的术语。它只是一堆以某种行终止符结尾的字符。字符本身使用特定的 character-encoding 编码。 .
此外,我将做出以下假设:
现在思路很简单了:
反之,逆向过程,虽然 IV 应该从 base 64 解码后的字节中检索,并且当然应该使用与加密期间相同的方法计算 key 。
关于c# - 逐行加密/解密文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12999999/
我有一个 C# 应用程序调用 Java 网络服务来验证用户密码。我想让 C# 应用程序加密密码,然后让 Java Web 服务解密密码。我已经完成了 Java 端的代码(解密代码),但我无法找出 C#
我正在使用以下代码在使用 openssl 的 Windows 中使用 C 加密和解密二进制数据。如您所见,在这两个函数中,我都知道纯文本的大小。有什么方法可以在不知道纯文本大小的情况下解密消息? #i
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有一个非常恼人的问题,Java中使用RSA算法对字符串进行不可靠的加密和解密。它似乎只能在大约 35% 的时间内工作,而且我不明白为什么它有时能工作,有时却不能。这是我写的一些测试代码,试图验证加密
我已经设法编写了用于文件加密/解密的函数。但它非常慢,尤其是随着文件大小的增加。例如几MB长的音频/视频文件 我几乎浏览了所有帖子来改进它,并尝试更改算法。如果有任何更改可以帮助我提高性能,请帮助我。
我正在尝试让我的转置密码发挥作用。 每当我将加密方法得到的密文输入解密方法时,我应该得到原始的明文......但事实并非如此...... 我做错了什么? 感谢您的帮助! public String E
我正在使用密码来加密和解密消息: public String encrypt(String string) throws InvalidKeyException, IllegalBlockSizeEx
我有一个在 MySQL 中存储数据的 spring-mvc 堆栈。其中一些数据需要保护,所以我想我应该加密它。由于我以后可能需要使用这些数据(信用卡、SSN 等),所以我需要对其进行解密。我认为这排除
作为一名SEOER,都想了解百度算法,通过算法原理来找到捷径的优化方案,那么今天我把研究多年的百度算法原理解密给大家,可能不是最好的,但是我可以给大家保证,这些都是非常实际的。希望给SEOER带来一
我试图找到一种技术来加密和解密程序中的文件,而无需将密码硬编码到程序中,也无需向用户询问密码。 如果我也可以从我正在编写的另一个程序中解密文件,那就太好了。 到目前为止,我还没有多少运气找到一种看起来
有没有一种方法可以使用作为字符串参数传递给程序的私钥而不是使用存储在机器上的证书来解密 PowerShell 中的 RSA?欢迎任何帮助,我的代码如下。 Function Decrypt-Asymme
通过问题Is it possible to use the Grails Jasypt plugin outside the GORM layer for simple String encrypti
我需要解密/加密我的域类中的几列,并且正在寻找有关如何做的信息。我已经找到了jasypt加密插件,但不幸的是它似乎与Grails 2.4不兼容。 我可能可以将一些东西拼凑在一起,但是想要确保Im遵循最
我需要有关声音文件加密/解密的帮助。我想在存储这个声音文件时加密一个声音文件,并在播放这个文件时解密它。我阅读了有关 java 中的加密/解密以及 java 中可用于此的大量示例代码。但这些程序不适用
我很感兴趣是否可以使用 Excel Visual Basic 和某些加密服务提供程序进行字符串加密/解密。 我找到了一个演练 Encrypting and Decrypting Strings in
我们正在使用加密/解密和UIIMAGE。如果我们在不保存到iphone画廊的情况下进行加密和解密以及UIIMAge,则可以正常工作,但是,如果我们进行加密,保存到画廊,将(加密的图像)加载到应用程序中
我正在做一个像这样的简单程序: package rsaexample; import java.io.*; import java.math.BigInteger; import java.secur
我发现这段代码返回给定字符串的校验和。 public static String getChecksum(String md5) { int counter = 0; while (c
我在 Java SE 和 Android 项目上使用相同的代码。在 Java 和 Android 中运行的应用程序连接到相同的 MQTT 代理并交换消息。消息使用 AES 进行加密/解密。我对 Jav
我想在 openssl/libcrypto 中使用 RSA 加密/解密一个长文件(我知道 AES 更好,但这只是为了比较)。我将输入文件分成大小为 numBlocks = inputFileLengt
我是一名优秀的程序员,十分优秀!