I need to know a good method of encrypting a simple text file
Printable View
I need to know a good method of encrypting a simple text file
You come up with your own algorithm, and then just run through the file.
Algorithms are usually based on some maths thing.
Eg. RSA is based on modular arithmetic.
- jamie
What do you mean by 'good'?
Do you mean that the encryption is almost completely secure, or do you want an easy and quick way to hide data from the casual observer?
If you want a reasonably secure algorithm, EnDcryptFile makes a mess of your file, call EnDcryptFile again (with the same password) and it will put it back together again.
This encryption CAN be broken, although it is secure if the password is random and at least as long as the file content.
Public Sub EnDcryptFile(Filename As String, password As String)
Dim filenum As Integer
Dim filedata As String
filenum = FreeFile
On Error Resume Next
Open Filename For Binary As #filenum
filedata = Space$(LOF(filenum))
Get #1, , filedata
Seek #filenum, 1
Put #1, , EnDcrypt(filedata, password)
Close #filenum
End Sub
Public Function EnDcrypt(inputstr As String, Pword As String) As String
Dim pStr As String
Dim mUpDown As Boolean
Dim ix As Long
Dim jx As Long
mUpDown = True
pStr = inputstr
jx = 0
For ix = 1 To Len(pStr)
jx = jx + 1
If jx = Len(Pword) Then jx = 1
mUpDown = Not mUpDown
If mUpDown Then
Mid$(pStr, ix, 1) = Chr$(Asc(Mid$(pStr, ix, 1)) Xor Asc(Mid$(Pword, jx, 1)) + 1)
Else
Mid$(pStr, ix, 1) = Chr$(Asc(Mid$(pStr, ix, 1)) Xor Asc(Mid$(Pword, jx, 1)) - 1)
End If
Next ix
EnDcrypt = pStr
End Function
Encrypting in VB s very slow. Unless there only a few small files then you can use VB but its best to use something written in something like C++, which, if in the form of a dll can be called from a vb program. The following link is to blowfish encryption. In vb, it takes 90 seconds to encrypt a 2.5mb file, but using the dll on this page, it is done in 3:
http://www.di-mgt.com.au/crypto.html#BlowfishVB
Well, here is how to do it using RSA.
It is extremely secure.
The code is in Mathematica format, but its easy to convert.
- jamieCode:message = ToCharacterCode["HOW COMES I AM SITTING HERE DOING THIS ETEST?"];
p = 251;
PrimeQ[p]
q = 953;
PrimeQ[q]
n = p * q;
m = EulerPhi[n];
e = 293;
GCD[e, m];
{e, n}
encrypt[m_, e_, n_] :=
Module[{f},
f = Function[b, PowerMod[b, e, n]];
Map[f, m]
];
makeDecryptKey[e_, p_, q_] :=
Module[{m = (p - 1)*(q - 1)},
PowerMod[e, EulerPhi[m] - 1, m]
];
dKey = makeDecryptKey[e, p, q]
cm = encrypt[message, e, n]
decrypt[cm_, d_, n_] :=
Module[{f},
f = Function[b, PowerMod[b, d, n]];
Map[f, cm]
];
FromCharacterCode[decrypt[cm, dKey, n]]
Could you give an example of converting this, cos I can't seem to work it out... a description of variables would also be nice???
Ad
Try looking up "Encryption" at www.planet-source-code.com. You'll find many examples.