C
C#•12mo 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 👻 🎃
what does EncryptText ?
jcotton42
jcotton42•12mo 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
MODiX•12mo 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
AurelOP•12mo ago
wait
Aurel
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
No description
x0rld 👻 🎃
look what modix said
Aurel
AurelOP•12mo ago
not encrypted:
No description
Aurel
AurelOP•12mo ago
encrypted and decrypted
No description
Aurel
AurelOP•12mo ago
some characters are borkens
x0rld 👻 🎃
first stop try to writing image as text that cannot work
Aurel
AurelOP•12mo ago
i tried to encrypt bites and things like that but i didnt succeeds bytes * how do i do then
x0rld 👻 🎃
use base64
Angius
Angius•12mo ago
thisblob
Aurel
AurelOP•12mo ago
also dont work with base64
Aurel
AurelOP•12mo ago
No description
No description
Aurel
AurelOP•12mo ago
original and encrypted and decrypted with base64
Angius
Angius•12mo ago
Has to work with Base64
Angius
Angius•12mo 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
AurelOP•12mo ago
ty i'll try tomorrow bc its getting a little late in France
Aurel
AurelOP•12mo ago
No description
Pobiega
Pobiega•12mo 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
AurelOP•12mo 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
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
No description
x0rld 👻 🎃
give as input your base64 string btw microsoft doc has an easier way to do AES encryption
Aurel
AurelOP•12mo ago
i do but it dont work
x0rld 👻 🎃
so you've done something wrong what's the issue ?
Aurel
AurelOP•12mo ago
look the bad thingsd that i made my encrypt thing
Aurel
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
and decrypt i tried to do some bytes to things things but it fail in the decrypt function
Aurel
AurelOP•12mo ago
x0rld 👻 🎃
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
AurelOP•12mo ago
what is the method ?
Aurel
AurelOP•12mo ago
this is the error
No description
Aurel
AurelOP•12mo ago
i says that there is non-base64 characters @xtreit
x0rld 👻 🎃
You have to use it when you write the file
Angius
Angius•12mo 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
AurelOP•12mo ago
do i need to reconvert it to bytes when i save it? it say it cant convert from byte to char
Angius
Angius•12mo ago
Are you trying to use File.WriteAllText() or something?
Aurel
AurelOP•12mo ago
No description
Angius
Angius•12mo ago
And where do you get this error?
Aurel
AurelOP•12mo ago
wait i fixed it but wait
Aurel
AurelOP•12mo ago
this is the decrypted and the original this is all my code:
Aurel
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
No description
Aurel
AurelOP•12mo ago
No description
Want results from more Discord servers?
Add your server