我正在尝试通过AES在C#中创建加密/解密程序。 这是我的代码:
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);
}
}
}
加密功能可以很好地完成工作,并返回正确的加密文本。 Decrypt函数中存在问题,一切顺利(我在屏幕上打印了我的变量),直到StreamReader使用.ReadToEnd()。我得到一个CryptographyException(我只在一个exectuion中得到两次相同的异常):
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长度或其他东西的东西。
我甚至尝试在互联网上寻找更多解密功能,但它们都不适用于我(我没有使用RijndaelManaged或salt这样做)。我在MacOS编译。
答案 0 :(得分:0)
为什么代码中有这么多字符串?密码术在字节上工作,摆脱所有字符串。
我的直觉是,如果你只是把所有事情视为byte[]
,你的问题就会消失。 (是的,您要保护的数据可以是文本,因此直接使用UTF8StringToBytes命中它是好的)