public void EncryptWithTripleDES_Should_ReturnExpectedResult_When_ProvidedKeysAndInitializationVector() { // Arrange var key = new EncryptionData("SecretKey"); var vector = new EncryptionData("InitializationVector"); var e1 = new SymmetricEncryption(SymmetricEncryption.Provider.TripleDES) { InitializationVector = vector }; var e2 = new SymmetricEncryption(SymmetricEncryption.Provider.TripleDES) { InitializationVector = vector }; // Act var encrypted = e1.Encrypt(_targetData, key); var decrypted = e2.Decrypt(encrypted, key); // -- the data will be padded to the encryption block size, so we need to trim it back down. var actual = decrypted.Text.Substring(0, _targetData.Bytes.Length); // Assert Assert.Equal(_targetString, actual); }
/// <summary> /// Sign Image document with metadata signature with customer object and encryption /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Advanced Usage] # SignImageWithCustomMetadata : Sign Image document with metadata signature with customer object and encryption\n"); // The path to the documents directory. string filePath = Constants.SAMPLE_IMAGE; string fileName = Path.GetFileName(filePath); string outputFilePath = Path.Combine(Constants.OutputPath, "SignImageWithCustomMetadata", fileName); using (Signature signature = new Signature(filePath)) { // setup key and passphrase string key = "1234567890"; string salt = "1234567890"; // create data encryption IDataEncryption encryption = new SymmetricEncryption(SymmetricAlgorithmType.Rijndael, key, salt); // setup options with text of signature MetadataSignOptions options = new MetadataSignOptions(); // create custom object DocumentSignatureData documentSignature = new DocumentSignatureData() { ID = Guid.NewGuid().ToString(), Author = Environment.UserName, Signed = DateTime.Now, DataFactor = 11.22M }; // Specify different Metadata Signatures and add them to options signature collection ushort imgsMetadataId = 41996; // Specify different Metadata Signatures and add them to options signature collection // setup Author property ImageMetadataSignature mdDocument = new ImageMetadataSignature(imgsMetadataId++, documentSignature); // set encryption mdDocument.DataEncryption = encryption; // setup Author property ImageMetadataSignature mdAuthor = new ImageMetadataSignature(imgsMetadataId++, "Mr.Scherlock Holmes"); // set encryption mdAuthor.DataEncryption = encryption; // setup data of document id ImageMetadataSignature mdDocId = new ImageMetadataSignature(imgsMetadataId++, Guid.NewGuid().ToString()); // set encryption mdDocId.DataEncryption = encryption; // add signatures to options options.Signatures.Add(mdDocument); options.Signatures.Add(mdAuthor); options.Signatures.Add(mdDocId); // sign document to file SignResult signResult = signature.Sign(outputFilePath, options); Console.WriteLine($"\nSource document signed successfully with {signResult.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); } }
public void ToEncyptedDataAndDecryptInternal() { #region Arrange var si = new SecretInformation() { Filename = Guid.NewGuid().ToString() }; var secret = Random.CreateData(512 / 8); var result = new MemoryStream(); #endregion #region Act var encyptedData = si.ToEncyptedData(secret); SymmetricEncryption.DecryptInternal(new MemoryStream(encyptedData), result, secret, null, null); var secretInformation = SecretInformation.FromProtoBufData(result.ToArray()); #endregion #region Assert Assert.That(secretInformation.Filename, Is.EqualTo(si.Filename)); #endregion }
public void Encrypted_Data_Differs_From_Raw_data() { byte[] rawData = Random.GetNumbers(256); byte[] encryptedData = SymmetricEncryption.Encrypt(rawData, key, iv); CollectionAssert.AreNotEqual(rawData, encryptedData); }
public void EncryptDecryptTest_Stream() { string decryptionKey = "7A008A8C4AD363C77E88F638DC32FFD8"; string algorithmName = "Rijndael"; string sourceFileName = "Source.txt"; string encryptedFileName = "Encrypted.enc"; string decryptedFileName = "Decrypted.txt"; string text = "これは、SymmetricEncryption クラスのテストです。"; File.WriteAllText(sourceFileName, text, Encoding.UTF8); using (Stream input = File.OpenRead(sourceFileName)) using (Stream output = File.OpenWrite(encryptedFileName)) { SymmetricEncryption.Encrypt(input, output, decryptionKey, algorithmName); } using (Stream input = File.OpenRead(encryptedFileName)) using (Stream output = File.OpenWrite(decryptedFileName)) { SymmetricEncryption.Decrypt(input, output, decryptionKey, algorithmName); } CollectionAssert.AreEqual(File.ReadAllBytes(sourceFileName), File.ReadAllBytes(decryptedFileName)); }
public IHttpActionResult Post(Dictionary <string, string> pData) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } SymmetricEncryption se = new SymmetricEncryption(); string key = pData["Key"]; string domain = pData["Domain"]; string remoteKey = se.Decrypt(key, domain); var clientData = DataRepository.Instance.ClienDataList.FirstOrDefault(data => data.Domain.Equals(domain)); if (clientData == null) { return(NotFound()); } clientData.RemoteKey = remoteKey; LicenseRepository.Instance.GenerateLicense(domain, 365); return(Ok(remoteKey)); }
public async Task SymmetricEncryptionEqualsTest() { string message = "aw3lrifos83fusoi3fjsofisjfo"; byte[] messageBytes = Encoding.UTF8.GetBytes(message); var asymmetricEncryption = new AsymmetricEncryption(_publicKey, _privateKey); string path = Path.GetTempFileName(); MemoryStream encryptedStream = new MemoryStream(); await asymmetricEncryption.EnvelopeAsync(messageBytes, 256, 128, encryptedStream, CancellationToken.None); byte[] encryptedBytes = encryptedStream.ToArray(); int keyLen = BitConverter.ToInt32(encryptedBytes.Take(4).ToArray(), 0); byte[] key = encryptedBytes.Skip(4).Take(keyLen).ToArray(); int ivLen = BitConverter.ToInt32(encryptedBytes.Skip(4 + key.Length).Take(4).ToArray(), 0); byte[] iv = encryptedBytes.Skip(4 + key.Length + 4).Take(ivLen).ToArray(); key = asymmetricEncryption.DecryptToBytes(key); iv = asymmetricEncryption.DecryptToBytes(iv); byte[] encryptedMessageBytes = encryptedBytes.Skip(4 + keyLen + 4 + ivLen).ToArray(); SymmetricEncryption symmetricEncryption = new SymmetricEncryption(key, iv); string decryptedMessage = symmetricEncryption.DecryptToString(encryptedMessageBytes, Encoding.UTF8); Assert.Equal(message, decryptedMessage); }
public void TestMethod5() { var se = new SymmetricEncryption("12345678", "87654321", SymmetricAlgorithmType.TripleDES); const string sourceStr = "humin123"; var targetStr = se.Encrypt(sourceStr); Assert.AreEqual(sourceStr, se.Decrypt(targetStr)); }
public void TestMethod13() { var se = new SymmetricEncryption("爱我中华爱我中华1574551¥#@54633", 0); const string sourceStr = "爱我中华爱"; Assert.AreNotEqual(sourceStr, se.Decrypt(se.Encrypt(sourceStr, Encoding.UTF8), Encoding.Unicode)); Assert.AreEqual(sourceStr, se.Decrypt(se.Encrypt(sourceStr, Encoding.Unicode), Encoding.Unicode)); }
public void TestMethod11() { var se = new SymmetricEncryption("爱我中华爱我中华1574551¥#@54633", 0); const string sourceStr = "humin123"; var targetStr = se.Encrypt(sourceStr); Assert.AreEqual(sourceStr, se.Decrypt(targetStr)); }
public void TestMethod1() { const string sourceStr = "humin123"; var se = new SymmetricEncryption("jay123456"); var targetStr = se.Encrypt(sourceStr); Assert.AreEqual(sourceStr, se.Decrypt(targetStr)); }
public void Can_Encrypt_Data() { byte[] rawData = Random.GetNumbers(256); byte[] encryptedData = SymmetricEncryption.Encrypt(rawData, key, iv); Assert.NotNull(encryptedData); CollectionAssert.IsNotEmpty(encryptedData); }
public void TDES() { SymmetricEncryption enc = new SymmetricEncryption(new TripleDESCryptoServiceProvider()); for(int i = 1000; i < 1025; i ++) { byte[] data = new byte[1010]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetBytes(data); byte[] encd = enc.EncryptData(data); if(i % 12 == 0) { continue; } byte[] decd = enc.DecryptData(encd); MemBlock mdecd = MemBlock.Reference(decd); MemBlock mdata = MemBlock.Reference(data); Assert.AreEqual(mdecd, mdata, "TDESEncryption: " + i); } }
public void Incoming() { SymmetricAlgorithm sa = new RijndaelManaged(); SymmetricEncryption se = new SymmetricEncryption(sa); HashAlgorithm hash = new SHA1CryptoServiceProvider(); int spi = 12345; int epoch = 67890; int seqid = 1222; Random rand = new Random(); byte[] data = new byte[144]; rand.NextBytes(data); MemBlock mdata = MemBlock.Reference(data); byte[] to_sign = new byte[12 + data.Length]; int pos = 0; NumberSerializer.WriteInt(spi, to_sign, pos); pos += 4; NumberSerializer.WriteInt(epoch, to_sign, pos); pos += 4; NumberSerializer.WriteInt(seqid, to_sign, pos); pos += 4; data.CopyTo(to_sign, pos); byte[] signature = hash.ComputeHash(to_sign); byte[] to_encrypt = new byte[4 + data.Length + signature.Length]; pos = 0; NumberSerializer.WriteInt(data.Length, to_encrypt, pos); pos += 4; data.CopyTo(to_encrypt, pos); pos += data.Length; signature.CopyTo(to_encrypt, pos); byte[] encrypted = se.EncryptData(to_encrypt); byte[] packet = new byte[12 + encrypted.Length]; pos = 0; NumberSerializer.WriteInt(spi, packet, pos); pos += 4; NumberSerializer.WriteInt(epoch, packet, pos); pos += 4; NumberSerializer.WriteInt(seqid, packet, pos); pos += 4; encrypted.CopyTo(packet, pos); MemBlock mpacket = MemBlock.Reference(packet); // check SecurityDataMessage sdm_e = new SecurityDataMessage(packet); sdm_e.Decrypt(se); Assert.AreEqual(spi, sdm_e.SPI, "SPI"); Assert.AreEqual(epoch, sdm_e.Epoch, "Epoch"); Assert.AreEqual(seqid, sdm_e.Seqid, "Seqid"); Assert.AreEqual(mdata, sdm_e.Data, "Data"); Assert.IsTrue(sdm_e.Verify(hash), "Signature"); Assert.AreEqual(mpacket, sdm_e.Packet, "Packet"); SecurityDataMessage sdm_d = new SecurityDataMessage(); sdm_d.SPI = spi; sdm_d.Epoch = epoch; sdm_d.Seqid = seqid; sdm_d.Data = data; sdm_d.Sign(hash); sdm_d.Encrypt(se); sdm_e = new SecurityDataMessage(sdm_d.Packet); sdm_e.Decrypt(se); Assert.AreEqual(spi, sdm_e.SPI, "SPI"); Assert.AreEqual(epoch, sdm_e.Epoch, "Epoch"); Assert.AreEqual(seqid, sdm_e.Seqid, "Seqid"); Assert.AreEqual(mdata, sdm_e.Data, "Data"); Assert.IsTrue(sdm_e.Verify(hash), "Signature"); Assert.AreEqual(sdm_d.Packet, sdm_e.Packet, "Packet"); }
///<summary>Decrypts the packet given a SymmetricEncryption returning true ///if it was able to decrypt it.</summary> public bool Decrypt(SymmetricEncryption se) { byte[] decrypted = se.DecryptData(_encrypted_data); int pos = 0; int data_len = NumberSerializer.ReadInt(decrypted, pos); pos += 4; _data = MemBlock.Reference(decrypted, pos, data_len); pos += data_len; _signature = MemBlock.Reference(decrypted, pos, decrypted.Length - pos); return true; }
///<summary>Encrypts the packet given a SymmetricEncryption.</summary> public void Encrypt(SymmetricEncryption se) { byte[] to_encrypt = new byte[4 + _data.Length + _signature.Length]; int pos = 0; NumberSerializer.WriteInt(_data.Length, to_encrypt, pos); pos += 4; _data.CopyTo(to_encrypt, pos); pos += _data.Length; _signature.CopyTo(to_encrypt, pos); _encrypted_data = se.EncryptData(to_encrypt); _update_icpacket = true; }