Ejemplo n.º 1
0
    public RAOPClient( string Host )
    {
        host = Host;

        volume = VOLUME_DEF;
        ajstatus = JACK_STATUS_DISCONNECTED;
        ajtype = JACK_TYPE_ANALOG;

        nfi = new CultureInfo( "en-US" ).NumberFormat;

        alg = Rijndael.Create();
        alg.Mode = CipherMode.CBC;
        alg.Padding = PaddingMode.None;
        alg.KeySize = 128;

        alg.GenerateKey();
        alg.GenerateIV();

        int i = host.LastIndexOf( '.' );
        string hostnet = host.Substring( 0, i );
        IPHostEntry iphe = Dns.GetHostEntry(Dns.GetHostName());//Dns.GetHostByName( Dns.GetHostName() );
        foreach( IPAddress ipaddr in iphe.AddressList )
        {
            string s = ipaddr.ToString();
            if( s.StartsWith( hostnet ) )
            {
                local = s;
                break;
            }
        }

        if( local == null )
            local = Host;
    }
Ejemplo n.º 2
0
        } //compute hash from arguments and return hash value as string

        private static string GetRijndaelHash(string text)
        {
            //create variables for computing hash and making it a string
            UnicodeEncoding UniCode = new UnicodeEncoding();

            byte[]   HashResult;
            byte[]   msg        = UniCode.GetBytes(text);
            Rijndael hashString = RijndaelManaged.Create();

            //generate a weak KEY and Initialization Vector for the hash algorithm
            hashString.GenerateKey();
            hashString.GenerateIV();
            ICryptoTransform encryptor = hashString.CreateEncryptor(hashString.Key, hashString.IV);
            string           Str       = "";

            //compute hash with Rijndael module and format output as string
            //convert bytes in HashResult to string values
            HashResult = encryptor.TransformFinalBlock(msg, 0, msg.Length);
            foreach (byte x in HashResult)
            {
                Str += String.Format("{0:x2}", x);
            }

            //clear excess resource usage
            hashString.Clear();
            return(Str);
        } //compute hash from arguments and return hash value as string
Ejemplo n.º 3
0
        //With the AsymmetricEncrypt we are going to encrypt the symmetric keys

        public static MemoryStream HybridEnryption(MemoryStream clearFile, string publicKey)
        {
            Rijndael myAlg = Rijndael.Create();

            myAlg.GenerateKey(); //secretkey
            myAlg.GenerateIV();  //iv

            var key = myAlg.Key;
            var iv  = myAlg.IV;

            byte[] fileInBytes   = clearFile.ToArray();
            var    encryptedFile = SymmetricEncrypt(fileInBytes, iv, key);

            byte[] encryptedKey = AsymmetricEncrypt(key, publicKey);
            byte[] encryptedIv  = AsymmetricEncrypt(iv, publicKey);

            MemoryStream msOut = new MemoryStream();

            msOut.Write(encryptedKey, 0, encryptedKey.Length);
            msOut.Write(encryptedIv, 0, encryptedIv.Length);

            MemoryStream encryptedFileContent = new MemoryStream(encryptedFile);

            encryptedFileContent.Position = 0;
            encryptedFileContent.CopyTo(msOut);

            return(msOut);
        }
Ejemplo n.º 4
0
        private static void TestShims(Rijndael alg)
        {
            alg.BlockSize = 128;
            Assert.Equal(128, alg.BlockSize);

            var emptyIV = new byte[alg.BlockSize / 8];

            alg.IV = emptyIV;
            Assert.Equal(emptyIV, alg.IV);
            alg.GenerateIV();
            Assert.NotEqual(emptyIV, alg.IV);

            var emptyKey = new byte[alg.KeySize / 8];

            alg.Key = emptyKey;
            Assert.Equal(emptyKey, alg.Key);
            alg.GenerateKey();
            Assert.NotEqual(emptyKey, alg.Key);

            alg.KeySize = 128;
            Assert.Equal(128, alg.KeySize);

            alg.Mode = CipherMode.ECB;
            Assert.Equal(CipherMode.ECB, alg.Mode);

            alg.Padding = PaddingMode.PKCS7;
            Assert.Equal(PaddingMode.PKCS7, alg.Padding);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// </summary>
 /// <returns> возвращает ключ в виде массива байтов, длина ключа 256 </returns>
 public byte[] GenerateKey()
 {
     using (Rijndael AES = Rijndael.Create())
     {
         AES.GenerateKey();
         return(AES.Key);
     }
 }
Ejemplo n.º 6
0
        public override void GenerateKey()
        {
            Rijndael rijndael = Rijndael.Create();

            rijndael.KeySize = this.KeySize;
            rijndael.Padding = PaddingMode.None;
            rijndael.GenerateKey();
            this._key = rijndael;
        }
Ejemplo n.º 7
0
 public static byte[] CreateAesKey()
 {
     using (Rijndael rijndael = Rijndael.Create())
     {
         rijndael.Mode    = CipherMode.CBC;
         rijndael.KeySize = 128;
         rijndael.Padding = PaddingMode.None;
         rijndael.GenerateKey();
         return(rijndael.Key);
     }
 }
Ejemplo n.º 8
0
        public static string CreateKey()
        {
            Rijndael rijAlg = Rijndael.Create();

            rijAlg.KeySize = 256;
            rijAlg.Mode    = CipherMode.CBC;
            rijAlg.Padding = PaddingMode.PKCS7;
            rijAlg.GenerateKey();
            string key = Convert.ToBase64String(rijAlg.Key);

            return(key);
        }
Ejemplo n.º 9
0
        public void GenerateKeys(int keySize)
        {
            rijndael           = Rijndael.Create();
            rijndael.BlockSize = 128;
            rijndael.KeySize   = keySize;
            rijndael.GenerateIV();
            rijndael.GenerateKey();

            this.blockSize = rijndael.BlockSize;
            this.keySize   = rijndael.KeySize;
            this.key       = rijndael.Key;
            this.iv        = rijndael.IV;
        }
Ejemplo n.º 10
0
        //drawback of this method is that you have to save the key and iv somwhere because you 'll need them for the decrypt
        public static SymmetricKeys GenerateSymmetricKeys()
        {
            Rijndael myAlg = Rijndael.Create();

            myAlg.GenerateKey(); myAlg.GenerateIV();

            SymmetricKeys keys = new SymmetricKeys()
            {
                Key = myAlg.Key,
                Iv  = myAlg.IV
            };

            return(keys);
        }
Ejemplo n.º 11
0
        // Generate random AES key and IV
        public static string[] GenerateAESKeyAndIV(int length)
        {
            using (Rijndael myAes = Rijndael.Create())
            {
                myAes.KeySize = length;

                myAes.GenerateKey();
                myAes.GenerateIV();

                string[] keyAndIV = new string[2];
                keyAndIV[0] = Convert.ToBase64String(myAes.Key);
                keyAndIV[1] = Convert.ToBase64String(myAes.IV);

                return(keyAndIV);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 获得密钥数组
        /// </summary>
        /// <returns>密钥数组</returns>
        private byte[] GetLegalKey()
        {
            string result = key;

            mCrypto.GenerateKey();
            byte[] keyBytes  = mCrypto.Key;
            int    keyLength = keyBytes.Length;

            if (result.Length > keyLength)
            {
                result = result.Substring(0, keyLength);
            }
            else if (result.Length < keyLength)
            {
                result = result.PadRight(keyLength, ' ');
            }
            return(ASCIIEncoding.ASCII.GetBytes(result));
        }
Ejemplo n.º 13
0
            // This encrypts the passed data
            internal void SetData(string data)
            {
                if (data != null)
                {
                    if (rijndael == null)
                    {
                        rijndael = Rijndael.Create();
                        rijndael.GenerateKey();                                         // Not sure if this is necessary. Is
                        rijndael.GenerateIV();                                          // this done by default in the ctor?
                    }

                    ICryptoTransform encryptor = rijndael.CreateEncryptor();
                    encryptedData = encryptor.TransformFinalBlock(ToBytes(data), 0, (data.Length) * 2);
                }
                else
                {
                    encryptedData = null;
                }
            }
Ejemplo n.º 14
0
        static string Encrypt(string stringToEncrypt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce aliquam magna ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
        {
            string retValue = "";

            Rijndael rij = Rijndael.Create();

            rij.GenerateKey();
            rij.GenerateIV();

            ICryptoTransform transformer = rij.CreateEncryptor();
            MemoryStream     ms          = new MemoryStream();
            CryptoStream     cs          = new CryptoStream(ms, transformer, CryptoStreamMode.Write);
            StreamWriter     sw          = new StreamWriter(cs);

            sw.Write(stringToEncrypt);

            retValue = Encoding.UTF8.GetString(ms.ToArray());

            return(retValue);
        }
Ejemplo n.º 15
0
        public static MemoryStream HybridEncrypt(MemoryStream clearFile, string publicKey)
        {
            //1. Generate the symmetric keys
            Rijndael myAlg = Rijndael.Create();

            myAlg.GenerateKey();
            myAlg.GenerateIV();
            var key = myAlg.Key;
            var iv  = myAlg.IV;

            //2. Encrypting the clearFile using Symmetric Encryption

            SymmetricKeys keys = new SymmetricKeys();

            keys.SecretKey = key;
            keys.Iv        = iv;


            //3. Asymmetrically encrypt using the public key, the sym key and iv above
            //string keyS = Convert.ToBase64String(key);
            byte[] encryptedKey = AsymmetricEnrypt(key, publicKey);

            //4. Store the above encrypted data n one file
            byte[] encryptedIv = AsymmetricEnrypt(iv, publicKey);

            MemoryStream msOut = new MemoryStream();

            msOut.Write(encryptedKey, 0, encryptedKey.Length);
            msOut.Write(encryptedIv, 0, encryptedIv.Length);

            byte[] bytes          = clearFile.ToArray();
            byte[] encryptedBytes = SymmetricEnryption(bytes, keys);

            MemoryStream encryptedfileContent = new MemoryStream(encryptedBytes);

            encryptedfileContent.Position = 0;
            encryptedfileContent.CopyTo(msOut);

            return(msOut);
        }
Ejemplo n.º 16
0
        private void encode_Rijndael_EBC_Click(object sender, EventArgs e)
        {
            DateTime then      = DateTime.Now;
            String   plainText = input5.Text;

            try
            {
                using (Rijndael rijAlg = Rijndael.Create())
                {
                    rijAlg.GenerateKey();
                    Key = rijAlg.Key;
                    rijAlg.GenerateIV();
                    IV = rijAlg.IV;

                    ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                swEncrypt.Write(plainText);
                            }
                            cipherbytes = msEncrypt.ToArray();
                        }
                    }
                }
                input6.Text = Encoding.UTF8.GetString(cipherbytes);
                delta3.Text = (DateTime.Now - then).TotalSeconds.ToString() + "сек";
            }
            catch (CryptographicException ex)
            {
                Console.WriteLine("A Cryptographic error occurred: {0}", ex.Message);
                return;
            }
        }
Ejemplo n.º 17
0
        //clear = unencrypted
        public static MemoryStream HybridEncrypt(MemoryStream clearFile, string publicKey)
        {
            //preparation:
            //              you need to fetch from db the public pertaining to the uploading/logged in user

            //1. Generate the symmetric keys
            Rijndael myAlg = Rijndael.Create();

            myAlg.GenerateKey(); myAlg.GenerateIV();
            var key = myAlg.Key; var iv = myAlg.IV;

            //2. Encrypting the clearFile using Symmetric Encryption
            //var encryptedBytes =  SymmetricEncrypt(clearFileAsBytes, key, iv)

            //3. Asymmetrically encrypt using the public key, the symm key and iv above
            string keyAsString          = Convert.ToBase64String(key);
            string encryptedKeyAsString = AsymmetricEncrypt(keyAsString, publicKey);


            //4. store the above encryted data n one file
            byte[] encrtypedKey = Convert.FromBase64String(encryptedKeyAsString);
            // byte[] encyptedIv;
            byte[] encryptedBytes; //this the uploaded file content

            MemoryStream msOut = new MemoryStream();

            msOut.Write(encrtypedKey, 0, encrtypedKey.Length);
            // msOut.Write(encyptedIv, 0, encyptedIv.Length);

            //encryptedBytes  [234alsdjfalsdkfj;alskdfjalsdkjfalskdjflaskdjflaskdjflasdjflaksjdflaksdjflaksd;jflaskdjaskldjflsdkfj]

            /*  MemoryStream encryptedfileContent = new MemoryStream(encryptedBytes);
             * encryptedfileContent.Position = 0;
             * encryptedfileContent.CopyTo(msOut);
             */
            return(msOut);
        }
Ejemplo n.º 18
0
    public RAOPClient(string Host)
    {
        host = Host;

        volume   = VOLUME_DEF;
        ajstatus = JACK_STATUS_DISCONNECTED;
        ajtype   = JACK_TYPE_ANALOG;

        nfi = new CultureInfo("en-US").NumberFormat;

        alg         = Rijndael.Create();
        alg.Mode    = CipherMode.CBC;
        alg.Padding = PaddingMode.None;
        alg.KeySize = 128;

        alg.GenerateKey();
        alg.GenerateIV();

        int         i       = host.LastIndexOf('.');
        string      hostnet = host.Substring(0, i);
        IPHostEntry iphe    = Dns.GetHostEntry(Dns.GetHostName());//Dns.GetHostByName( Dns.GetHostName() );

        foreach (IPAddress ipaddr in iphe.AddressList)
        {
            string s = ipaddr.ToString();
            if (s.StartsWith(hostnet))
            {
                local = s;
                break;
            }
        }

        if (local == null)
        {
            local = Host;
        }
    }
Ejemplo n.º 19
0
        public static string Encrypt(string inputString = "This is an encripted string and you will never see it...")
        {
            var rtnValue = "";

            using (Rijndael crypt = Rijndael.Create())
            {
                crypt.GenerateKey();
                crypt.GenerateIV();

                ICryptoTransform transformer = crypt.CreateEncryptor();
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, transformer, CryptoStreamMode.Write))
                    {
                        using (var wr = new StreamWriter(cs))
                        {
                            wr.Write(inputString);
                        }
                        rtnValue = System.Text.Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
            }
            return(rtnValue);
        }
Ejemplo n.º 20
0
        public async static Task <String> Encrypt(String input = "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTest")
        {
            var Value = "";

            using (Rijndael cryPt = Rijndael.Create())
            {
                cryPt.GenerateKey();
                cryPt.GenerateIV();

                ICryptoTransform transform = cryPt.CreateEncryptor();
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                    {
                        using (var wr = new StreamWriter(cs))
                        {
                            await wr.WriteAsync(input);
                        }
                    }
                    Value = System.Text.Encoding.UTF8.GetString(ms.ToArray());
                }
            }
            return(Value);
        }
Ejemplo n.º 21
0
        } // end of Send_Info()

        //function to generate Key for AES 256
        private void Generate_Symetric_Key()
        {
            AES.GenerateKey(); // Generate the AES Key
            AES.GenerateIV();  // Generate the AES IV for CBC mode
        }//end of Genetare_Symetric_Key
Ejemplo n.º 22
0
 public override void GenerateKeyAndIV()
 {
     _rijn.GenerateIV();
     _rijn.GenerateKey();
 }
Ejemplo n.º 23
0
 static ZoneTicket()
 {
     _rijndael.GenerateKey();
     _rijndael.GenerateIV();
 }
Ejemplo n.º 24
0
 public string CreateRijndaelKey()
 {
     rijndael.GenerateKey();
     return(ByteToString(rijndael.Key));
 }
 static SecureXml()
 {
     rKey.GenerateKey();
     rKey.GenerateIV();
 }
Ejemplo n.º 26
0
 public void generate()
 {
     aes.GenerateKey();
     aes.GenerateIV();
 }