I need a .NET (C# ideally) algorithm that will produce the same results as a vb6 one I made to encrypt and decrypt binary files. I've been trying to hunt down code for a week now and have had little success.
Encrypt Decrypt Vb6 Source Code
Download: https://tinourl.com/2vKH36
I started with How to encrypt a string in VB6 decrypt VB.NET but it was only a half solution and it used RC2 rather than AES. After some fiddling I got my vb6 code to work in AES but I cannot find a way to get c# to produce the same results. I think it has to do with the way I'm making my Key and IV. I've been going over Cryptographic Services my attempts can be broken into two basic types. One where I have tried PasswordDeriveBytes.CryptDeriveKey and trying to make it work with aes. No success at all. And Tries where I tried to mimic it's behavior. Bellow I will post my vb6 code and my closest c# attempt. I can't say with 100% certainty that the vb6 is perfect so if changes need to be made there I'm open to it. I know there is a way to use p invoke and I know there is a way to make .net dll to be called from my vb apps. I would prefer not to go that way.
However in the C# code you are getting bytes for the ASCII text "0000000000000000" which is not the same as a byte array of the number zero 0, 0, 0, 0, ... 0 as the character '0' is equal to the number 48 in ASCII (see: ). I think this is likely to be the source of your problems.
Looking for a simple text encryption/decryption VB6 code. Ideally, the solution should accept (text, password) arguments and produce readable output (without any special characters), so it can be used anywhere without encoding issues.
For much stronger AES 256-bit encryption (in ECB mode) and proper handling of unicode texts/passwords you can check out Simple AES 256-bit password protected encryption as implemented in mdAesEcb.bas module (380 LOC).
Here's my encryption class. I use several constants to define the encryption key because in my mind it's a little more secure from someone trying to decompile the code to find it. Cryptography isn't my thing so maybe I'm kidding myself. Anyway, I used this class in an ActiveX dll called from other programs to do encryption and the reverse in a separate dll for decryption. I did it this way so people who shouldn't be seeing encrypted data don't even have the dll to do the decrypting. Change the key constants to what you want (5 long). I use a mix including unprintable characters and it has worked well for me so far. The CAPICOM is part of Windows so you don't have to distribute.
Joe's code itself is the best source for a description of how it works. He has thoroughly documented it and used an easy to follow programming style. Besides providing these functions in a ready to use .bas module, he has also wrapped them in a DLL.
The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.
Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!
This is a test of a rudimentary encryption/decryption algorithm.Note: It is possible for the encrypted string to include Chr$(0). Visual Basic handles this just fine but Windows (and therefore the TextBox control) uses Chr$(0) to signify the end of the string. Therefore, programs such as this one that store the encrypted data in a text box may end up truncating the data.
'This program may be distributed on the condition that it is'distributed in full and unchanged, and that no fee is charged for'such distribution with the exception of reasonable shipping and media'charged. In addition, the code in this program may be incorporated'into your own programs and the resulting programs may be distributed'without payment of royalties.''This example program was provided by:' SoftCircuits Programming' ' P.O. Box 16262' Irvine, CA 92623Option Explicit'Set to True to make the password case-sensitive#Const CASE_SENSITIVE_PASSWORD = FalsePrivate Sub cmdEncrypt_Click() ' You can encrypt twice for extra security txtText = EncryptText((txtText), txtPassword) txtText = EncryptText((txtText), txtPassword)End SubPrivate Sub cmdDecrypt_Click() txtText = DecryptText((txtText), txtPassword) txtText = DecryptText((txtText), txtPassword)End Sub'Encrypt textPrivate Function EncryptText(strText As String, ByVal strPwd As String) Dim i As Integer, c As Integer Dim strBuff As String#If Not CASE_SENSITIVE_PASSWORD Then 'Convert password to upper case 'if not case-sensitive strPwd = UCase$(strPwd)#End If 'Encrypt string If Len(strPwd) Then For i = 1 To Len(strText) c =Asc(Mid$(strText, i, 1)) c = c +Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1)) strBuff =strBuff & Chr$(c And &HFF) Next i Else strBuff = strText End If EncryptText = strBuffEnd Function'Decrypt text encrypted with EncryptTextPrivate Function DecryptText(strText As String, ByVal strPwd As String) Dim i As Integer, c As Integer Dim strBuff As String#If Not CASE_SENSITIVE_PASSWORD Then 'Convert password to upper case 'if not case-sensitive strPwd = UCase$(strPwd)#End If 'Decrypt string If Len(strPwd) Then For i = 1 To Len(strText) c =Asc(Mid$(strText, i, 1)) c = c -Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1)) strBuff =strBuff & Chr$(c And &HFF) Next i Else strBuff = strText End If DecryptText = strBuffEnd Function
I've added a sample project which includes a basic file browser to select a file to encrypt. There is no error checking in the project; it is simply to show the usage of the encryption and decryption functions.
Public Shared Function DESEncrypt(ByVal strKey As String, ByVal strData As String) As String Dim strValue As String = "" If strKey "" Then ' convert key to 16 characters for simplicity Select Case Len(strKey) Case Is strKey = strKey & Left("XXXXXXXXXXXXXXXX", 16 - Len(strKey)) Case Is > 16 strKey = Left(strKey, 16) End Select ' create encryption keys Dim byteKey() As Byte = Encoding.UTF8.GetBytes(Left(strKey, 8)) Dim byteVector() As Byte = Encoding.UTF8.GetBytes(Right(strKey, 8)) ' convert data to byte array Dim byteData() As Byte = Encoding.UTF8.GetBytes(strData) ' encrypt Dim objDES As New DESCryptoServiceProvider Dim objMemoryStream As New MemoryStream Dim objCryptoStream As New CryptoStream(objMemoryStream, objDES.CreateEncryptor(byteKey, byteVector), CryptoStreamMode.Write) objCryptoStream.Write(byteData, 0, byteData.Length) objCryptoStream.FlushFinalBlock() ' convert to string and Base64 encode strValue = Convert.ToBase64String(objMemoryStream.ToArray()) Else strValue = strData End If Return strValue End Function
I'd like to be able use the same routines from VB6 code, so that I can encrypt/decrypt strings in legacy apps that share data with my VB.Net app.Is this possible using the same routines or are there compatible classes I can reference in VB6 somewhere?
::blowfish::blowfish ?-mode [ecbcbc]? ?-dir [encryptdecrypt]? -key keydata ?-iv vector? ?-out channel? ?-chunksize size? ?-pad padchar? [ -in channel ?--? data ] ::blowfish::Init mode keydata iv ::blowfish::Encrypt Key data ::blowfish::Decrypt Key data ::blowfish::Reset Key iv ::blowfish::Final Key
The encryption password should not be hard-coded in the application. There are different approaches how the password can be provided to encrypt anddecrypt the database. In the case of EncryptedSharedPreferences the Android Keystore can be used to store the password. Other databasescan rely on EncryptedSharedPreferences to store passwords. The password can also be provided dynamically by the user of the applicationor it can be fetched from a remote server if the other methods are not feasible.
There are different approaches how the key can be provided to encrypt and decrypt the database. One of the most convinient way to is to rely onEncryptedSharedPreferences to store encryption keys. It can also be provided dynamically by the user of the application or fetched from aremote server.
Those programers who do use PIC in anger tend to be compiler writers or those who write code for limitied resource microcontrollers where some form of multitasking is a requirment. Often PIC is written in assembler, and if multiuser operation is required it will often be used to write a higher level interpreter that has the user space protection and gatbage collection mechanisms built in. 2ff7e9595c
Kommentit