VB.NET | Suhail Kaleem

VB.NET

Get months list between two dates

0

Following function  will  get a list of months  with days remaining in those months  when start date and end dates are provided


Shared Function GetMonthsList(StartDate As Date, EndDate As Date) As ArrayList

Try

Dim dt As Date = StartDate
Dim edt As Date = EndDate

Dim ar As New ArrayList
Dim str As New StringBuilder

While dt < edt

str.Append(dt.Date.ToString("MMMM yyyy"))
str.Append(",")
Dim dt2 As Date = dt.AddMonths(1)

If dt2 > edt Then
Dim minusdays As Integer = DateDiff(DateInterval.Day, dt, edt)
str.Append(minusdays + 1)
ar.Add(str.ToString)
Exit While
End If

Dim remainingdays As Integer = System.DateTime.DaysInMonth(dt.Year, dt.Month) - dt.Day
str.Append(remainingdays + 1)

ar.Add(str.ToString)
dt = dt.AddDays(remainingdays + 1)
str.Clear()
End While

Return ar

Catch ex As Exception
Throw ex
End Try
End Function

Use can use this functions as


GetMonthsList(Date.Parse("1/4/2012"),Date.Parse("4/30/2012"))

this will output

January 2012,28
February 2012,29
March 2012,31
April 2012,30

Function to format dateTime to RSS 2 pubDate

0

The following vb.net function will format the date-time  to pubDate format for rss 2

Private Function BuildPubDate(ByVal d As Date) As String
Try
Dim RV As String
Dim day As String = d.Day.ToString
If day.Length = 1 Then day = "0" &amp; day
Dim month As String = d.Month.ToString
Select Case month
Case "1"
month = "January"
Case "2"
month = "February"
Case "3"
month = "March"
Case "4"
month = "April"
Case "5"
month = "May"
Case "6"
month = "June"
Case "7"
month = "July"
Case "8"
month = "August"
Case "9"
month = "September"
Case "10"
month = "October"
Case "11"
month = "November"
Case "12"
month = "December"
End Select

Dim Mtime As String = ""
Dim mDate As Date = d.ToUniversalTime
If mDate.Hour.ToString.Length = 1 Then
Mtime = "0" &amp; mDate.Hour.ToString
Else
Mtime = mDate.Hour.ToString
End If
Mtime += ":"
If mDate.Minute.ToString.Length = 1 Then
Mtime += "0" &amp; mDate.Minute.ToString
Else
Mtime += mDate.Minute.ToString
End If
Mtime += ":"
If mDate.Second.ToString.Length = 1 Then
Mtime += "0" &amp; mDate.Second.ToString
Else
Mtime += mDate.Second.ToString
End If

RV = d.DayOfWeek.ToString.Substring(0, 3)
RV += ", " &amp; day &amp; " " &amp; month.Substring(0, 3)
RV += " " &amp; d.Year.ToString &amp; " " &amp; Mtime &amp; " GMT"
Return RV
Catch ex As Exception
Return ""
End Try

Encrypt in VB.NET and Decrypt in PHP and Vice Versa

22
########################################
# BEGIN PHP CODE
########################################

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$ky = 'lkirwf897+22#bbtrm8814z5qq=498j5'; // 32 * 8 = 256 bit key
$iv = '741952hheeyy66#cs!9hjv887mxx7@8y'; // 32 * 8 = 256 bit iv

$text = "Here is my data to encrypt!!!";

$from_vb = "QBlgcQ2+v3wd8RLjhtu07ZBd8aQWjPMfTc/73TPzlyA=";   // enter value from vb.net app here to test

$etext = encryptRJ256($ky, $iv, $text);
$dtext = decryptRJ256($ky, $iv, $etext);
$vtext = decryptRJ256($ky, $iv, $from_vb);

echo "orignal string: $text";
echo "encrypted in php: $etext";
echo "decrypted in php: $dtext";
echo "encrypted in vb: $from_vb";
echo "from vb decrypted in php: $vtext";
echo "If you like it say thanks! ";

exit;

function decryptRJ256($key,$iv,$string_to_decrypt)
{

$string_to_decrypt = base64_decode($string_to_decrypt);

$rtn = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_decrypt, MCRYPT_MODE_CBC, $iv);

$rtn = rtrim($rtn, "\4");

return($rtn);

}

function encryptRJ256($key,$iv,$string_to_encrypt)
{

$rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_encrypt, MCRYPT_MODE_CBC, $iv);

$rtn = base64_encode($rtn);

return($rtn);

}

?>

########################################
# END PHP CODE
########################################
########################################
# BEGIN VB.NET CODE (console app)
########################################

Imports System
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO

Module Module1

Sub Main()

'Shared 256 bit Key and IV here
Dim sKy As String = "lkirwf897+22#bbtrm8814z5qq=498j5"  '32 chr shared ascii string (32 * 8 = 256 bit)
Dim sIV As String = "741952hheeyy66#cs!9hjv887mxx7@8y"  '32 chr shared ascii string (32 * 8 = 256 bit)

Dim sTextVal As String = "Here is my data to encrypt!!!"

Dim eText As String
Dim dText As String

eText = EncryptRJ256(sKy, sIV, sTextVal)
dText = DecryptRJ256(sKy, sIV, eText)

Console.WriteLine("key: " &amp; sKy)
Console.WriteLine()
Console.WriteLine(" iv: " &amp; sIV)
Console.WriteLine("txt: " &amp; sTextVal)
Console.WriteLine("encrypted: " &amp; eText)
Console.WriteLine("decrypted: " &amp; dText)
Console.WriteLine("If you like it say thanks! ")
Console.WriteLine("press any key to exit")
Console.ReadKey(True)

End Sub

Public Function DecryptRJ256(ByVal prm_key As String, ByVal prm_iv As String, ByVal prm_text_to_decrypt As String)

Dim sEncryptedString As String = prm_text_to_decrypt

Dim myRijndael As New RijndaelManaged
myRijndael.Padding = PaddingMode.Zeros
myRijndael.Mode = CipherMode.CBC
myRijndael.KeySize = 256
myRijndael.BlockSize = 256

Dim key() As Byte
Dim IV() As Byte

key = System.Text.Encoding.ASCII.GetBytes(prm_key)
IV = System.Text.Encoding.ASCII.GetBytes(prm_iv)

Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)

Dim sEncrypted As Byte() = Convert.FromBase64String(sEncryptedString)

Dim fromEncrypt() As Byte = New Byte(sEncrypted.Length) {}

Dim msDecrypt As New MemoryStream(sEncrypted)
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

Return (System.Text.Encoding.ASCII.GetString(fromEncrypt))

End Function

Public Function EncryptRJ256(ByVal prm_key As String, ByVal prm_iv As String, ByVal prm_text_to_encrypt As String)

Dim sToEncrypt As String = prm_text_to_encrypt

Dim myRijndael As New RijndaelManaged
myRijndael.Padding = PaddingMode.Zeros
myRijndael.Mode = CipherMode.CBC
myRijndael.KeySize = 256
myRijndael.BlockSize = 256

Dim encrypted() As Byte
Dim toEncrypt() As Byte
Dim key() As Byte
Dim IV() As Byte

key = System.Text.Encoding.ASCII.GetBytes(prm_key)
IV = System.Text.Encoding.ASCII.GetBytes(prm_iv)

Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)

Dim msEncrypt As New MemoryStream()
Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

toEncrypt = System.Text.Encoding.ASCII.GetBytes(sToEncrypt)

csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
csEncrypt.FlushFinalBlock()

encrypted = msEncrypt.ToArray()

Return (Convert.ToBase64String(encrypted))

End Function

End Module

########################################
# END VB.NET CODE
########################################
Go to Top