-
Notifications
You must be signed in to change notification settings - Fork 0
/
RsaAndShaInUse.cs
95 lines (80 loc) · 3.89 KB
/
RsaAndShaInUse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace RsaAndShaInUse
{
class Program
{
const string publicKeyFileLocation = @"C:\Users\x\Documents\PublicKey.pem";
const string privateKeyFileLocation = @"C:\Users\x\Documents\PrivateKey.pem";
static void Main(string[] args)
{
//get user input & convert to bytes
Console.WriteLine(" * type something to encrypt");
string input = Console.ReadLine();
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] rsaEncryptedBytes = RsaEncrypt(inputBytes);
string rsaEncryptedString = Encoding.UTF8.GetString(rsaEncryptedBytes);
Console.WriteLine("\n * RSA Encrypted text:\n" + rsaEncryptedString);
byte[] rsaDecryptedBytes = RsaDecrypt(rsaEncryptedBytes);
string rsaDecryptedString = Encoding.UTF8.GetString(rsaDecryptedBytes);
Console.WriteLine("\n * Decrypted RSA text:\n" + rsaDecryptedString);
byte[] shaEncryptedBytes = ShaEncrypt(inputBytes);
Console.WriteLine("\n * SHA256 hash:\n" + ShaStringBuilder(shaEncryptedBytes));
string hashConvertedToString = ShaStringBuilder(shaEncryptedBytes);
byte[] convertedHashBackToBytes = Encoding.UTF8.GetBytes(hashConvertedToString);
byte[] reHash = ShaEncrypt(convertedHashBackToBytes);
Console.WriteLine("\n * Rehashed hash with SHA256:\n" + ShaStringBuilder(reHash));
byte[] shaRsaEncrytedBytes = RsaEncrypt(shaEncryptedBytes);
string shaRsaEncrytedString = Encoding.UTF8.GetString(shaRsaEncrytedBytes);
Console.WriteLine("\n * Hash with SHA256 and THEN encrypt with RSA:\n" + shaRsaEncrytedString);
byte[] shaRsaDecryptedBytes = RsaDecrypt(shaRsaEncrytedBytes);
Console.WriteLine("\n * Decrypted RSA text with SHA256 hash left over:\n" + ShaStringBuilder(shaRsaDecryptedBytes));
//program has ended
Console.ReadLine();
}
//Converts SHA256 byte[] to string
static string ShaStringBuilder(byte[] textBytes)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (byte i in textBytes)
{
stringBuilder.Append(i.ToString("x2"));
}
return stringBuilder.ToString();
}
static byte[] ShaEncrypt(byte[] textBytes)
{
SHA256Managed hashString = new SHA256Managed();
return hashString.ComputeHash(textBytes);
}
static byte[] RsaEncrypt(byte[] textBytes)
{
//get PublicKey pem File
PemReader KeyTextReader = new PemReader(File.OpenText(publicKeyFileLocation));
RsaKeyParameters publicKey = KeyTextReader.ReadObject() as RsaKeyParameters;
//encrypt byte array
IAsymmetricBlockCipher encryptCipher = new OaepEncoding(new RsaEngine());
encryptCipher.Init(true, publicKey);
return encryptCipher.ProcessBlock(textBytes, 0, textBytes.Length);
}
static byte[] RsaDecrypt(byte[] ct)
{
//get private key pem file
AsymmetricCipherKeyPair keyPair;
StreamReader reader = File.OpenText(privateKeyFileLocation);
keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject();
RsaKeyParameters privateKey = keyPair.Private as RsaKeyParameters;
//decrypt byte array
IAsymmetricBlockCipher decryptCipher = new OaepEncoding(new RsaEngine());
decryptCipher.Init(false, privateKey);
return decryptCipher.ProcessBlock(ct, 0, ct.Length);
}
}
}