C
C#7mo ago
Aurel

C# save file

Hi, I have a function that encrypt some string and i want to encrypt an image with it, but when i decrypt it it is just bricked; this is my code; feel free to mp me if you need more info
No description
No description
53 Replies
x0rld
x0rld7mo ago
what does EncryptText ?
jcotton42
jcotton427mo ago
images cannot be read or written as text they're binary data, attempting to treat them as text will always corrupt it and yeah, how do EncryptText and DecryptText work? also, use $code for sharing snippets
MODiX
MODiX7mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Aurel
Aurel7mo ago
wait
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
No description
x0rld
x0rld7mo ago
look what modix said
Aurel
Aurel7mo ago
not encrypted:
No description
Aurel
Aurel7mo ago
encrypted and decrypted
No description
Aurel
Aurel7mo ago
some characters are borkens
x0rld
x0rld7mo ago
first stop try to writing image as text that cannot work
Aurel
Aurel7mo ago
i tried to encrypt bites and things like that but i didnt succeeds bytes * how do i do then
x0rld
x0rld7mo ago
use base64
Angius
Angius7mo ago
thisblob
Aurel
Aurel7mo ago
also dont work with base64
Aurel
Aurel7mo ago
No description
No description
Aurel
Aurel7mo ago
original and encrypted and decrypted with base64
Angius
Angius7mo ago
Has to work with Base64
Angius
Angius7mo ago
Or you're doing something else wrong Code for reference:
var bytes = await File.ReadAllBytesAsync("v1.png");
var base64 = Convert.ToBase64String(bytes);
await File.WriteAllTextAsync("v1.txt", base64);
var newBase64 = await File.ReadAllTextAsync("v1.txt");
var newBytes = Convert.FromBase64String(newBase64);
await File.WriteAllBytesAsync("v1-new.png", newBytes);
var bytes = await File.ReadAllBytesAsync("v1.png");
var base64 = Convert.ToBase64String(bytes);
await File.WriteAllTextAsync("v1.txt", base64);
var newBase64 = await File.ReadAllTextAsync("v1.txt");
var newBytes = Convert.FromBase64String(newBase64);
await File.WriteAllBytesAsync("v1-new.png", newBytes);
<!-- .csproj -->
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Content Update="v1.png">
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>
</ItemGroup>

</Project>
<!-- .csproj -->
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Content Update="v1.png">
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>
</ItemGroup>

</Project>
Aurel
Aurel7mo ago
ty i'll try tomorrow bc its getting a little late in France
Aurel
Aurel7mo ago
No description
Pobiega
Pobiega7mo ago
unless it has changed, you're reading the file content into a string, so that borks it from the start you'll need to read it into a byte array or similar
Aurel
Aurel7mo ago
oh ok but how do i encrypt it then ok but i dont want to encrypt it with b64 it is not secure i have this for encryption how can i use this
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
No description
x0rld
x0rld7mo ago
give as input your base64 string btw microsoft doc has an easier way to do AES encryption
Aurel
Aurel7mo ago
i do but it dont work
x0rld
x0rld7mo ago
so you've done something wrong what's the issue ?
Aurel
Aurel7mo ago
look the bad thingsd that i made my encrypt thing
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
and decrypt i tried to do some bytes to things things but it fail in the decrypt function
Aurel
Aurel7mo ago
x0rld
x0rld7mo ago
cause you don't decode your base64 you write directly the base64 into a file there is a method in Convert to get bytes from a base64
Aurel
Aurel7mo ago
what is the method ?
Aurel
Aurel7mo ago
this is the error
No description
Aurel
Aurel7mo ago
i says that there is non-base64 characters @xtreit
x0rld
x0rld7mo ago
You have to use it when you write the file
Angius
Angius7mo ago
1. Read bytes 2. Convert to base64 3. Encrypt the base64 string 4. Save To load, 1. Load 2. Get the enctrypted string 3. Decrypt it 4. Base64-decode it back into bytes 5. Save the bytes as a file
Aurel
Aurel7mo ago
do i need to reconvert it to bytes when i save it? it say it cant convert from byte to char
Angius
Angius7mo ago
Are you trying to use File.WriteAllText() or something?
Aurel
Aurel7mo ago
No description
Angius
Angius7mo ago
And where do you get this error?
Aurel
Aurel7mo ago
wait i fixed it but wait
Aurel
Aurel7mo ago
this is the decrypted and the original this is all my code:
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
No description
Aurel
Aurel7mo ago
No description