public void Test(KeyIvModel model) { //Test(model.IV); AesKrypto aes = new AesKrypto(System.Security.Cryptography.CipherMode.CBC, System.Security.Cryptography.PaddingMode.None, 256); byte[] IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] IV2 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; byte[] key = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] text = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] text2 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; //model.IV = IV; //model.Key = key; var c1 = aes.EncryptToByte(text, model); var p1 = aes.DecryptFromByte(c1, model); AddOne(model.IV); var c2 = aes.EncryptToByte(text2, model); var p2 = aes.DecryptFromByte(c2, model); for (int i = 0; i < c1.Length; i++) { Console.Out.WriteLine($"{c1[i]}\t{c2[i]}\t{(byte)(c1[i] ^ c2[i])}"); } }
public void OFB() { AesKrypto aes = new AesKrypto(System.Security.Cryptography.CipherMode.OFB); var x = aes.Encrypt(_test, new KeyIvModel { Key = _Key, IV = _Iv }); var result = aes.Decrypt(x, new KeyIvModel { Key = _Key, IV = _Iv }); Assert.AreEqual(result, _test); }
public static void L1() { KeyStore test = new KeyStore(); test.LoadKeys("siemanko"); test.GetKey(0); AesKrypto aesKrypto = new AesKrypto(); aesKrypto.Encrypt("nowy.txt", "nowy.kz", test.GetKey(0)); aesKrypto.Decrypt("nowy.kz", "nowy1.txt", test.GetKey(0)); }
public bool LoadKeys(string password) { if (_password != password) { return(false); } string file; using (StreamReader sr = new StreamReader(_keyStore)) file = sr.ReadToEnd(); AesKrypto aes = new AesKrypto(); file = aes.Decrypt(file, new KeyIvModel { Key = _Key, IV = _Iv }); var jsonserialize = new JavaScriptSerializer(); _keys = jsonserialize.Deserialize <List <KeyIvModel> >(file); return(true); }
public KeyStore(string password, string keyStore) { _password = password; _keyStore = keyStore; if (!File.Exists(keyStore)) { var jsonSerializer = new JavaScriptSerializer(); var json = jsonSerializer.Serialize(GenerateKeyIvList()); var aes = new AesKrypto(); var file = aes.Encrypt(json, new KeyIvModel { Key = _Key, IV = _Iv }); try { using (StreamWriter sr = new StreamWriter(keyStore)) sr.Write(file); }catch (Exception e) { throw e; } } }
public void Exe2(KeyIvModel model) { AesKrypto aes = new AesKrypto(System.Security.Cryptography.CipherMode.CBC, System.Security.Cryptography.PaddingMode.None, 256); var rand = new Random(); byte[] text = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] text2 = new byte[] { 13, 65, 123, 98, 8, 4, 65, 15, 32, 47, 98, 14, 200, 10, 0, 128 }; byte[] newcipher2; model.IV = iv; var newcipher1 = aes.EncryptToByte(text, model); //for (int i = 0; i < 100; i++) //{ // AddOne(text); // AddOne(model.IV); // var temp = aes.EncryptToByte(text, model); // if (!Check(newcipher1, temp)) // { // for (int j = 0; j < temp.Length; j++) // { // Console.Out.WriteLine($"{newcipher1[j]}\t{temp[j]}\t{(byte)(newcipher1[j] ^ temp[j])}"); // } // Console.Out.WriteLine(); // } //} do { AddOne(text); AddOne(model.IV); AddOne(text2); newcipher2 = rand.NextDouble() <= 0.5 ? aes.EncryptToByte(text, model) : aes.EncryptToByte(text2, model); } while (Check(newcipher1, newcipher2)); do { AddOne(text); AddOne(model.IV); var ncipher = rand.NextDouble() <= 0.5 ? aes.EncryptToByte(text, model) : aes.EncryptToByte(text2, model); if (Check(newcipher1, ncipher)) { Console.Out.WriteLine("pierwszy"); break; } else if (Check(newcipher2, ncipher)) { Console.Out.WriteLine("pierwszy"); newcipher1 = newcipher2; break; } else { Console.Out.WriteLine("drugi"); } } while (true); for (int i = 0; i < 100; i++) { AddOne(text); AddOne(model.IV); byte[] newcipher; if (rand.NextDouble() <= 0.5) { newcipher = aes.EncryptToByte(text, model); } else { newcipher = aes.EncryptToByte(text2, model); } if (Check(newcipher1, newcipher)) { Console.Out.WriteLine("pierwszy"); } else { Console.Out.WriteLine("drugi"); } } }