Click here to Skip to main content
15,892,839 members
Home / Discussions / C#
   

C#

 
Questionhow to read data from gps (TK103) in .net Pin
Member 454542811-Oct-15 6:34
professionalMember 454542811-Oct-15 6:34 
AnswerRe: how to read data from gps (TK103) in .net Pin
Dave Kreskowiak11-Oct-15 11:09
mveDave Kreskowiak11-Oct-15 11:09 
AnswerRe: how to read data from gps (TK103) in .net Pin
ZurdoDev13-Oct-15 2:59
professionalZurdoDev13-Oct-15 2:59 
Questionwant to add xlsm file in project and automatic copy the data of xlsm file in other side Pin
Member 1204967211-Oct-15 1:20
Member 1204967211-Oct-15 1:20 
AnswerRe: want to add xlsm file in project and automatic copy the data of xlsm file in other side Pin
OriginalGriff11-Oct-15 1:53
mveOriginalGriff11-Oct-15 1:53 
SuggestionRe: want to add xlsm file in project and automatic copy the data of xlsm file in other side Pin
Kornfeld Eliyahu Peter11-Oct-15 2:44
professionalKornfeld Eliyahu Peter11-Oct-15 2:44 
General2 texbox value transfar ti cobobox Pin
Member 1204736710-Oct-15 17:21
Member 1204736710-Oct-15 17:21 
QuestionIs this a secure way to encrypt? Pin
Jassim Rahma10-Oct-15 11:27
Jassim Rahma10-Oct-15 11:27 
Hi,

I am planning encryption and I want to know if below way I found is secure to encrypt and decypt or not? and do I need to add a slat to it or it's already secure?
C#
using System;
//System.IO we include for the access to the memory stream
using System.IO;
//The big using here, this includes everything you could want to do with cryptography.
//Including hashing and public key or semetric key cryptography.
//Gives us acess to cryptostream, the crypto transforms and the rijndael class.
using System.Security.Cryptography;

namespace AES_Tutuorial
{
    class Crypto_tut
    {
        static void Main()
        {

            string Plain_Text;
            string Decrypted;
            string Encrypted_Text;
            byte[] Encrypted_Bytes;

            //This class here the Rijndael is what will have most all of the methods we need to do aes encryption.
            //When this is called it will create both a key and Initialization Vector to use.
            RijndaelManaged Crypto = new RijndaelManaged();

            //This is just here to convert the Encrypted byte array to a string for viewing purposes.
            System.Text.UTF8Encoding UTF = new System.Text.UTF8Encoding();

            Console.WriteLine("Please put in the text to be encrypted.");
            Plain_Text = Console.ReadLine();

            try
            {

                Encrypted_Bytes = encrypt_function(Plain_Text, Crypto.Key, Crypto.IV);
                Encrypted_Text = UTF.GetString(Encrypted_Bytes);
                Decrypted = decrypt_function(Encrypted_Bytes, Crypto.Key, Crypto.IV);

                Console.WriteLine("Start: {0}", Plain_Text);
                Console.WriteLine("Encrypted: {0}", Encrypted_Text);
                Console.WriteLine("Decrypted: {0}", Decrypted);

            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadKey();

        }
        private static byte[] encrypt_function(string Plain_Text, byte[] Key, byte[] IV)
        {

            RijndaelManaged Crypto = null;
            MemoryStream MemStream = null;
            //I crypto transform is used to perform the actual decryption vs encryption, hash function are a version of crypto transforms.
            ICryptoTransform Encryptor = null;
            //Crypto streams allow for encryption in memory.
            CryptoStream Crypto_Stream = null;

            System.Text.UTF8Encoding Byte_Transform = new System.Text.UTF8Encoding();

            //Just grabbing the bytes since most crypto functions need bytes.
            byte[] PlainBytes = Byte_Transform.GetBytes(Plain_Text);

            try
            {
                Crypto = new RijndaelManaged();
                Crypto.Key = Key;
                Crypto.IV = IV;

                MemStream = new MemoryStream();

                //Calling the method create encryptor method Needs both the Key and IV these have to be from the original Rijndael call
                //If these are changed nothing will work right.
                Encryptor = Crypto.CreateEncryptor(Crypto.Key, Crypto.IV);

                //The big parameter here is the cryptomode.write, you are writing the data to memory to perform the transformation
                Crypto_Stream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write);

                //The method write takes three params the data to be written (in bytes) the offset value (int) and the length of the stream (int)
                Crypto_Stream.Write(PlainBytes, 0, PlainBytes.Length);

            }
            finally
            {
                //if the crypto blocks are not clear lets make sure the data is gone
                if (Crypto != null)
                    Crypto.Clear();
                //Close because of my need to close things then done.
                Crypto_Stream.Close();
            }
            //Return the memory byte array
            return MemStream.ToArray();
           }

        private static string decrypt_function(byte[] Cipher_Text, byte[] Key, byte[] IV)
        {
            RijndaelManaged Crypto = null;
            MemoryStream MemStream = null;
            ICryptoTransform Decryptor = null;
            CryptoStream Crypto_Stream = null;
            StreamReader Stream_Read = null;
             string Plain_Text;

             try
             {
                 Crypto = new RijndaelManaged();
                 Crypto.Key = Key;
                 Crypto.IV = IV;

                 MemStream   = new MemoryStream(Cipher_Text);

                 //Create Decryptor make sure if you are decrypting that this is here and you did not copy paste encryptor.
                 Decryptor = Crypto.CreateDecryptor(Crypto.Key, Crypto.IV);

                 //This is different from the encryption look at the mode make sure you are reading from the stream.
                 Crypto_Stream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);

                 //I used the stream reader here because the ReadToEnd method is easy and because it return a string, also easy.
                 Stream_Read = new StreamReader(Crypto_Stream);
                 Plain_Text = Stream_Read.ReadToEnd();
             }
            finally
            {
                if (Crypto != null)
                    Crypto.Clear();

                MemStream.Flush();
                MemStream.Close();

            }
            return Plain_Text;
        }

    }
}

Thanks,
Jassim[^]

Technology News @ www.JassimRahma.com

AnswerRe: Is this a secure way to encrypt? Pin
ZurdoDev12-Oct-15 5:26
professionalZurdoDev12-Oct-15 5:26 
QuestionFill Image in tile mode not working fine form Zoom Pin
Le@rner9-Oct-15 2:52
Le@rner9-Oct-15 2:52 
AnswerRe: Fill Image in tile mode not working fine form Zoom Pin
Matt T Heffron9-Oct-15 8:31
professionalMatt T Heffron9-Oct-15 8:31 
QuestionCan't add reference to project, Visual studio Pin
Member 120456929-Oct-15 0:00
Member 120456929-Oct-15 0:00 
QuestionRe: Can't add reference to project, Visual studio Pin
CHill609-Oct-15 1:02
mveCHill609-Oct-15 1:02 
AnswerRe: Can't add reference to project, Visual studio Pin
Member 120456929-Oct-15 1:11
Member 120456929-Oct-15 1:11 
GeneralRe: Can't add reference to project, Visual studio Pin
CHill609-Oct-15 1:32
mveCHill609-Oct-15 1:32 
GeneralRe: Can't add reference to project, Visual studio Pin
Member 120456929-Oct-15 1:52
Member 120456929-Oct-15 1:52 
GeneralRe: Can't add reference to project, Visual studio Pin
CHill609-Oct-15 2:03
mveCHill609-Oct-15 2:03 
GeneralRe: Can't add reference to project, Visual studio Pin
Member 120456929-Oct-15 2:05
Member 120456929-Oct-15 2:05 
GeneralRe: Can't add reference to project, Visual studio Pin
CHill609-Oct-15 2:13
mveCHill609-Oct-15 2:13 
GeneralRe: Can't add reference to project, Visual studio Pin
Member 120456929-Oct-15 2:32
Member 120456929-Oct-15 2:32 
GeneralRe: Can't add reference to project, Visual studio Pin
CHill609-Oct-15 2:44
mveCHill609-Oct-15 2:44 
GeneralRe: Can't add reference to project, Visual studio Pin
Dave Kreskowiak9-Oct-15 15:02
mveDave Kreskowiak9-Oct-15 15:02 
Questionc# webbrowser control to filter all xmlhttprequest Pin
JueYingCh8-Oct-15 23:00
JueYingCh8-Oct-15 23:00 
GeneralProject Explanation Pin
c31777968-Oct-15 14:59
c31777968-Oct-15 14:59 
GeneralRe: Project Explanation Pin
BillWoodruff8-Oct-15 19:49
professionalBillWoodruff8-Oct-15 19:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.