Esempio n. 1
0
        /// <summary>
        /// Deserializes a given class from file, using a decryption key.
        /// </summary>
        /// <returns>The deserialized object.</returns>
        /// <param name="loc">Location of the file.</param>
        /// <param name="decryptKey">Decryption key.</param>
        /// <typeparam name="T">The type of the serialized object in the file.</typeparam>
        public static T FromFile <T>(string loc, Key decryptKey)
        {
            //Reading bytes from file.
            byte[] encryptedBytes = File.ReadAllBytes(loc);

            //Decrypting with given key.
            byte[] decryptedBytes = RSA.DecryptBytes(encryptedBytes, decryptKey);

            //Converting to text, deserializing.
            string serialized = Encoding.ASCII.GetString(decryptedBytes);

            return((T)JsonConvert.DeserializeObject(serialized));
        }
Esempio n. 2
0
        public void EncryptionTest()
        {
            // Create RSA Keys
            RSA rsa = new RSA(RSA.KEY_SIZE);

            string pubKey  = TextConversion.Base64Encode(rsa.GetPublicKey());
            string privKey = TextConversion.Base64Encode(rsa.GetPrivateKey());

            byte[] dataToEncrypt = new byte[300];
            RandomNumberGenerator.Fill(dataToEncrypt);

            // Create AES Keys
            byte[] key = new byte[16];
            RandomNumberGenerator.Fill(key);

            byte[] aad = new byte[32];
            RandomNumberGenerator.Fill(aad);

            // SECURE GCM Key
            // Encrypt Key for AES with RSA and check if given and decrypted keys are equal
            byte[] encryptedAeskey = RSA.EncryptBytes(key, RSA.SetKeyFromString(pubKey));
            byte[] decryptedAesKey = RSA.DecryptBytes(encryptedAeskey, RSA.SetKeyFromString(privKey));
            Assert.IsTrue(key.SequenceEqual(decryptedAesKey));

            // ENCRYPT DATA
            // Encrypt with key and decrypt with DecryptedAesKey
            byte[] encryptedData = GCM.Encrypt(dataToEncrypt, key, aad);
            byte[] decryptedData = GCM.Decrypt(encryptedData, decryptedAesKey, aad);

            Assert.IsTrue(dataToEncrypt.SequenceEqual(decryptedData));

            string stringToEncrypt = "Ahoj svet";

            byte[] encryptedStringData = GCM.Encrypt(Encoding.UTF8.GetBytes(stringToEncrypt), key, aad);
            byte[] decryptedStringData = GCM.Decrypt(encryptedStringData, decryptedAesKey, aad);

            Assert.AreEqual(stringToEncrypt, Encoding.UTF8.GetString(decryptedStringData));
        }
Esempio n. 3
0
        //Testing the key generation method to check valid keys are returned.
        public bool RSAKeyGen()
        {
            //Generating a test 1024 bit keypair.
            KeyPair keys = RSA.GenerateKeyPair(1024);

            //Setting up a payload, testing for perfect encrypt/decrypt.
            byte[] package   = { 0xFF, 0x2A, 0x00, 0x00, 0x01, 0x00 };
            byte[] encrypted = RSA.EncryptBytes(package, keys.public_);
            byte[] decrypted = RSA.DecryptBytes(encrypted, keys.private_);

            //Checking decrypt.
            if (decrypted.SequenceEqual(package))
            {
                return(true);
            }
            else
            {
                Console.WriteLine("Returned bytes from RSA encrypt/decrypt differed, resulting in failure.");
                Console.WriteLine("ORIGINAL BYTES: " + Utils.RawByteString(package));
                Console.WriteLine("FAILED BYTES:" + Utils.RawByteString(decrypted));
                return(false);
            }
        }
Esempio n. 4
0
        //Testing the encryption and decryption method with a randomly generated long set of 1020 bits.
        public bool RSACoverageTest()
        {
            KeyPair keys = RSA.GenerateKeyPair(1024);

            //Randomly filling a 126 byte array. (leaving some free space, n can sometimes (1%) be a bit smaller than 128 bytes).
            byte[] b    = new byte[126];
            Random rand = new Random(Environment.TickCount);

            rand.NextBytes(b);

            //Encrypting and decrypting, testing reliability.
            byte[] encrypted = RSA.EncryptBytes(b, keys.public_);
            byte[] decrypted = RSA.DecryptBytes(encrypted, keys.private_);
            if (decrypted.SequenceEqual(b))
            {
                Console.WriteLine("SUCCESS: Randomized RSA payload encrypted and decrypted successfully.");
                return(true);
            }
            else
            {
                Console.WriteLine("Randomized RSA payload failed to encrypt and decrypt without data loss. Check SharpRSA package for errors.");
                return(false);
            }
        }