Encryption and Decryption Text
-
How Can I Encrypt and Decrypt a String remember its encrypt and Decrypt a null string also Please,Help me
Arindam Banerjee Sr. Software Developer Rance Computer Pvt Ltd. Kolkata (India)
Please check the following sample, I think you can also include null strings checking. I hope it works out for you. :)
Imports System.Text
Imports System.Security.CryptographyPublic Class Encryption
#Region "Public Routines"
const ENCRYPT_KEY AS STRING = "TEST"
const ENCRYPT_VECTOR AS STRING = "CODEPRJT"Public Shared Function ToBase64(ByVal sDataToEncrypt As String) As String Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream Dim key As Byte() = Encoding.Default.GetBytes(ENCRYPT\_KEY.PadRight(24, Chr(0))) Dim Vector As Byte() = Encoding.Default.GetBytes(ENCRYPT\_VECTOR.PadRight(8, Chr(0))) Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider Dim cryptoStream As CryptoStream = New CryptoStream(stream, des.CreateEncryptor(key, Vector), CryptoStreamMode.Write) Dim Input() As Byte = Encoding.Default.GetBytes(sDataToEncrypt) cryptoStream.Write(Input, 0, Input.Length) cryptoStream.FlushFinalBlock() Return Convert.ToBase64String(stream.ToArray()) End Function Public Shared Function ToBase32(ByVal sDataToDecrypt As String) As String Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream Dim key As Byte() = Encoding.Default.GetBytes(ENCRYPT\_KEY.PadRight(24, Chr(0))) Dim Vector As Byte() = Encoding.Default.GetBytes(ENCRYPT\_VECTOR.PadRight(8, Chr(0))) Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider Dim cryptoStream As CryptoStream = New CryptoStream(stream, des.CreateDecryptor(key, Vector), CryptoStreamMode.Write) Dim Input() As Byte = Convert.FromBase64String(sDataToDecrypt) cryptoStream.Write(Input, 0, Input.Length) cryptoStream.FlushFinalBlock() Return Encoding.Default.GetString(stream.ToArray()) End Function
#End Region
End ClassWhat a curious mind needs to discover knowledge is noting else than a pin-hole.
-
Please check the following sample, I think you can also include null strings checking. I hope it works out for you. :)
Imports System.Text
Imports System.Security.CryptographyPublic Class Encryption
#Region "Public Routines"
const ENCRYPT_KEY AS STRING = "TEST"
const ENCRYPT_VECTOR AS STRING = "CODEPRJT"Public Shared Function ToBase64(ByVal sDataToEncrypt As String) As String Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream Dim key As Byte() = Encoding.Default.GetBytes(ENCRYPT\_KEY.PadRight(24, Chr(0))) Dim Vector As Byte() = Encoding.Default.GetBytes(ENCRYPT\_VECTOR.PadRight(8, Chr(0))) Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider Dim cryptoStream As CryptoStream = New CryptoStream(stream, des.CreateEncryptor(key, Vector), CryptoStreamMode.Write) Dim Input() As Byte = Encoding.Default.GetBytes(sDataToEncrypt) cryptoStream.Write(Input, 0, Input.Length) cryptoStream.FlushFinalBlock() Return Convert.ToBase64String(stream.ToArray()) End Function Public Shared Function ToBase32(ByVal sDataToDecrypt As String) As String Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream Dim key As Byte() = Encoding.Default.GetBytes(ENCRYPT\_KEY.PadRight(24, Chr(0))) Dim Vector As Byte() = Encoding.Default.GetBytes(ENCRYPT\_VECTOR.PadRight(8, Chr(0))) Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider Dim cryptoStream As CryptoStream = New CryptoStream(stream, des.CreateDecryptor(key, Vector), CryptoStreamMode.Write) Dim Input() As Byte = Convert.FromBase64String(sDataToDecrypt) cryptoStream.Write(Input, 0, Input.Length) cryptoStream.FlushFinalBlock() Return Encoding.Default.GetString(stream.ToArray()) End Function
#End Region
End ClassWhat a curious mind needs to discover knowledge is noting else than a pin-hole.
-
its throws an error message : Specified key is a known weak key for 'TripleDES' and cannot be used
Arindam Banerjee Sr. Software Developer Rance Computer Pvt Ltd. Kolkata (India)