Skip to content

Commit

Permalink
#113 AES savekey from xml to json (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
maythamfahmi authored Oct 20, 2024
1 parent d66ed05 commit 3380863
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 15 deletions.
5 changes: 0 additions & 5 deletions CryptoNet.Models/CryptoNetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,10 @@ public AesDetail(byte[] key, byte[] iv)
public AesKeyValue AesKeyValue { get; set; }
}

[Serializable()]
public class AesKeyValue
{
[System.Xml.Serialization.XmlElement("key")]
public byte[] Key { get; set; }

Check warning on line 56 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 56 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 56 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 56 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 56 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 56 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 56 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 56 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

[System.Xml.Serialization.XmlElement("iv")]
public byte[] Iv { get; set; }

Check warning on line 57 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable property 'Iv' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable property 'Iv' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Iv' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Iv' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable property 'Iv' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable property 'Iv' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable property 'Iv' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 57 in CryptoNet.Models/CryptoNetInfo.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Non-nullable property 'Iv' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

}

public enum KeyType
Expand Down
2 changes: 1 addition & 1 deletion CryptoNet.UnitTests/CryptoNetAesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace CryptoNet.UnitTests;
public class CryptoNetAesTests
{
private static readonly string BaseFolder = AppDomain.CurrentDomain.BaseDirectory;
private static readonly string SymmetricKeyFile = Path.Combine(BaseFolder, $"{KeyType.SymmetricKey}.xml");
private static readonly string SymmetricKeyFile = Path.Combine(BaseFolder, $"{KeyType.SymmetricKey}.json");
private static readonly byte[] symmetricKey = Encoding.UTF8.GetBytes("b14ca5898a4e4133bbce2ea2315a1916");

[Test]
Expand Down
1 change: 1 addition & 0 deletions CryptoNet/CryptoNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
</Content>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 4 additions & 9 deletions CryptoNet/Utils/CryptoNetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.IO;
using System.Text.Json;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Xml.Serialization;
Expand Down Expand Up @@ -42,18 +43,12 @@ internal static void SaveKey(string filename, string content)
internal static string ExportAndSaveAesKey(Aes aes)
{
AesKeyValue aesKeyValue = new AesKeyValue { Key = aes.Key, Iv = aes.IV };
XmlSerializer serializer = new XmlSerializer(typeof(AesKeyValue));
StringWriter writer = new StringWriter();
serializer.Serialize(writer, aesKeyValue);
writer.Close();
return writer.ToString();
return JsonSerializer.Serialize(aesKeyValue);
}

internal static AesKeyValue ImportAesKey(string aes)
internal static AesKeyValue ImportAesKey(string aesJson)
{
XmlSerializer deserializer = new XmlSerializer(typeof(AesKeyValue));
StringReader reader = new StringReader(aes);
return (AesKeyValue)deserializer.Deserialize(reader)!;
return JsonSerializer.Deserialize<AesKeyValue>(aesJson)!;
}

internal static string GetDescription(KeyType value)
Expand Down

0 comments on commit 3380863

Please sign in to comment.