I am trying to decrypt my password stored in aspnet_membership table...
I am using the following code,
Dim encodedPassword() As Byte = System.Text.Encoding.UTF8.GetBytes(password)
encodedPassword = MyBase.EncryptPassword(encodedPassword)
Dim decryptedPassword() As Byte = MyBase.DecryptPassword(encodedPassword)
If (decryptedPassword IsNot Nothing) Then
Return System.Text.Encoding.UTF8.GetString(decryptedPassword, 0, decryptedPassword.Length)
End If
but at the line of DecryptPassword(encodedPassword) it shows error as
"Length of the data to decrypt is invalid."
I think you need to Base64 Decode it first:
byte[] encodedPassword = Convert.FromBase64String(pass);
byte[] bytes = this.DecryptPassword(encodedPassword);
or in VB.NET:
Dim encodedPassword As Byte() = Convert.FromBase64String(pass)
Dim bytes As Byte() = Me.DecryptPassword(encodedPassword)
Edit: As #Eranga pointed out, this is provided that the MembershipProvider used actually supports decryption and for the default provider the passwordFormat setting controls if it's "hashed", "encrypted" or "plain". By default the setting is "hashed", which means no decryption possible.
Encrypted passwords are base64 encoded before being saved to the database and for that reason they need to be decoded before being decrypted.
Related
I have a asp.net webapp, and runs in private network
I want determine PCs clients for
allow or block the requests
knows who is connected
connects only by special PCs
and more
I used IP Addresses, but anyone can connect when change the IP Address by one that passed in webapp, I want users login by just some PCs . any suggestions ?
LOGIN and PASSWORD going to a database of preferences... The best way to control everything about users (IP control can be bypassed, it´s not a valid control).
UPDATE
New method to pass info in URL:
To get hardware information:
Dim query As New SelectQuery("Win32_bios")
Dim search As New ManagementObjectSearcher(query)
Dim info As ManagementObject
For Each info In search.Get()
BIOS_Msg = info("version").ToString()
Next
Dim searchMainboard As New ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem")
Dim infoMain As ManagementObject
For Each infoMain In searchMainboard.Get()
Computer_Mainboard = infoMain("model").ToString()
Next
To encode Strings:
You may set a KEY and IV as byte array. The key may change according date and even some specific parameters according to the current time.
Add also:
Imports System.Security.Cryptography
Imports System.Text
Friend Function EncryptStringToBytes_Aes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As String
' Check arguments.
Dim encrypted() As Byte
' Create an AesCryptoServiceProvider object
' with the specified key and IV.
Using aesAlg As New AesCryptoServiceProvider()
aesAlg.Key = Key
aesAlg.IV = IV
Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
' Create the streams used for encryption.
Using msEncrypt As New MemoryStream()
Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
Using swEncrypt As New StreamWriter(csEncrypt)
'Write all data to the stream.
swEncrypt.Write(plainText)
End Using
encrypted = msEncrypt.ToArray()
End Using
End Using
End Using
' Return the encrypted bytes from the memory stream.
Return Convert.ToBase64(encrypted)
End Function 'EncryptStringToBytes_Aes
To decrypt it:
Friend Function DecryptStringFromBytes_Aes(ByVal cipherText As String, ByVal Key() As Byte, ByVal IV() As Byte) As String
Dim plaintext As String = Nothing
Dim SourceText as Byte() = Convert.FromBase64(CipherText)
' Create an AesCryptoServiceProvider object
' with the specified key and IV.
Using aesAlg As New AesCryptoServiceProvider()
aesAlg.Key = Key
aesAlg.IV = IV
' Create a decrytor to perform the stream transform.
Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)
' Create the streams used for decryption.
Using msDecrypt As New MemoryStream(SourceText)
Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
Using srDecrypt As New StreamReader(csDecrypt)
' Read the decrypted bytes from the decrypting stream
' and place them in a string.
plaintext = srDecrypt.ReadToEnd()
End Using
End Using
End Using
End Using
Return plaintext
End Function 'DecryptStringFromBytes_Aes
Using these functions, you can:
- Get specific hardware info;
- Encode any string and convert it to Base64
- Pass them in URL using normal convention.
- Receive them in your code-behind and translate them.
I guess that´s the best way to get reliable and specific information of each workstation instead IP-number. You may also work with the Windows Serial Number.
I am using AES CBC encryption in VB.Net and decryption is done using AS3Crypto. First few characters (about 16) are missing during decryption and replaced with random characters like below.
05[ÚðÊ\ÃPôôÄ]óbR
Here is my .net code. On AS3Crypto demo page, I use Secret Key > AES > CBC. I tried with different settings for Padding and Key Formats still no luck.
Thanks.
Dim plainText = txt2encrypt.Text.Trim
Dim encrypted() As Byte '
Using aesAlg As New AesCryptoServiceProvider()
aesAlg.Mode = CipherMode.CBC
' Create a decrytor to perform the stream transform.
Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
' Create the streams used for encryption.
Using msEncrypt As New MemoryStream()
Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
Using swEncrypt As New StreamWriter(csEncrypt)
'Write all data to the stream.
swEncrypt.Write(plainText)
End Using
encrypted = msEncrypt.ToArray()
End Using
End Using
Dim encryptedText = Convert.ToBase64String(encrypted)
txtkey.Text = Convert.ToBase64String(aesAlg.Key)
txtiv.Text = Convert.ToBase64String(aesAlg.IV)
txtkeysize.Text = aesAlg.KeySize
txtencrypted.Text = encryptedText
txtpadding.Text = aesAlg.Padding
End Using
I am developing Windows Phone 7 application in which I send encrypted data to a webservice that decrypts it. I'm using the protectedData.protect which is only working properly as long as I am encrypting and decrypting from my application (for testing only); once I use the webservice the behaviour changes.
This is the code from the webservice:
If Flag = False Then ' Decrypt
Dim ProtectedPinByte As Byte() = Encoding.UTF8.GetBytes(password)
Dim PinByte2 As Byte() = ProtectedData.Unprotect(ProtectedPinByte, Nothing, DataProtectionScope.CurrentUser)
password = Encoding.UTF8.GetString(PinByte2, 0, PinByte2.Length)
Return Bll.Utilites.EncryptDecryptStr(True, password) 'Encrypts using another algorithm
Else ' Encrypt
EncPassword = Bll.Utilites.EncryptDecryptStr(False, password) 'decrypts from another algorithm
Dim PinByte As Byte() = Encoding.UTF8.GetBytes(EncPassword)
Dim ProtectedPinByte As Byte() = ProtectedData.Protect(PinByte, Nothing, DataProtectionScope.CurrentUser)
Return Encoding.UTF8.GetString(ProtectedPinByte, 0, ProtectedPinByte.Length)
End If
This isn't possible as the ProtectedData class on Windows Phone uses device credentials to encrypt the data, it can only be decrypted on that device.
In Silverlight for Windows Phone, both the user and machine
credentials are used to encrypt or decrypt data.
http://msdn.microsoft.com/en-us/library/system.security.cryptography.protecteddata(v=vs.96).aspx
I use the following two methods to encrypt and decrypt strings:
'Encrypts string. Returns encrypted byte array.
Public Function Encrypt(ByVal str As String) As Byte()
Dim inputInBytes() As Byte = Encoding.Unicode.GetBytes(str)
Dim laesProvider As New AesCryptoServiceProvider()
laesProvider.Key = _key
laesProvider.Mode = CipherMode.CBC
laesProvider.IV = _IV
laesProvider.Padding = PaddingMode.PKCS7
Dim lencryptor As ICryptoTransform = laesProvider.CreateEncryptor
Dim encryptedStream As New MemoryStream
Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, lencryptor, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
encryptedStream.Position = 0
Dim result(encryptedStream.Length - 1) As Byte
encryptedStream.Read(result, 0, encryptedStream.Length)
cryptStream.Close()
Return result
End Function
'Decrypts bytearray. Returns string.
Public Function DecryptToStr(ByVal inputInBytes() As Byte) As String
Dim laesProvider As New AesCryptoServiceProvider()
laesProvider.Key = _key
laesProvider.Mode = CipherMode.CBC
laesProvider.IV = _IV
laesProvider.Padding = PaddingMode.PKCS7
Dim ldecryptor As ICryptoTransform = laesProvider.CreateDecryptor
' Provide a memory stream to decrypt information into
Dim decryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, ldecryptor, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock() '#### This is where the exception is thrown ####
decryptedStream.Position = 0
' Read the memory stream and convert it back into a string
Dim result(decryptedStream.Length - 1) As Byte
decryptedStream.Read(result, 0, decryptedStream.Length)
cryptStream.Close()
Return Encoding.Unicode.GetString(result)
End Function
The error occurs when attempting to decrypt certain length strings. When the string is a social security # (11 chars including dashes) then is throws "The input data is not a complete block" CryptographicException. If I pass in for example a string that is exactly 8 characters long, then everything works as expected. I thought that the PKCS7 padding would take care of the various lengths. I'm sure that I'm missing something simple, but after hours of googling, the answer eludes me.
The issue wasn't in the encryption method, it was in the length of the varbinary set in the database where it was being stored. So the encrypted data was being truncated.
I can't seem to get past this problem, I've created what I assume to be a 256-bit key using the random generator at GRC and combined that with my IV. I keep getting the error below:
Specified key is not a valid size for this algorithm.
Any help is gratefully received, here is the code I am using to Encrypt/Decrypt:
Private Function Rijndael(ByVal sInput As String, ByVal bEncrypt As Boolean) As String
' Setup the Key and Initialization Vector.
Dim byteKey As Byte() = Encoding.UTF8.GetBytes("C3CA193570B26E5C3CBB50FD805A0E23BAFFABA135E82C41517EEDCB9B7C90AC")
Dim byteIV As Byte() = Encoding.UTF8.GetBytes("c+O2r)J~?L:$]u[2")
' Create an instance of the encyrption algorithm.
Dim _rijndael As New RijndaelManaged()
' Create an encryptor using our key and IV
Dim transform As ICryptoTransform
If bEncrypt Then
transform = _rijndael.CreateEncryptor(byteKey, byteIV)
Else
transform = _rijndael.CreateDecryptor(byteKey, byteIV)
End If
' Create the streams for input and output
Dim msOutput As New System.IO.MemoryStream()
Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
' Feed data into the crypto stream.
msInput.Write(Encoding.UTF8.GetBytes(sInput), 0, Encoding.UTF8.GetBytes(sInput).Length)
' Flush crypto stream.
msInput.FlushFinalBlock()
Return Convert.ToBase64String(msOutput.ToArray)
End Function
C3CA193570B26E5C3CBB50FD805A0E23BAFFABA135E82C41517EEDCB9B7C90AC is hex code for a 256-bit-long bitstream.
But Encoding.UTF8.GetBytes does not turn hex code into the corresponding bytes in the way you were thinking.
Because you had 64 characters in that string, you get 64 bytes of UTF-8 bytes. That's a 512-bit-long bitstream.