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; }
} //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
//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); }
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); }
/// <summary> /// </summary> /// <returns> возвращает ключ в виде массива байтов, длина ключа 256 </returns> public byte[] GenerateKey() { using (Rijndael AES = Rijndael.Create()) { AES.GenerateKey(); return(AES.Key); } }
public override void GenerateKey() { Rijndael rijndael = Rijndael.Create(); rijndael.KeySize = this.KeySize; rijndael.Padding = PaddingMode.None; rijndael.GenerateKey(); this._key = rijndael; }
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); } }
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); }
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; }
//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); }
// 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); } }
/// <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)); }
// 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; } }
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); }
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); }
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; } }
//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); }
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; } }
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); }
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); }
} // 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
public override void GenerateKeyAndIV() { _rijn.GenerateIV(); _rijn.GenerateKey(); }
static ZoneTicket() { _rijndael.GenerateKey(); _rijndael.GenerateIV(); }
public string CreateRijndaelKey() { rijndael.GenerateKey(); return(ByteToString(rijndael.Key)); }
static SecureXml() { rKey.GenerateKey(); rKey.GenerateIV(); }
public void generate() { aes.GenerateKey(); aes.GenerateIV(); }