阅读:2606回复:4
请教:toolflag程序中AES透明加解密的一个问题(急急!)
在toolflag程序中,若使用AES加密算法,由于AES为不等长加密,密文字符串要比明文长一些(好像 密文长度=16*(明文长度/16+1)),但在sfwrit中只能将与明文长度等长的密文写入文件,这导致解密时文件出错,这个问题困扰很长时间了,请教牛人怎么解决,怎么才能将完整的密文写入文件?尽管这样会使文件长度大于原文件长度
|
|
|
沙发#
发布于:2007-06-04 15:51
在线等~~~~~~~~~
我使用这个程序,总是解密读时,文件长度%16 的最后余数几位是乱码 经过使用应用程序测试,原因是密文不完整,导致解密出的明文出错 下面是sfwrite的代码,我将IrpSp->Parameters.Write.Length的长度加16,结果一个的错误 请各位牛人帮忙,感激不尽!!! NTSTATUS SfWrite( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PSFILTER_DEVICE_EXTENSION DevExt = (PSFILTER_DEVICE_EXTENSION) DeviceObject->DeviceExtension; PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp); PFILE_OBJECT FileObject = IrpSp->FileObject; FILE_CONTEXT_HDR FileCtxHdr; PFILE_CONTEXT FileCtxPtr = NULL; PREAD_WRITE_COMPLETION_CONTEXT CompletionCtx = NULL; PVOID OldBuffer = NULL; PVOID MyBuffer = NULL; ULONG Length = 0; ULONG Offset = 0; NTSTATUS Status = STATUS_SUCCESS; ULONG reallength,i=0; //struct rc4_state *s; //unsigned char buffer[1024]; UCHAR keys[16]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef}; PAGED_CODE(); // // Sfilter doesn't allow handles to its control device object to be created, // therefore, no other operation should be able to come through. // ASSERT(!IS_MY_CONTROL_DEVICE_OBJECT(DeviceObject)); ASSERT(IS_MY_DEVICE_OBJECT(DeviceObject)); // // We only care about volume filter device object // if (!DevExt->StorageStackDeviceObject) { IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(DevExt->AttachedToDeviceObject, Irp); } #if DBG //wl if (DevExt->UsbType == FALSE) { IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(DevExt->AttachedToDeviceObject, Irp); } #endif if (!(Irp->Flags & (IRP_NOCACHE | IRP_PAGING_IO | IRP_SYNCHRONOUS_PAGING_IO))) { IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(DevExt->AttachedToDeviceObject, Irp); } ExAcquireFastMutex(&DevExt->FsCtxTableMutex); FileCtxHdr.FsContext = FileObject->FsContext; FileCtxPtr = RtlLookupElementGenericTable(&DevExt->FsCtxTable, &FileCtxHdr); ExReleaseFastMutex(&DevExt->FsCtxTableMutex); if (!FileCtxPtr || !FileCtxPtr->EncryptOnWrite) { IoSkipCurrentIrpStackLocation(Irp); return IoCallDriver(DevExt->AttachedToDeviceObject, Irp); } IrpSp->Parameters.Write.Length = IrpSp->Parameters.Write.Length+16; //这里我将Write.Length加16,结果一样的错误 do { Length = IrpSp->Parameters.Write.Length; if (Irp->MdlAddress) { OldBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority); } else { PMDL Mdl; Mdl = IoAllocateMdl(Irp->UserBuffer, Length, FALSE, FALSE, NULL); if (Mdl == NULL) { KdPrint(("sfilter!SfWrite: IoAllocateMdl failed\n")); Status = STATUS_INSUFFICIENT_RESOURCES; break; } try { MmProbeAndLockPages(Mdl, Irp->RequestorMode, IoReadAccess); } except (EXCEPTION_EXECUTE_HANDLER) { KdPrint(("sfilter!SfWrite: STATUS_INVALID_USER_BUFFER\n")); IoFreeMdl(Mdl); Status = STATUS_INVALID_USER_BUFFER; break; } MmUnlockPages(Mdl); IoFreeMdl(Mdl); OldBuffer = Irp->UserBuffer; } if (!OldBuffer) { KdPrint(("sfilter!SfWrite: STATUS_INVALID_PARAMETER\n")); Status = STATUS_INVALID_PARAMETER; break; } CompletionCtx = ExAllocateFromNPagedLookasideList(&gReadWriteCompletionCtxLookAsideList); if (!CompletionCtx) { KdPrint(("sfilter!SfWrite: STATUS_INSUFFICIENT_RESOURCES\n")); Status = STATUS_INSUFFICIENT_RESOURCES; break; } MyBuffer = ExAllocatePoolWithTag(NonPagedPool, IrpSp->Parameters.Write.Length, SFLT_POOL_TAG); if (!MyBuffer) { KdPrint(("sfilter!SfWrite: STATUS_INSUFFICIENT_RESOURCES\n")); ExFreePool(CompletionCtx); Status = STATUS_INSUFFICIENT_RESOURCES; break; } CompletionCtx->OldMdl = Irp->MdlAddress; CompletionCtx->OldUserBuffer = Irp->UserBuffer; CompletionCtx->OldSystemBuffer = Irp->AssociatedIrp.SystemBuffer; CompletionCtx->MdlForUserBuffer = NULL; CompletionCtx->OldBuffer = OldBuffer; CompletionCtx->MyBuffer = MyBuffer; CompletionCtx->Length = Length; //Length = IrpSp->Parameters.Write.Length+16; Irp->MdlAddress = IoAllocateMdl(MyBuffer, IrpSp->Parameters.Write.Length, FALSE, TRUE, NULL); if (!Irp->MdlAddress) { KdPrint(("sfilter!SfWrite: STATUS_INSUFFICIENT_RESOURCES\n")); Irp->MdlAddress = CompletionCtx->OldMdl; ExFreePool(CompletionCtx); ExFreePool(MyBuffer); Status = STATUS_INSUFFICIENT_RESOURCES; break; } KdPrint(("sfilter!SfWrite: Encrypt %ws\n", FileCtxPtr->Name)); keyset(keys, 16); reallength = CompletionCtx->Length;//记录文件长度 //CompletionCtx->Length = Length; AES_Encrypt((PCHAR)MyBuffer,(PCHAR)OldBuffer,reallength); MmBuildMdlForNonPagedPool(Irp->MdlAddress); Irp->UserBuffer = MmGetMdlVirtualAddress(Irp->MdlAddress); IoCopyCurrentIrpStackLocationToNext(Irp); IoSetCompletionRoutine(Irp, SfWriteCompletion, CompletionCtx, TRUE, TRUE,TRUE); return IoCallDriver(DevExt->AttachedToDeviceObject, Irp); } while (FALSE); Irp->IoStatus.Status = Status; IoCompleteRequest(Irp, IO_NO_INCREMENT); return Status; } |
|
|
板凳#
发布于:2007-06-04 16:27
自己顶一下
怎么没人呢 |
|
|
地板#
发布于:2007-06-08 12:08
AES分组长度为128bit即16字节,密文明文长度一样.
密钥有128,192,256bit三种情况. 不知你为什么不等长呢? 还有请问一下,如何处理文件结束? 要不要对文件偏移进行处理? |
|
|
地下室#
发布于:2011-09-24 17:42
同遇到这个问题??不知道楼主解决了没??请教方案
|
|