私は単純な文字列の暗号化と復号化しようとしたとき、すべてが完璧に罰金行きでは動作しません。..C#ラインダール復号化はJPGのみ
をしかし、私はByteArrayのにJPGをコード化し、それとまったく同じことをしたときbytearray、復号化はもはや動作しませんでした(bytearrayは元のものとは全く異なり、もう表示できません)...
bytearrayが大きすぎますか?
誰かが私の問題の解決策を持っていますか?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Encrypter2
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine();
// Create a new instance of the Rijndael
// class. This generates a new key and initialization
// vector (IV).
byte[] originalFile = File.ReadAllBytes(@"C:/Users/Elron/Documents/Visual Studio 2015/Projects/FileEncrypt/FileEncrypt/bin/Debug/harambe.jpg");
using (Rijndael myRijndael = Rijndael.Create())
{
// Encrypt the string to an array of bytes
// Encrypted byte[]
byte[] encrypted = EncryptStringToBytes(originalFile, myRijndael.Key, myRijndael.IV);
using (FileStream fs = File.Create(@"C:/Users/Elron/Documents/Visual Studio 2015/Projects/FileEncrypt/FileEncrypt/bin/Debug/harambeEncrypted.jpg"))
{
//Add some information to the file.
fs.Write(encrypted, 0, encrypted.Length);
}
// Decrypted byte[]
byte[] roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);
using (FileStream fs = File.Create(@"C:/Users/Elron/Documents/Visual Studio 2015/Projects/FileEncrypt/FileEncrypt/bin/Debug/harambeDecrypted.jpg"))
{
//Add some information to the file.
fs.Write(roundtrip, 0, roundtrip.Length);
}
// Display the original data and the decrypted data.
// Encrypted string
Console.ReadKey();
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
static byte[] EncryptStringToBytes(byte[] plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
string toEncrypt = Encoding.Default.GetString(plainText);
swEncrypt.Write(toEncrypt);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static byte[] DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
byte[] returnText;
// Create an Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
ASCIIEncoding asc = new ASCIIEncoding();
returnText = asc.GetBytes(plaintext);
}
}
}
}
return returnText;
}
}
}
バイナリデータのテキスト用のStreamReaderとStreamWriterを使用することは意味がありません。どこからこのコードをコピーして貼り付けたのですか?それが何をしているのか理解していますか?最初に解決しようとしている問題は何ですか? – CodeCaster
MSDNのページからそれをコピーし、後で多くのストリームチュートリアルを見て、それを理解しようとしました。 – Mreifenberger