Imports Microsoft.VisualBasic
Imports System

''' <summary>
'''*************************************************
''' Copyright (C) 2000 MIND
''' All rights reserved.
''' The information contained here in is confidential and *proprietary to MIND and forms the part of the MIND 
''' CREATED BY		:	Samir Ranjan PAdhi
''' Date			:	22/06/2007 (DD/MM/YYYY format)
''' Description	:	class for encryption technique
''' **Project		:	GAS
''' </summary>

Namespace com.azilon.common.security
	''' <summary>
	''' @author Samir Ranjan Padhi
	''' 
	''' To change the template for this generated type comment go to
	''' Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
	''' </summary>

	Public Class Password_Enc_Dec
	''' <summary>
	'''************************************************************
	'''     Method             :encrypt(String oldPassword) 
	'''     Return Values()	:void
	'''     Parameter(s)		:String oldPassword
	'''     Purpose			:This method is for encrypt the string value
	'''	 Developed by       :Luxmi Nagda
	''' </summary>
			Public Overridable Function encrypt(ByVal oldPassword As String) As String
			  Dim len As Integer=oldPassword.Length
			  Dim newPassword As String=""
			  Dim temp(len - 1) As Integer
			  Dim i As Integer=0
			  Dim ran As Integer=CInt(Fix(Math.Floor((New Random(1)).NextDouble()*256)+1))
			  Dim ch As Integer
			  For i = 0 To len - 1
			  ch= AscW(oldPassword.Chars(i))
			  temp(i)=Convert.ToInt32("" & ch)
			  If (temp(i)>=0) AndAlso (temp(i)<64) Then
				  temp(i) +=128
			  ElseIf (temp(i)>=64) AndAlso (temp(i)<128) Then
				  temp(i) +=128
			  ElseIf (temp(i)>=128) AndAlso (temp(i)<192) Then
				  temp(i) -=128
			  ElseIf (temp(i)>=192) AndAlso (temp(i)<256) Then
				  temp(i) -=128
			  Else

			  End If
			  temp(i) +=ran
			  newPassword +=convertToBoolean(temp(i))
			  Next i
			 newPassword +=convertToBoolean(ran)
			 Return newPassword
			End Function


	''' <summary>
	'''************************************************************
	'''     Method             :decryptInDecimal(String password) 
	'''     Return Values()	:void
	'''     Parameter(s)		:String password
	'''     Purpose			:This method is for decrypt the encrypted boolean value into decimalvalue without the random no.
	''' 	 Developed by       :Luxmi Nagda
	''' </summary>
			Public Overridable Function decryptInDecimal(ByVal password As String) As String
				Dim len As Integer=password.Length
				Dim oldPassword As String=""
				Dim pwdLen As Integer=((len\9)-1)
				Dim temp(pwdLen - 1) As Integer
				Dim k As Integer=0
				Dim ran As Integer


				Dim strbooleantemp As String=""
				Dim strbooleanRan As String=password.Substring(len-9)
				ran=convertToDecimal(strbooleanRan)

				For k = 0 To (pwdLen) - 1
					strbooleantemp=password.Substring((9*k), 9*(k+1) - ((9*k)))
					temp(k)=(convertToDecimal(strbooleantemp)-ran)
					oldPassword +=temp(k) & ""
				Next k
				Return oldPassword
			End Function


	''' <summary>
	'''************************************************************
	'''     Method             :decryptFromDecimalToString(String password) 
	'''     Return Values()	:void
	'''     Parameter(s)		:String password
	'''     Purpose			:This method decrypt the decimal value(that is stored in database without random no) into actual string value
	''' 	 Developed by       :Luxmi Nagda
	''' </summary>
		   Public Overridable Function decryptFromDecimalToString(ByVal password As String) As String
				Dim len As Integer=password.Length
				Dim oldPassword As String=""
				Dim pwdLen As Integer=(len\3)
				Dim temp(pwdLen - 1) As Integer
				Dim k As Integer=0
				Dim p As Char
				Dim strbooleantemp As String=""
				For k = 0 To (pwdLen) - 1
					strbooleantemp=password.Substring((3*k), 3*(k+1) - ((3*k)))
					temp(k)=Convert.ToInt32(strbooleantemp)

					If (temp(k)>=0) AndAlso (temp(k)<64) Then
						temp(k) +=128
					ElseIf (temp(k)>=64) AndAlso (temp(k)<128) Then
						temp(k) +=128
					ElseIf (temp(k)>=128) AndAlso (temp(k)<192) Then
						temp(k) -=128
					ElseIf (temp(k)>=192) AndAlso (temp(k)<256) Then
						temp(k) -=128
					Else

					End If

					p=ChrW(temp(k))
					oldPassword +=p
				Next k
				Return oldPassword
		   End Function


	''' <summary>
	'''************************************************************
	'''     Method             :convertToDecimal(String password) 
	'''     Return Values()	:void
	'''     Parameter(s)		:String strbooleancode
	'''     Purpose			:This method is for converting the boolean value to decimal value.
	'''	 Developed by       :Luxmi Nagda
	''' </summary>
			Public Overridable Function convertToDecimal(ByVal strbooleancode As String) As Integer
				 Dim i As Integer = 0
				 Dim j As Integer = 0
				 Dim result As Integer = 0
				 Dim base As Integer = 2
				 Dim temp As Char = "-"c
				 i=0
				 j=8
				 Do While i<9
					temp=strbooleancode.Chars(i)
					result += Math.Pow(base,j)*Convert.ToInt32("" & AscW(temp))
					 i += 1
					 j -= 1
				 Loop
				 Return result
			End Function
	''' <summary>
	'''************************************************************
	'''     Method             :convertToBoolean(String password) 
	'''     Return Values()	:void
	'''     Parameter(s)		:int intcode
	'''     Purpose			:This method is for converting the decimal value to boolean value.
	'''	 Developed by       :Luxmi Nagda
	''' </summary>
			Public Overridable Function convertToBoolean(ByVal intcode As Integer) As String
				Dim num As Integer=intcode
				Dim quot As Integer
				Dim div As Integer=2
				Dim [rem] As Integer
				Dim temp(8) As Integer
				Dim temp2(8) As Integer
				Dim len As Integer=temp.Length
				Dim str As String=""
				Dim k As Integer=0
				k=0
				Do While num<>0
					 quot=num\div
					 [rem]=num Mod div
					 temp(k)=[rem]
					 num=quot
					k += 1
				Loop
				' for suffixing zeros to make it 9bit.
				For i As Integer = k To len - 1
					temp(i)=0
				Next i
				' For storing valuesreversing array
				Dim i As Integer=len-1
				Dim j As Integer=0
				Do While i>=0
					temp2(j)=temp(i)
					i -= 1
					j += 1
				Loop
				' for converting array to string value.
				For i As Integer = 0 To len - 1
				str +=temp2(i)
				Next i
				Return str
			End Function
	''' <summary>
	'''************************************************************
	'''     Method             :convertToBoolean(String password) 
	'''     Return Values()	:void
	'''     Parameter(s)		:int intcode
	'''     Purpose			:This method is for converting the  boolean value to String.
	'''	 Developed by       :Luxmi Nagda
	''' </summary>

	Public Overridable Function decrypt(ByVal password As String) As String
	  Dim len As Integer=password.Length
	  Dim oldPassword As String=""
	  Dim pwdLen As Integer=((len\9)-1)
	  Dim temp(pwdLen - 1) As Integer
	  Dim k As Integer=0
	  Dim ran As Integer
	  Dim p As Char
	  Dim strbooleantemp As String=""
	  Dim strbooleanRan As String=password.Substring(len-9)
	  ran=convertToDecimal(strbooleanRan)

	  For k = 0 To (pwdLen) - 1
		strbooleantemp=password.Substring((9*k), 9*(k+1) - ((9*k)))
		temp(k)=(convertToDecimal(strbooleantemp)-ran)

		If (temp(k)>=0) AndAlso (temp(k)<64) Then
			temp(k) +=128
		ElseIf (temp(k)>=64) AndAlso (temp(k)<128) Then
			temp(k) +=128
		ElseIf (temp(k)>=128) AndAlso (temp(k)<192) Then
			temp(k) -=128
		ElseIf (temp(k)>=192) AndAlso (temp(k)<256) Then
			temp(k) -=128
		Else

		End If

		p=ChrW(temp(k))
		oldPassword +=p
	  Next k
	Return oldPassword
	End Function



		Shared Sub Main(ByVal arg() As String)
		  
			  Dim pwdEncDecription As New Password_Enc_Dec()
			  Console.WriteLine(pwdEncDecription.decryptInDecimal(pwdEncDecription.encrypt("172.29.55.218"))) 
			  Console.WriteLine((pwdEncDecription.decryptFromDecimalToString("177183178174178185174181181174178177184"))) 
			
		End Sub
	End Class

End Namespace