The MASM Forum

General => The Soap Box => Topic started by: cman on November 25, 2015, 04:29:18 AM

Title: Simple Encryption Algorithm
Post by: cman on November 25, 2015, 04:29:18 AM
What is a simple but fairly secure algorithm for encrypting text files? I don't know much about cryptography , so I though someone could point me to an algorithm I could just "plug into" my project. It almost seems like I could just "make one up" by applying a sequence of operations to the characters in the files to encrypt them , and then reversing the process to decrypt  , but I'm sure there is more to encryption then that.  :biggrin: Thanks for any information...
Title: Re: Simple Encryption Algorithm
Post by: dedndave on November 25, 2015, 04:42:51 AM
assuming you want a plain text file when encrypted.....

just use a translation table for characters:
30 to 39 hex, inclusive (numeric digits 0 to 9)
41 to 5A hex, inclusive (upper case letters)
61 to 7A hex, inclusive (lower case letters)
Title: Re: Simple Encryption Algorithm
Post by: FORTRANS on November 25, 2015, 05:22:15 AM
Hi,

What is a simple but fairly secure algorithm for encrypting text files?

   That depends on how hard you want to work and who you want
to hide the text from.  Dave's idea is good if you want to hide text
from someone that is not very informed about cryptography.  Though
I would add punctuation symbols and white space.  That can create
simple text files, so you do not have to worry about binary files.

   Another simple process is to exclusive or your text with a pattern
generated by a pass phrase.  Prompt for the pass phrase, AND it
with 01FH to preserve the high bits of the text file, and exclusive or
the phrase with your text file.  Don't use a very short phrase though.
To recover the original text, repeat the process with the same pass
phrase.

   Either of those algorithms will encrypt your text against most people.
If you want to hide text from more knowledgeable people, you will
need a better algorithm.
 
Regards,

Steve N.
Title: Re: Simple Encryption Algorithm
Post by: ragdog on November 25, 2015, 05:32:18 AM
Hello

A simply encryption is a Xor encryption but not secure.
Title: Re: Simple Encryption Algorithm
Post by: cman on November 25, 2015, 06:11:02 AM
Ok , thanks for the replies ! Searching around on the internet I see simple algorithms like "TEA" and "Threefish" and other algorithms that are composed of a few simple lines of code. I wonder if any of these would be sufficient. I guess I could pull an "Algorithm Analysis" textbook off my shelf , I guess I'm getting lazy. :biggrin:
Title: Re: Simple Encryption Algorithm
Post by: hutch-- on November 25, 2015, 08:29:37 AM
There are two items in the MASM32 SDK that can help you if you are working in MASM. A simple but fast stream XOR algo called "xordata" in the masm32 library and a specialised tool called RPG.EXE (example05\rpg\"that creates reasonably good quality random pads. The longer the pad, the harder it is to break and the less often you use the same pad the better the security is.
Title: Re: Simple Encryption Algorithm
Post by: Farabi on November 25, 2015, 02:23:23 PM
The most simplest and confusing one is by adding each charater of your file with the password concequtively, sorry about the typo.
Title: Re: Simple Encryption Algorithm
Post by: cman on November 26, 2015, 08:11:29 AM
Thanks for all your suggestions! I've been looking at the TEA ( tiny encryption algorithm ) to write into my project. I might try this algorithm as I think it might be fun to use something somewhat secure. The code is simple and I've even found an assembly language implementation: ( AT&T syntax  :( )  http://www.nayuki.io/page/tiny-encryption-algorithm-in-x86-assembly
Title: Re: Simple Encryption Algorithm
Post by: jj2007 on November 26, 2015, 11:51:36 AM
I've just hacked together an encrypter-decrypter pair based on Rand() (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1030). Sources and executables attached, usage should be straightforward: Extract all to a folder, then drag whatever file over Encrypt.exe

It is fast and secure as long as you don't just accept the suggested password ;)

"Secure" means it looks 100% random. Here is the ENT analysis (http://www.fourmilab.ch/random/) of a combined Windows.inc + WinExtra.inc 2MB file:

Original:
Code: [Select]
ent \masm32\include\combined.inc
Entropy = 4.678218 bits per byte.

Optimum compression would reduce the size
of this 2045966 byte file by 41 percent.

Chi square distribution for 2045966 samples is 49984775.66, and randomly
would exceed this value less than 0.01 percent of the times.

Arithmetic mean value of data bytes is 62.6084 (127.5 = random).
Monte Carlo value for Pi is 4.000000000 (error 27.32 percent).
Serial correlation coefficient is 0.581272 (totally uncorrelated = 0.0).

Encrypted:
Code: [Select]
ent combi.tmp
Entropy = 7.999906 bits per byte.

Optimum compression would reduce the size
of this 2045972 byte file by 0 percent.

Chi square distribution for 2045972 samples is 267.73, and randomly
would exceed this value 27.97 percent of the times.

Arithmetic mean value of data bytes is 127.5636 (127.5 = random).
Monte Carlo value for Pi is 3.141430226 (error 0.01 percent).
Serial correlation coefficient is -0.000726 (totally uncorrelated = 0.0).
Title: Re: Simple Encryption Algorithm
Post by: K_F on November 26, 2015, 05:32:28 PM
This should tickle your fancy....  https://en.wikipedia.org/wiki/Enigma_machine (https://en.wikipedia.org/wiki/Enigma_machine)
Title: Re: Simple Encryption Algorithm
Post by: ragdog on November 27, 2015, 05:24:03 AM
Ohh lol Enigma

But for a tool to encrypt/decrypt messages or Files think i is it bad
Other good way is Rsa greater 512 better 1024
Title: Re: Simple Encryption Algorithm
Post by: GoneFishing on November 27, 2015, 05:44:46 AM
The simplest one : https://en.wikipedia.org/wiki/Caesar_cipher
More advanced    : Vigenère cipher (https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher)

Code: [Select]
include \masm32\include\masm32rt.inc

VigenereTable MACRO
     FORC char , <ABCDEFGHIJKLMNOPQRSTUVWXYZA>
          pos INSTR <ABCDEFGHIJKLMNOPQRSTUVWXYZ>, <&char>
          tmp$ SUBSTR <ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ>,pos,26
          %echo tmp$
    ENDM
ENDM

DECRYPT MACRO Ciphertext, key
    local cnt
    cnt = 1
    FORC char, <&Ciphertext>
         keychar SUBSTR <&key>,cnt,1
         pos   INSTR <ABCDEFGHIJKLMNOPQRSTUVWXYZ>, keychar
         line$ SUBSTR <ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ>,pos,26
         pos_1 INSTR line$, <&char>
         origChar SUBSTR  <ABCDEFGHIJKLMNOPQRSTUVWXYZ>,pos_1,1
         %echo origChar
        cnt =cnt+1
    ENDM
ENDM

.code
        start:

        VigenereTable

        DECRYPT  LXFOPVEFRNHR, LEMONLEMONLE

 .err

end start

OUTPUT:
Quote
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG
IJKLMNOPQRSTUVWXYZABCDEFGH
JKLMNOPQRSTUVWXYZABCDEFGHI
KLMNOPQRSTUVWXYZABCDEFGHIJ
LMNOPQRSTUVWXYZABCDEFGHIJK
MNOPQRSTUVWXYZABCDEFGHIJKL
NOPQRSTUVWXYZABCDEFGHIJKLM
OPQRSTUVWXYZABCDEFGHIJKLMN
PQRSTUVWXYZABCDEFGHIJKLMNO
QRSTUVWXYZABCDEFGHIJKLMNOP
RSTUVWXYZABCDEFGHIJKLMNOPQ
STUVWXYZABCDEFGHIJKLMNOPQR
TUVWXYZABCDEFGHIJKLMNOPQRS
UVWXYZABCDEFGHIJKLMNOPQRST
VWXYZABCDEFGHIJKLMNOPQRSTU
WXYZABCDEFGHIJKLMNOPQRSTUV
XYZABCDEFGHIJKLMNOPQRSTUVW
YZABCDEFGHIJKLMNOPQRSTUVWX
ZABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ
A
T
T
A
C
K
A
T
D
A
W
N


TESTED WITH JWASM
Title: Re: Simple Encryption Algorithm
Post by: GoneFishing on November 27, 2015, 09:37:16 AM
DECIPHER ME:
Quote
FUWXU KHW HZO IKEQ LVIL EPX ZSCDWP YWGG
 FMCEMY ZN FRWKEJA RVS LHMPQW NYJHKR
 KHSV JXXE FHR QTCJEX JIO KKA EEIZTU
 AGO EKXEKHWY VYM QEOADL PTMGKBRKH
HINT:  Ex-Libris by Ross King
Title: Re: Simple Encryption Algorithm
Post by: K_F on November 27, 2015, 09:49:29 PM
Ohh lol Enigma

But for a tool to encrypt/decrypt messages or Files think i is it bad
Other good way is Rsa greater 512 better 1024
Ahh!.. Enigma was cracked because the coding machine 'mechanics' was known..
For computer file encryption and transfer, the 'encoding machine' is unknown to the eaves-dropper.
RSA 256/1024 size styles could just as easily be made up of many more enigma wheels.
Each wheel's content and movement only known to the user, and these variables can be changed by code within the message.

It's simple to implement in asm, and can be very effective.
Every code is crackable.. even RSA has been cracked.
 :biggrin:
Title: Re: Simple Encryption Algorithm
Post by: ragdog on November 27, 2015, 11:41:07 PM
Quote
even RSA has been cracked

Sure but Rsa is secure the last broken key is 768 but to factorize >768 it cost many time (years)

Quote
RSA-768

The effort took almost 2000 2.2GHz-Opteron-CPU years according to the submitters, just short of 3 years of calendar time.



I think by factor a key >768  say your Cpu (home pc) good night  :biggrin:


Title: Re: Simple Encryption Algorithm
Post by: jj2007 on November 28, 2015, 05:22:20 AM
The effort took almost 2000 2.2GHz-Opteron-CPU years according to the submitters, just short of 3 years of calendar time.

Could be worse (http://masm32.com/board/index.php?topic=4855.0) ;)
Title: Re: Simple Encryption Algorithm
Post by: xanatose on December 01, 2015, 01:30:02 AM
Assuming binary file output and that the size is large enough to bother:

1. Compress the file with a custom header. The result will look like garbage unless you know the header you are using. (for a good compression library http://7-zip.org/sdk.html).

2. Encrypt the result with whatever encryption you want.

That means that the attacker would need to know how to decrypt the file. And how to uncompress it.
Is important that the compression takes place BEFORE the encryption. Otherwise the file will not compress.

To recover the file.
1. Decrypt the file.
2. Decompress the file.

Not perfect, but should be enough for most cases. Not the NSA. But most cases.
Title: Re: Simple Encryption Algorithm
Post by: K_F on December 01, 2015, 08:45:50 AM
All it takes.. is one person to think of something different.. and there's panic in the 'control room'.

PGP is the perfect example.. one person with an idea - and the CIA went ballistic (probably coz they were stupid then ;) )
Now there are 6 billion+ people around with as many ideas.... enough to cause CIA/KBG/Whatever good many well deserved heart attacks.
 :biggrin:
Title: Re: Simple Encryption Algorithm
Post by: hutch-- on December 01, 2015, 09:39:47 AM
I have a highly cynical view on popular encryption methods, if it has a bit count, its already dead. Comparisons to some clapped out old Opteron is not telling you what is going on. National security agencies all over the world have dedicated hardware, massive multi-parallel processor capacity for brute forcing ordinary methods of encryption. There are not that many decent alternatives but any that are vaguely useful are conceptual, not just winding up the bit count.

Anyone who has ever done cryptic crosswords (my mum was a whizz at them but I hated them) knows that the more abstract the association, the more difficult it is to get the result. Then you can have single symbols that represent phrases,  non word character collections that represent ideas and there is a massive range of variation available to do things like this. My favourite that can at least be done on a computer is high quality random pads that are then simply xorred to the text message. The high quality random pad is very large and must be securely transferred to both ends of the communications but once it is done, its very hard to beat.

Look at the stats for every character having the range of 0 to 255 and with no consistent method of transferring one character to another. 1st character is one in 256, second character is one in 256. Just with 2 characters you have the range of 256 * 256. Now this accelerating level of complexity comes with a couple of provisos, the reason why you need a massive random pad is so you can always choose an unused part of the pad for the encryption as reuse of the identical pad weakens the security of the technique. The other consideration is verification of attempts to break the encryption, if the encrypted data has no format or header then there is no routine way of testing if there has been a successful result.

Now xanatose has a good idea that makes verification even more complex, if the data is scrambled with compression then even a successful breaking of some or part of the encrypted data still looks like garbage. Using a different encryption method before running the data against a random pad will do much the same.

Now if you were a Mossad agent prying secret data out of Iran, you would take a CD of the speeches of the Ayotollah Khomenie  as your pad, if you were a Canadian business spy stealing trade secrets from the US, you would take a CD or more probably a blue ray data disk of Joe Biden's speeches on foreign policy and use that for your secret transmissions. The drift is to do something that is different and you may keep them in the dark for a millenium or two.
Title: Re: Simple Encryption Algorithm
Post by: MichaelW on December 01, 2015, 12:07:10 PM
There is an improved version of TEA  here (https://en.wikipedia.org/wiki/XTEA), but it looks like an efficient assembly implementation will require more registers than you have available in 32-bit code.
Title: Re: Simple Encryption Algorithm
Post by: Farabi on December 01, 2015, 07:41:18 PM
I think all webserver is secure enough. I saw M-SQL server, I had no idea how to break in. So if I were CIA, NSA, KGB, I`ll focused on brute-force. But I saw social engineering can still be worked. I was wondered how to make it more secure, from social engineering.

I can see that lots of mistake was
1. Distraction
2. Trust
3. Appearance.

That is my personal weakness. I thought a good tablet was that have a screen that can see from any direction, but I was mistaken. My old Toshiba laptop, which I own at 1999 was having a good screen. Where no one can see it from another angle. No wonder my dad bought it.