Skip to content

Commit d2fd0b3

Browse files
committed
add demo
1 parent c551b50 commit d2fd0b3

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

example/NETCore.Encrypt.Demo/NETCore.Encrypt.Demo.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp1.1</TargetFramework>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\NETCore.Encrypt\NETCore.Encrypt.csproj" />
10+
</ItemGroup>
11+
812
</Project>
Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,104 @@
11
using System;
2+
using System.Security.Cryptography;
3+
using System.Text;
24

35
namespace NETCore.Encrypt.Demo
46
{
57
class Program
68
{
79
static void Main(string[] args)
810
{
9-
Console.WriteLine("Hello World!");
11+
var aesKey = EncryptProvider.CreateAesKey();
12+
var key = aesKey.Key;
13+
var iv = aesKey.IV;
14+
15+
/*
16+
var _max = 10000;
17+
18+
var s1 = Stopwatch.StartNew();
19+
20+
for (int i = 0; i < _max; i++)
21+
{
22+
aesKey = EncryptProvider.CreateAesKey();
23+
}
24+
s1.Stop();
25+
26+
var s2 = Stopwatch.StartNew();
27+
for (int i = 0; i < _max; i++)
28+
{
29+
aesKey = EncryptProvider.CreateAesKey(false);
30+
}
31+
s2.Stop();
32+
33+
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
34+
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
35+
Console.Read();
36+
*/
37+
38+
var plaintext = "Hello world 123456789/*-+!@#$%^&*()-=_+";
39+
var encrypted = EncryptProvider.AESEncrypt(plaintext, key, iv);
40+
var decrypted = EncryptProvider.AESDecrypt(encrypted, key, iv);
41+
42+
Console.WriteLine("Plaintext to encrypt: " + plaintext);
43+
Console.WriteLine();
44+
45+
Console.WriteLine("** AES SecureRandom **");
46+
Console.WriteLine("Encrypted " + " (Length: " + encrypted.Length + ") " + encrypted);
47+
Console.WriteLine("Decrypted " + " (Length: " + decrypted.Length + ") " + decrypted);
48+
//Console.WriteLine("Key: {0} IV: {1}", key, iv);
49+
50+
Console.WriteLine();
51+
Console.WriteLine("** AES SecureRandom with Byte input/output **");
52+
//byte[] bencrypted = EncryptProvider.AESEncrypt(Encoding.UTF8.GetBytes(plaintext), key, iv);
53+
//byte[] bdecrypted = EncryptProvider.AESDecrypt(bencrypted, key, iv);
54+
55+
//Console.WriteLine("Encrypted " + " (Length: " + bencrypted.Length + ") " + Encoding.UTF8.GetString(bencrypted));
56+
//Console.WriteLine("Decrypted " + " (Length: " + bdecrypted.Length + ") " + Encoding.UTF8.GetString(bdecrypted));
57+
//Console.WriteLine("Key: {0} IV: {1}", key, iv);
58+
59+
Console.WriteLine();
60+
61+
Console.WriteLine("** AES Non-SecureRandom **");
62+
63+
aesKey = EncryptProvider.CreateAesKey();
64+
key = aesKey.Key;
65+
iv = aesKey.IV;
66+
67+
encrypted = EncryptProvider.AESEncrypt(plaintext, key, iv);
68+
decrypted = EncryptProvider.AESDecrypt(encrypted, key, iv);
69+
Console.WriteLine("Encrypted " + " (Length: " + encrypted.Length + ") " + encrypted);
70+
Console.WriteLine("Decrypted " + " (Length: " + decrypted.Length + ") " + decrypted);
71+
//Console.WriteLine("Key: {0} IV: {1}", key, iv);
72+
73+
Console.WriteLine();
74+
Console.WriteLine("** RSA **");
75+
var rsaKey = EncryptProvider.CreateRsaKey();
76+
77+
var publicKey = rsaKey.PublicKey;
78+
var privateKey = rsaKey.PrivateKey;
79+
//var exponent = rsaKey.Exponent;
80+
//var modulus = rsaKey.Modulus;
81+
82+
encrypted = EncryptProvider.RSAEncrypt(publicKey, plaintext);
83+
84+
encrypted = EncryptProvider.RSAEncrypt(publicKey, plaintext, RSAEncryptionPadding.Pkcs1);
85+
decrypted = EncryptProvider.RSADecrypt(privateKey, encrypted, RSAEncryptionPadding.Pkcs1);
86+
87+
88+
Console.WriteLine("Encrypted: " + encrypted);
89+
Console.WriteLine("Decrypted: " + decrypted);
90+
//Console.WriteLine("publicKey: {0} privateKey: {1}", publicKey, privateKey);
91+
92+
Console.WriteLine();
93+
Console.WriteLine("** SHA **");
94+
Console.WriteLine("SHA1: " + EncryptProvider.Sha1(plaintext));
95+
Console.WriteLine("SHA256: " + EncryptProvider.Sha256(plaintext));
96+
Console.WriteLine("SHA384: " + EncryptProvider.Sha384(plaintext));
97+
Console.WriteLine("SHA512: " + EncryptProvider.Sha512(plaintext));
98+
99+
Console.ReadKey();
100+
101+
10102
}
11103
}
12104
}

0 commit comments

Comments
 (0)