Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

binary array shifting

hi everybody,

i am using encryption and decryptionin with RSA and symmectric algo aes in which

i have to shift byte to right and i m using that code in VB.Net

dim keyex(machine.shift.right(rsa.keysize,3) as byte
byte[] keyex(rsa.ketsize >> 3) as byte in c#

and i m getting error at word machine which is machine is not declared .
so i want to know that is there any class for machine.shift.right or how can i use machine class in vb.net

or is there any another way of doing which is this line machine.shift.right is doing

plz solve my problem
i would be very thankful to you
0
softsolvers
Asked:
softsolvers
  • 3
  • 2
1 Solution
 
RonaldBiemansCommented:
I gather from this that you tried a c# to vb.net converter

if rsa.keysize is an unsigned integer than you are out of luck because vb.net doesn't support them (they will in the 2005 version though http://lab.msdn.microsoft.com/vs2005/.)

if rsa.keysize isn't an unsigned integer then you can just do
rsa.keysize >> 3
0
 
RonaldBiemansCommented:
I picked this up somewhere in the past but I can't remember where (so the credit for this should go to somebody else)

Namespace Machine

 ' This namespace/class should work as drop in methods for use
 ' with code that is converted from C# to VB.NET using most of
 ' the C#->VB.NET conversion tools available that generate the
 ' following:
 ' // original C# code...
 ' value = value << 16;
 ' ' which converts to...
 ' value = Machine.Shift.Left(value, 16)

 Public Interface IOperation
  Function ShiftLeft(ByVal lhs As Int32, ByVal count As Int32) As Int32
  Function ShiftRight(ByVal lhs As Int32, ByVal count As Int32) As Int32
 End Interface

 Public Class Shift

  Private Shared _opInstance As IOperation

  Private Sub New()
  End Sub

  Private Shared Sub DoIL()

   Dim asmName As New AssemblyName()
   Dim type As Type
   Dim asmBuilder As AssemblyBuilder
   Dim modBuilder As ModuleBuilder
   Dim typeBuilder As TypeBuilder
   Dim mBuilder As MethodBuilder
   Dim ilgen As ILGenerator

   asmName.Name = "DummyAsm"
   asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run)

   modBuilder = asmBuilder.DefineDynamicModule("DummyMod")

   typeBuilder = modBuilder.DefineType("DummyType", TypeAttributes.Public)
   typeBuilder.AddInterfaceImplementation(GetType(IOperation))
   typeBuilder.DefineDefaultConstructor(MethodAttributes.Public Or MethodAttributes.SpecialName Or MethodAttributes.RTSpecialName)
   mBuilder = typeBuilder.DefineMethod("ShiftLeft", MethodAttributes.Public Or MethodAttributes.Virtual, CallingConventions.Standard, GetType(System.Int32), New Type() {GetType(System.Int32), GetType(System.Int32)})
   ilgen = mBuilder.GetILGenerator()
   ilgen.Emit(OpCodes.Ldarg_1)
   ilgen.Emit(OpCodes.Ldarg_2)
   ilgen.Emit(OpCodes.Shl)
   ilgen.Emit(OpCodes.Ret)

   typeBuilder.DefineMethodOverride(mBuilder, GetType(IOperation).GetMethod("ShiftLeft"))
   mBuilder = typeBuilder.DefineMethod("ShiftRight", MethodAttributes.Public Or MethodAttributes.Virtual, CallingConventions.Standard, GetType(System.Int32), New Type() {GetType(System.Int32), GetType(System.Int32)})
   ilgen = mBuilder.GetILGenerator()
   ilgen.Emit(OpCodes.Ldarg_1)
   ilgen.Emit(OpCodes.Ldarg_2)
   ilgen.Emit(OpCodes.Shr)
   ilgen.Emit(OpCodes.Ret)

   typeBuilder.DefineMethodOverride(mBuilder, GetType(IOperation).GetMethod("ShiftRight"))

   type = typeBuilder.CreateType()
   _opInstance = DirectCast(Activator.CreateInstance(type), IOperation)

  End Sub

Public Shared Function Right(ByVal value As Integer, ByVal count As Integer) As Integer
   If _opInstance Is Nothing Then
    DoIL()
   End If
   Return _opInstance.ShiftRight(value, count)
  End Function

  Public Shared Function Left(ByVal value As Integer, ByVal count As Integer) As Integer
   If _opInstance Is Nothing Then
    DoIL()
   End If
   Return _opInstance.ShiftLeft(value, count)
  End Function

 End Class

End Namespace
0
 
softsolversAuthor Commented:
sir,

i would like to thank you a lot for taking intrest in my problem.

the first solution which you have provided i:e to use "rsa.keysize >> 3" so i tried

Dim keyex(rsa.KeySize >> 3) As Byte

although this is not giving any compilation error but it gives a runtime error saying

ERROR : offset and lenght were out of bounds for the array or count is greater than number of elements from index to end of the source collection

---------------------------------------------------------------------------------------------

now sir i am trying your second solution that is with namespace.
so give me just few minutes so i'll get back to you

softsolvers


 
0
 
softsolversAuthor Commented:
can u please tell me which classes i need to import for your namespace. cos it is giving errors on doil function
0
 
RonaldBiemansCommented:
sorry about that

Imports System.Reflection.Emit
Imports System.Reflection
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now