- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试制作一个可以加密和解密文件的控制台应用程序。我给加密文件一个自定义文件扩展名“.aes”(例如 samplefile.aes),现在问题是当我解密文件时,无法识别加密文件的原始扩展名是什么。我有机会从 AES 256 加密文件中获取文件类型吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;
namespace FileEncryption
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Environment.ExitCode = 1;
Console.Error.WriteLine(Strings.CommandlineUsage);
return;
}
bool encrypt = args[0].StartsWith("-e", StringComparison.InvariantCultureIgnoreCase);
bool decrypt = args[0].StartsWith("-d", StringComparison.InvariantCultureIgnoreCase);
if (!(encrypt || decrypt))
{
Environment.ExitCode = 1;
Console.Error.WriteLine(Strings.CommandlineUnknownMode);
return;
}
string inputname = (args.Length >= 3) ? args[2] : null;
if (inputname != null && !File.Exists(inputname))
{
Environment.ExitCode = 2;
Console.Error.WriteLine(Strings.CommandlineInputFileNotFound);
return;
}
byte[] passwordBytes = Encoding.UTF8.GetBytes(args[1]);
// Hash the password with SHA256
passwordBytes = SHA256Managed.Create().ComputeHash(passwordBytes);
try
{
if (encrypt)
{
//Encrypt file
byte[] bytesToBeEncrypted = File.ReadAllBytes(inputname);
byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
//Save encrypted file
string fileEncrypted = inputname.Remove(inputname.IndexOf('.')) + ".aes";
File.WriteAllBytes(fileEncrypted, bytesEncrypted);
}
else
{
byte[] bytesToBeDecrypted = File.ReadAllBytes(inputname);
byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
string file = inputname;
File.WriteAllBytes(file, bytesDecrypted);
}
Environment.ExitCode = 0;
}
catch (Exception ex)
{
Console.Error.WriteLine(string.Format(Strings.CommandlineError, ex.Message));
}
}
//***********************************************************************************************
// --- HELPER FUNCTIONS ---
//*
//Encrypt File
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
//Decrypt File
public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
}
}
我知道加密文件的本质是不识别原始扩展名的。我要做的是,当我加密文件时,我会给它 .originalextension.aes(例如 picture.png.aes),然后当我解密它时,我只需要删除 .aes 扩展名。只是想知道这是不是一个好方法?
最佳答案
文件类型是否需要加密隐藏是用户需求。您可能只想保护内容而不是文件类型。请注意,文件大小也可以向“攻击者”提供提示。是否需要隐藏此类元数据也取决于您。
如果没有扩展名,您可以使用文件指纹识别查看文件内容。许多文件格式都给出了一些关于它们是什么的提示。例如,猜测 .txt 文件的字符编码当然是可能的。 JPG 文件具有 JPG header ,zip 存档等也是如此。通常这些工具输出 MIME 文件类型(因为扩展名并不总是定义明确或标准化)。
在 GNU 系统上,您可以为此使用 file
命令行。这种指纹识别也用于许多内容管理系统 (CMS)。因此,您可以查看使用您的语言的 CMS,并尝试过滤掉其中的 file
命令功能。
或者,现在您知道了正确的搜索关键字,您可以在 StackOverflow 上简单地找到它:Using .NET, how can you find the mime type of a file based on the file signature not the extension在输入“指纹文件类型 c# mime”时被列为第 3 次命中。
请注意,由于扩展并不总是定义得那么好,您可能无法通过这种方式取回原始扩展。为此,您需要以明文(在文件名中)存储扩展名,增加文件名大小(!),或者您可以将其与数据一起加密存储。您必须为此创建一个加密协议(protocol)。
关于c# - 如何从 AES 256 加密文件中获取文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38449626/
我只想使用这 3 种模式从 openSSL 测试 AES: key 长度为 128,192 和 256,但我的解密文本与我的输入不同,我不知道为什么。此外,当我传递一个巨大的输入长度(比如说 1024
最近我终于(在 stackoverflow 的用户@WhozCraig 的帮助下)开始在 CBC 模式下使用 AES。现在,我想做完全相同的事情,但使用 AES IGE。我查看了 openssl-1.
网络设备已经配置了 snmpv3 用户,使用 AES192 作为隐私协议(protocol)。但是当执行以下命令时 snmpwalk -v3 -l authPriv -u user -a SHA -A
我在 c# 中使用 AES 算法进行加密和解密。我使用 AesCryptoServiceProvider 类进行加密和解密。 这是我在代码中的设置 AesCryptoServiceProvider r
我正在尝试使用具有不同 key 大小的 openssl 的 AES_decrypt 函数来解密密文。我能够成功解密 key 大小 = 128 的消息。这是我的代码 mydecrypt.c #inclu
如何在 AES-128、AES-192 和 AES-256 之间切换。我目前的实现仅使用 AES-128 Cipher cipher = Cipher.getInstance("AES/CBC/NoP
我的问题是我想在一个线图上叠加一个散点图,这两个图的颜色随着一个变量而变化。我只想保留一种颜色的图例。如果我使用 scale_colour_discrete(guide = "none") 它们都将消
我想用 C# 编写一个可以打开 KeePass 的程序1.x kdb 文件。我下载了源代码并尝试移植密码数据库读取功能。数据库内容已加密。加密 key 通过以下方式获得: 用户输入密码; 计算密码的
我只想将ruby代码迁移到Java 这是我的 ruby 代码 require 'openssl' require 'base64' key = '7c54367a45b37a192abc2cd7f45
我正在使用 AES 的 PyCrypto 实现,并且我正在尝试使用 24 字节 key 加密一些文本(24 字节)。 aes_ecb = AES.new('\x00'*24, AES.MODE_ECB
有人比较这些加密算法的优缺点吗? 最佳答案 使用 AES。 更多详细信息: DES 是七十年代的旧“数据加密标准”。它的 key 大小对于适当的安全性而言太短(56 个有效位;这可以被暴力破解,如 m
我在 iOS 中加密一个 NSString,编码和解码都很好: NSString *stringtoEncrypt = @"This string is to be encrypted"; NSStr
我正在尝试使用 nVidia CUDA 在 CTR 模式下实现 AES-256。我已经成功地为 key 扩展编写了 CPU 代码,现在我需要实现实际的 AES-256 算法。根据维基百科,我见过一些代
我正在 Contiki OS 中研究 AES 安全性。我有 AES 库,它支持两种类型的加密/解密: 即时 固定键 在即时中,当我使用 key 加密数据时,会生成新 key 和加密数据。这个新生成的
关于 AES 有很多问题,但我有以下问题。我目前正在使用以下 AES 实现来加密数据 byte [] PRFkey = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
有没有人一起比较这些加密算法的优缺点? 最佳答案 使用 AES。 更多细节: DES 是七十年代的旧“数据加密标准”。它的 key 大小对于适当的安全性来说太短了(56 位有效位;这可以被强制执行,正
我的团队需要开发一种解决方案,以在用 Java 编写的 Android 应用程序的上下文中加密二进制数据(存储为 byte[])。加密后的数据将通过多种方式传输和存储,在此过程中不排除出现数据损坏的情
我在客户端使用 CryptoJS AES 算法加密文本,我在服务器端用 java 解密它,但出现异常。 JS代码: var encrypted = CryptoJS.AES.encrypt("Mess
我之所以问这个问题,是因为 2 天来我已经阅读了很多关于加密 AES 加密的帖子,就在我以为我明白了的时候,我意识到我根本没有明白。 这篇文章是最接近我的问题的,我有完全相同的问题但没有得到解答: C
我想知道 AES 加密后的数据大小,这样我就可以避免缓冲我的 AES 后数据(在磁盘或内存上)主要是为了知道大小。 我使用 128 位 AES 和 javax.crypto.Cipher 和 java
我是一名优秀的程序员,十分优秀!