0

Below is the code snippet from vb.net. I want to convert it to python. I used hashlib, hmac and also pyDes but none produced the same result as by the vb program. Any Suggestions.? This is my first time dealing with encryption. Please help me sort this problem.. code:

Imports Microsoft.VisualBasic.CompilerServices
Imports System
Imports System.Diagnostics
Imports System.Security.Cryptography
Imports System.Text

Namespace _Cargo
    Public Class Crypto
        Private Shared DES As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()

        Private Shared MD5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()

        <DebuggerNonUserCode()>
        Public Sub New()
        End Sub

        Public Shared Function MD5Hash(value As String) As Byte()
            Return Crypto.MD5.ComputeHash(Encoding.ASCII.GetBytes(value))
        End Function

        Public Shared Function Encrypt(stringToEncrypt As String) As String
            Crypto.DES.Key = Crypto.MD5Hash("L6#F&,q2$xLx")
            Crypto.DES.Mode = CipherMode.ECB
            Dim bytes As Byte() = Encoding.ASCII.GetBytes(stringToEncrypt)
            Return Convert.ToBase64String(Crypto.DES.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length))
        End Function

        Public Shared Function Decrypt(encryptedString As String) As String
            Dim result As String
            Try
                Crypto.DES.Key = Crypto.MD5Hash("L6#F&,q2$xLx")
                Crypto.DES.Mode = CipherMode.ECB
                Dim array As Byte() = Convert.FromBase64String(encryptedString)
                result = Encoding.ASCII.GetString(Crypto.DES.CreateDecryptor().TransformFinalBlock(array, 0, array.Length))
                Return result
            Catch expr_4D As Exception
                ProjectData.SetProjectError(expr_4D)
                ProjectData.ClearProjectError()
            End Try
            result = Nothing
            Return result
        End Function
    End Class
End Namespace
1
  • So, what did you try? What didn't work? Commented Dec 25, 2013 at 12:11

2 Answers 2

1

Check out the following code snippet

SIGNATURE OF THE DES

pyDes.des(key, [mode], [IV], [pad], [padmode])

from pyDes import *

data = "Please encrypt my data"
k = des("DESCRYPT", ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)

Update

I have converted vb.net code to python and it's working fine

from pyDes import *
import hashlib
import base64
key = hashlib.md5("L6#F&,q2$xLx").digest()
data = "I love security"
k = triple_des(key, ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
base64Encrypted= base64.b64encode(d)
print "Encrypted: %r" % d
base64Decrypted= base64.b64decode(base64Encrypted)
print "Decrypted: %r" % k.decrypt(base64Decrypted)
Sign up to request clarification or add additional context in comments.

5 Comments

sir, where am I supposed to enter the key..?
check out the signature of the des method.First parameter is the key
I tried the above code but it throws an error. It says Invalid key size. The keys must be exactly 8 bytes long. In my case the key is 12 bytes long..
key length should by 8/16 bytes for DES.
Are there any other encryption which is referred by the above snippets..? Its giving me a massive headache.
0

You can use the Crypto library, very handy for all cryptography related stuff

from Crypto.Cipher import DES
from Crypto.Hash import MD5
from Crypto import Random

def encrypt(string, key):
    hash = MD5.new(key).digest()
    iv = Random.new().read(DES.block_size)
    cipher = DES.new(hash, DES.MODE_ECB, iv)
    return iv + cipher.encrypt(string)

3 Comments

Thanks for the reply sir. This throws an error when I call it : encrypt('Convert This','12 digit key') . The error is: global name 'DES3' is not defined
Yes, it's DES not DES3. DES is no longer used and even triple DES(DES3) is not considered secured nowadays. I would better use AES if I were you.
Tried accordingly but was unfortunate again. The result produced by the above way didn't match the encryption function of dotnet. Complete code of crypto is:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.