public static Pair<IV, int> extractIVFromContent(BufferReference content)
		{
			if (content.Length < 1 || content.Length < content[0] + 1)
				throw new InternalError("Failed to extract IV from encrypted block.");

			var ivBytes = content[0];
			var iv = new byte[content[0]];
			content.copy(1, iv, 0, ivBytes);
			return Pair.make(new IV(iv), 1 + ivBytes);
		}
		public BufferReference decrypt(BufferReference content)
		{
			Pair<IV, int> ivInfo = extractIVFromContent(content);

			var iv = ivInfo.First;
			var ivPrefixLength = ivInfo.Second;

			_provider.IV = iv.Data;

			using (var encryptor = _provider.CreateDecryptor())
			{
				_outputStream.SetLength(0);
				using (var stream = new CryptoStream(_outputStream, encryptor, CryptoStreamMode.Write))
				{
					stream.Write(content.Buffer, content.Offset + ivPrefixLength, content.Length - ivPrefixLength);
					stream.FlushFinalBlock();
					return _outputStream.asBufferReference();
				}
			}
		}
		public BufferReference encrypt(IV iv, BufferReference content)
		{
			_provider.IV = iv.Data;

			// don't see a way how we can reuse MemoryStream / CryptoStream here, need
			// to implement based on TransFormBlock, but even then we need to create the 
			// Encryptor for each instance.

			_outputStream.SetLength(0);
			writeIV(_provider.IV, _outputStream);

			using (var encryptor = _provider.CreateEncryptor())
			{
				// note: cryptostream closes our output stream, which is fine for us!)
				using (var stream = new CryptoStream(_outputStream, encryptor, CryptoStreamMode.Write))
				{
					stream.Write(content.Buffer, content.Offset, content.Length);
					stream.FlushFinalBlock();
					return _outputStream.asBufferReference();
				}
			}
		}
Example #4
0
 public override void link(Linker usr)
 {
     buffer = createBuffer(Name, width, height);
 }
Example #5
0
 protected override void doLink(Linker user)
 {
     buffer = createBuffer(name);
     targ   = user.getTarget(name);
 }
		// Return decrypted data by using DecryptionService and the given serialized Key
		public static BufferReference decrypt(string serializedKey, BufferReference encrypted)
		{
			return decrypt(Key.deserialize(serializedKey), encrypted);
		}
		// Return decrypted data by using DecryptionService and the given Key
		public static BufferReference decrypt(Key key, BufferReference content)
		{
			using (var service = new DecryptionService(key))
			{
				return service.decrypt(content);
			}
		}
		public static BufferReference encrypt(Key key, IV? iv_, BufferReference content)
		{
			using (var service = new EncryptionService(key))
			{
				var iv = iv_ ?? service.createRandomIV();
				return service.encrypt(iv, content);
			}
		}
		public static BufferReference encrypt(Key key, BufferReference content)
		{
			return encrypt(key, null, content);
		}
		public static BufferReference encrypt(string serializedKey, IV? iv_, BufferReference content)
		{
			return encrypt(Key.deserialize(serializedKey), iv_, content);
		}