Exemple #1
0
        private string Decryt(string data)
        {
            string dataDecrypted = string.Empty;

            try
            {
                if (data != null)
                {
                    if (data.Trim().Length != 0)
                    {
                        dataDecrypted = aes.Decrypt(data);
                    }
                }
            }
            catch
            {
                dataDecrypted = "Error en Decrypt";
            }
            return(dataDecrypted);
        }
Exemple #2
0
    public static DbEntry Read(FileInfo f, bool isUser)
    {
        DbEntry entry = new DbEntry()
        {
            filename   = f.Name,
            hash       = new Hashtable(),
            isUser     = isUser,
            write_lock = new object()
        };

        try {
            if (f.Length < 2)
            {
                throw new Exception("null file: " + f.FullName);
            }
            byte[] bytes = File.ReadAllBytes(f.FullName);

            string   plain = Encoding.UTF8.GetString(CryptoAes.Decrypt(bytes, Program.DB_KEY_A, Program.DB_KEY_B));
            string[] split = plain.Split((char)127);

            entry.hash.Add(".FILENAME", new string[] { f.Name, "", "" });

            for (int i = 0; i < split.Length - 3; i += 4)
            {
                if (!entry.hash.ContainsKey(split[i]))
                {
                    entry.hash.Add(split[i], new string[] { split[i + 1], split[i + 2], split[i + 3] });
                }
            }
        } catch (IOException ex) {
            entry.hash = null;
            Logging.Err(ex);
        } catch (Exception ex) {
            entry.hash = null;
            Logging.Err(ex);
        }

        return(entry);
    }
Exemple #3
0
        static void Serve(TcpClient client)
        {
            byte[] buffer = new byte[client.ReceiveBufferSize];
            int    length;

            try {
                length = client.Client.Receive(buffer);
                Array.Resize(ref buffer, length);
            } catch {
                client.Close();
                return;
            }

            byte[] decrypt = CryptoAes.Decrypt(buffer, Main.bKey, Main.bIv);
            if (decrypt is null)
            {
                client.Close();
                return;
            }

            Action(System.Text.Encoding.UTF8.GetString(decrypt));

            client.Close();
        }
        static void Main(string[] args)
        {
            var str = CryptoSecureString.ToSecureString(new[] { 'h', 'i', 'r', 'o' });

            char [] charArray = CryptoSecureString.CharacterData(str);

            string unsecure = CryptoSecureString.ConvertToUnsecureString(str);

            var rsaParameters = new CryptoRSAParameters();

            rsaParameters.GenerateKeys();

            Console.WriteLine($"Random {Convert.ToBase64String(CryptoRandom.Generate(32))}");

            Console.WriteLine("-------------------------------------------");

            var message      = "Hiro universe";
            var messageBytes = Encoding.UTF8.GetBytes(message);

            Console.WriteLine($"MD5 message: {message} hash: {Convert.ToBase64String(CryptoHash.Md5(messageBytes))}");
            Console.WriteLine($"SHA1 message: {message} hash: {Convert.ToBase64String(CryptoHash.Sha1(messageBytes))}");
            Console.WriteLine($"SHA256 message: {message} hash: {Convert.ToBase64String(CryptoHash.Sha256(messageBytes))}");
            Console.WriteLine($"SHA512 message: {message} hash: {Convert.ToBase64String(CryptoHash.Sha512(messageBytes))}");

            var key = CryptoRandom.Generate(32);

            Console.WriteLine($"HMAC MD5 message: {message} hash: {Convert.ToBase64String(CryptoHmac.Md5(messageBytes, key))}");
            Console.WriteLine($"HMAC SHA1 message: {message} hash: {Convert.ToBase64String(CryptoHmac.Sha1(messageBytes, key))}");
            Console.WriteLine($"HMAC SHA256 message: {message} hash: {Convert.ToBase64String(CryptoHmac.Sha256(messageBytes, key))}");
            Console.WriteLine($"HMAC SHA512 message: {message} hash: {Convert.ToBase64String(CryptoHmac.Sha512(messageBytes, key))}");

            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("Passsword hash with salt");
            Console.WriteLine($"Password: {message}");
            Console.WriteLine($"Password hashed: {Convert.ToBase64String(CryptoHash.Password(messageBytes))}");

            Console.WriteLine("-------------------------------------------");
            var salt = CryptoRandom.Generate(32);

            Console.WriteLine("Passsword hash with salt - Based Key Derivation Function - PBKDF2");
            Console.WriteLine($"Password: {message}");
            Console.WriteLine($"Password hashed 100 rounds: {Convert.ToBase64String(CryptoHash.Password(messageBytes, salt, 100))}");
            Console.WriteLine($"Password hashed 1000 rounds: {Convert.ToBase64String(CryptoHash.Password(messageBytes, salt, 1000))}");
            Console.WriteLine($"Password hashed 10000 rounds: {Convert.ToBase64String(CryptoHash.Password(messageBytes, salt, 10000))}");

            Console.WriteLine("-------------------------------------------");
            var desKey = CryptoRandom.Generate(8);
            var desIv  = CryptoRandom.Generate(8);
            var desEncryptedMessage = CryptoDes.Encrypt(message, desKey, desIv);
            var desDecryptedMessage = CryptoDes.Decrypt(desEncryptedMessage, desKey, desIv);

            Console.WriteLine("DES Encryption");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Key: {Convert.ToBase64String(desKey)}");
            Console.WriteLine($"IV: {Convert.ToBase64String(desIv)}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(desEncryptedMessage)}");
            Console.WriteLine($"Decrypted: {desDecryptedMessage}");

            Console.WriteLine("-------------------------------------------");
            var tripleDesKey = CryptoRandom.Generate(16);
            var tripleDesIv  = CryptoRandom.Generate(8);
            var tripleDesEncryptedMessage = CryptoTripleDes.Encrypt(message, tripleDesKey, tripleDesIv);
            var tripleDesDecryptedMessage = CryptoTripleDes.Decrypt(tripleDesEncryptedMessage, tripleDesKey, tripleDesIv);

            Console.WriteLine("Triple DES Encryption");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Key: {Convert.ToBase64String(tripleDesKey)}");
            Console.WriteLine($"IV: {Convert.ToBase64String(tripleDesIv)}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(tripleDesEncryptedMessage)}");
            Console.WriteLine($"Decrypted: {tripleDesDecryptedMessage}");

            Console.WriteLine("-------------------------------------------");
            var aesKey = CryptoRandom.Generate(32);
            var aesIv  = CryptoRandom.Generate(16);
            var aesEncryptedMessage = CryptoAes.Encrypt(message, aesKey, aesIv);
            var aesDecryptedMessage = CryptoAes.Decrypt(aesEncryptedMessage, aesKey, aesIv);

            Console.WriteLine("AES Encryption");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Key: {Convert.ToBase64String(aesKey)}");
            Console.WriteLine($"IV: {Convert.ToBase64String(aesIv)}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(aesEncryptedMessage)}");
            Console.WriteLine($"Decrypted: {aesDecryptedMessage}");


            Console.WriteLine("-------------------------------------------");
            var rsaEncryptedMessage = CryptoRsa.Encrypt(messageBytes, rsaParameters.publicKey);
            var rsaDecryptedMessage = CryptoRsa.Decrypt(rsaEncryptedMessage, rsaParameters.privateKey);

            Console.WriteLine("RSA Encryption");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(rsaEncryptedMessage)}");
            Console.WriteLine($"Decrypted: {Encoding.Default.GetString(rsaDecryptedMessage)}");

            Console.WriteLine("-------------------------------------------");
            var hybridEncryptedPacket  = CryptoHybrid.Encrypt(message, rsaParameters.publicKey);
            var hybridDecryptedMessage = CryptoHybrid.Decrypt(hybridEncryptedPacket, rsaParameters.privateKey);

            Console.WriteLine("Hybrid Encryption using AES and RSA");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Encrypted: {Convert.ToBase64String(hybridEncryptedPacket.EncryptedData)}");
            Console.WriteLine($"Decrypted: {hybridDecryptedMessage}");

            Console.WriteLine("-------------------------------------------");
            var hashedMessage = CryptoHash.Sha256(messageBytes);
            var signature     = CryptoDigitalSignature.Sign(hashedMessage, rsaParameters.privateKey);
            var verify        = CryptoDigitalSignature.Verify(hashedMessage, signature, rsaParameters.publicKey);

            Console.WriteLine("Digital Signature");
            Console.WriteLine($"Text: {message}");
            Console.WriteLine($"Signature: {Convert.ToBase64String(signature)}");
            Console.WriteLine("Is Verified: " + (verify ? "true" : "false"));

            Console.WriteLine("-------------------------------------------");
            try {
                var hybridSignatureEncryptedPacket  = CryptoHybridIntegrity.Encrypt(message, rsaParameters);
                var hybridSignatureDecryptedMessage = CryptoHybridIntegrity.Decrypt(hybridSignatureEncryptedPacket, rsaParameters);
                Console.WriteLine("Hybrid encryption with digital signature");
                Console.WriteLine($"Text: {message}");
                Console.WriteLine($"Signature: {Convert.ToBase64String(hybridSignatureEncryptedPacket.Signature)}");
                Console.WriteLine($"Encrypted: {Convert.ToBase64String(hybridSignatureEncryptedPacket.EncryptedData)}");
                Console.WriteLine($"Decrypted: {hybridSignatureDecryptedMessage}");
            }
            catch (CryptographicException ex)
            {
                Console.WriteLine("Error : " + ex.Message);
            }
        }