由 C# AES 解密
c# AES decrypting
我正在尝试通过 AES 在 C# 中制作一个 encrypt/decrypt 程序。
这是我的代码:
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Security.Cryptography;
namespace encryptingApp
{
public class AES_Crypt
{
public static void Main ()
{
string text = "this-needs-to-be-encrypted";
string IV = "0000000000000000";
int ivBlockSize = 16;
string key = "00000000000000000000000000000000";
int keySize = 32;
string encriptedText = Encrypt(text,key,IV);
string decrypted = Decrypt(encriptedText, key, ivBlockSize);
}
public static string Encrypt(string clearText, string key, string iv )
{
byte[] textBytes=GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
encryptor.IV = GetBytes(iv);
encryptor.Key = GetBytes(key);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(encryptor.Key,encryptor.IV), CryptoStreamMode.Write))
{
cs.Write(textBytes, 0, textBytes.Length);
cs.Close();
}
string rv= iv + ByteToHex(ms.ToArray()).ToLower();
clearText = Base64Encode(rv);
}
}
return clearText;
}
public static string Decrypt(string encriptedText, string key, int ivBlockSize)
{
string decryptedText = null;
string fullText=Base64Decode(encriptedText);
string realIV = fullText.Substring( 0 , ivBlockSize );
string cypherText = fullText.Substring(ivBlockSize, fullText.Length - ivBlockSize - 1);
byte[] cypherTextInBytes = HexToByte(cypherText);
using (Aes decryptor = Aes.Create())
{
decryptor.Key = GetBytes(key);
decryptor.IV = GetBytes(realIV);
decryptor.Mode = CipherMode.CBC;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, decryptor.CreateDecryptor(decryptor.Key,decryptor.IV), CryptoStreamMode.Read))
{
using (var sr = new StreamReader(cs))
{
decryptedText = sr.ReadToEnd();
}
}
}
}
return decryptedText;
}
static byte[] GetBytes(string str)
{
return System.Text.Encoding.UTF8.GetBytes(str);
}
static string GetString(byte[] bytes)
{
return System.Text.Encoding.UTF8.GetString(bytes);
}
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
public static string ByteToHex(byte[] ba)
{
return BitConverter.ToString(ba).Replace("-", "").ToLower();
}
public static byte[] HexToByte(string hex)
{
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length >> 1; ++i)
{
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}
return arr;
}
public static int GetHexVal(char hex)
{
int val = (int) hex;
return val - (val < 58 ? 48 : 87);
}
}
}
Encrypt 函数非常出色,returns 我得到了正确的加密文本。问题存在于 Decrypt 函数中,一切顺利(我一直在屏幕上打印我的变量),直到 StreamReader 使用 .ReadToEnd()。我得到一个 CryptographyException(我在一次执行中两次得到相同的异常):
Unhandled Exception:
System.Security.Cryptography.CryptographicException: Bad PKCS7 padding. Invalid length 0.
at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (System.Security.Cryptography.PaddingMode padding, System.Int32 length, System.Int32 position) [0x0005c] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00179] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00034] in <8f2c484307284b51944a1a13a14c0266>:0
at System.Security.Cryptography.CryptoStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x00318] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.StreamReader.ReadBuffer () [0x0002b] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.StreamReader.ReadToEnd () [0x00055] in <8f2c484307284b51944a1a13a14c0266>:0
at encryptingApp.AES_Crypt.Decrypt (System.String encriptedText, System.String key, System.Int32 ivBlockSize) [0x000e4] in <f27b48dde1ea4b788e8038439b4bdb55>:0
at encryptingApp.AES_Crypt.Main () [0x000e0] in <f27b48dde1ea4b788e8038439b4bdb55>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Security.Cryptography.CryptographicException: Bad PKCS7 padding. Invalid length 0.
at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (System.Security.Cryptography.PaddingMode padding, System.Int32 length, System.Int32 position) [0x0005c] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00179] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00034] in <8f2c484307284b51944a1a13a14c0266>:0
at System.Security.Cryptography.CryptoStream.FlushFinalBlock () [0x0001b] in <8f2c484307284b51944a1a13a14c0266>:0
at System.Security.Cryptography.CryptoStream.Dispose (System.Boolean disposing) [0x00011] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.Stream.Close () [0x00000] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.StreamReader.Dispose (System.Boolean disposing) [0x0001c] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.TextReader.Dispose () [0x00000] in <8f2c484307284b51944a1a13a14c0266>:0
at encryptingApp.AES_Crypt.Decrypt (System.String encriptedText, System.String key, System.Int32 ivBlockSize) [0x000f8] in <f27b48dde1ea4b788e8038439b4bdb55>:0
at encryptingApp.AES_Crypt.Main () [0x000e0] in <f27b48dde1ea4b788e8038439b4bdb55>:0
我想我应该修复 Streamreader,但我不知道该怎么做,我已经在这里待了几个小时了!该程序似乎正在尝试读取长度为 0 或其他内容的内容。
我什至尝试在互联网上寻找更多解密功能,但 none 对我有用(我在没有 RijndaelManaged 或盐的情况下这样做)。我在 MacOS 中编译。
为什么你的代码中有这么多字符串?密码学以字节为单位,去掉所有的字符串。
- 您的 IV(理想情况下应该是 Encrypt generated/emitted,而不是传递给它)被读取为 UTF-8 字符串。如果它有任何不在 ASCII 0-127 中的字符,它就不会有 1:1 字符串长度到 byte[] 长度。
- 你也很难提供一个带有 iv 0x00000000000000000000000000000003 的字符串
- 与您的密钥类似。
- 您作为 IV+Ciphertext blob Base64Encode(UTF8Bytes(Concat(ivString, Hex(ciphertextBytes)))) 发出。
- Base64Encode(Concat(ivBytes, ciphertextBytes)) 涉及少两个转换。所以它更不容易出错,而且速度更快。
- 当您进入 Decrypt 时,您会正确应用所有转换以撤消您所做的操作,但您需要从外部知道有多少字符构成了 IV。由于您已经将 UTF8 处理添加到混合中,您实际上并不知道它。
- 因此您的 Decrypt IV 得到以下处理
- Base64解码
- UTF8BytesToString
- 子串
- UTF8StringToBytes
- 如果不涉及字符串,您只需使用 Base64Decode 和 Copy。
我的直觉是,如果您将所有事情都视为 byte[]
,您的问题就会消失。 (是的,您要保护的数据可以是文本,因此立即使用 UTF8StringToBytes 就可以了)
我正在尝试通过 AES 在 C# 中制作一个 encrypt/decrypt 程序。 这是我的代码:
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Security.Cryptography;
namespace encryptingApp
{
public class AES_Crypt
{
public static void Main ()
{
string text = "this-needs-to-be-encrypted";
string IV = "0000000000000000";
int ivBlockSize = 16;
string key = "00000000000000000000000000000000";
int keySize = 32;
string encriptedText = Encrypt(text,key,IV);
string decrypted = Decrypt(encriptedText, key, ivBlockSize);
}
public static string Encrypt(string clearText, string key, string iv )
{
byte[] textBytes=GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
encryptor.IV = GetBytes(iv);
encryptor.Key = GetBytes(key);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(encryptor.Key,encryptor.IV), CryptoStreamMode.Write))
{
cs.Write(textBytes, 0, textBytes.Length);
cs.Close();
}
string rv= iv + ByteToHex(ms.ToArray()).ToLower();
clearText = Base64Encode(rv);
}
}
return clearText;
}
public static string Decrypt(string encriptedText, string key, int ivBlockSize)
{
string decryptedText = null;
string fullText=Base64Decode(encriptedText);
string realIV = fullText.Substring( 0 , ivBlockSize );
string cypherText = fullText.Substring(ivBlockSize, fullText.Length - ivBlockSize - 1);
byte[] cypherTextInBytes = HexToByte(cypherText);
using (Aes decryptor = Aes.Create())
{
decryptor.Key = GetBytes(key);
decryptor.IV = GetBytes(realIV);
decryptor.Mode = CipherMode.CBC;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, decryptor.CreateDecryptor(decryptor.Key,decryptor.IV), CryptoStreamMode.Read))
{
using (var sr = new StreamReader(cs))
{
decryptedText = sr.ReadToEnd();
}
}
}
}
return decryptedText;
}
static byte[] GetBytes(string str)
{
return System.Text.Encoding.UTF8.GetBytes(str);
}
static string GetString(byte[] bytes)
{
return System.Text.Encoding.UTF8.GetString(bytes);
}
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
public static string ByteToHex(byte[] ba)
{
return BitConverter.ToString(ba).Replace("-", "").ToLower();
}
public static byte[] HexToByte(string hex)
{
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length >> 1; ++i)
{
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}
return arr;
}
public static int GetHexVal(char hex)
{
int val = (int) hex;
return val - (val < 58 ? 48 : 87);
}
}
}
Encrypt 函数非常出色,returns 我得到了正确的加密文本。问题存在于 Decrypt 函数中,一切顺利(我一直在屏幕上打印我的变量),直到 StreamReader 使用 .ReadToEnd()。我得到一个 CryptographyException(我在一次执行中两次得到相同的异常):
Unhandled Exception:
System.Security.Cryptography.CryptographicException: Bad PKCS7 padding. Invalid length 0.
at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (System.Security.Cryptography.PaddingMode padding, System.Int32 length, System.Int32 position) [0x0005c] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00179] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00034] in <8f2c484307284b51944a1a13a14c0266>:0
at System.Security.Cryptography.CryptoStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x00318] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.StreamReader.ReadBuffer () [0x0002b] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.StreamReader.ReadToEnd () [0x00055] in <8f2c484307284b51944a1a13a14c0266>:0
at encryptingApp.AES_Crypt.Decrypt (System.String encriptedText, System.String key, System.Int32 ivBlockSize) [0x000e4] in <f27b48dde1ea4b788e8038439b4bdb55>:0
at encryptingApp.AES_Crypt.Main () [0x000e0] in <f27b48dde1ea4b788e8038439b4bdb55>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Security.Cryptography.CryptographicException: Bad PKCS7 padding. Invalid length 0.
at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (System.Security.Cryptography.PaddingMode padding, System.Int32 length, System.Int32 position) [0x0005c] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00179] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00034] in <8f2c484307284b51944a1a13a14c0266>:0
at System.Security.Cryptography.CryptoStream.FlushFinalBlock () [0x0001b] in <8f2c484307284b51944a1a13a14c0266>:0
at System.Security.Cryptography.CryptoStream.Dispose (System.Boolean disposing) [0x00011] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.Stream.Close () [0x00000] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.StreamReader.Dispose (System.Boolean disposing) [0x0001c] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.TextReader.Dispose () [0x00000] in <8f2c484307284b51944a1a13a14c0266>:0
at encryptingApp.AES_Crypt.Decrypt (System.String encriptedText, System.String key, System.Int32 ivBlockSize) [0x000f8] in <f27b48dde1ea4b788e8038439b4bdb55>:0
at encryptingApp.AES_Crypt.Main () [0x000e0] in <f27b48dde1ea4b788e8038439b4bdb55>:0
我想我应该修复 Streamreader,但我不知道该怎么做,我已经在这里待了几个小时了!该程序似乎正在尝试读取长度为 0 或其他内容的内容。
我什至尝试在互联网上寻找更多解密功能,但 none 对我有用(我在没有 RijndaelManaged 或盐的情况下这样做)。我在 MacOS 中编译。
为什么你的代码中有这么多字符串?密码学以字节为单位,去掉所有的字符串。
- 您的 IV(理想情况下应该是 Encrypt generated/emitted,而不是传递给它)被读取为 UTF-8 字符串。如果它有任何不在 ASCII 0-127 中的字符,它就不会有 1:1 字符串长度到 byte[] 长度。
- 你也很难提供一个带有 iv 0x00000000000000000000000000000003 的字符串
- 与您的密钥类似。
- 您作为 IV+Ciphertext blob Base64Encode(UTF8Bytes(Concat(ivString, Hex(ciphertextBytes)))) 发出。
- Base64Encode(Concat(ivBytes, ciphertextBytes)) 涉及少两个转换。所以它更不容易出错,而且速度更快。
- 当您进入 Decrypt 时,您会正确应用所有转换以撤消您所做的操作,但您需要从外部知道有多少字符构成了 IV。由于您已经将 UTF8 处理添加到混合中,您实际上并不知道它。
- 因此您的 Decrypt IV 得到以下处理
- Base64解码
- UTF8BytesToString
- 子串
- UTF8StringToBytes
- 如果不涉及字符串,您只需使用 Base64Decode 和 Copy。
- 因此您的 Decrypt IV 得到以下处理
我的直觉是,如果您将所有事情都视为 byte[]
,您的问题就会消失。 (是的,您要保护的数据可以是文本,因此立即使用 UTF8StringToBytes 就可以了)