Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

The "Tiny encryption algorithm"

michael_green
Beginner
1,848 Views
Hi all,
I needed to do some simple encryption work and found, on the Web, the answer to my problem in Wheeler & Needham's Tiny Encryption Algorithm. The only problem was that it was written in C, which I really don't understand very well - C seems ironically appropriate for writing an encryption algorithm. All attempts to find a Fortran version failed, the best I could do was find a version written in Java. So I did the hard work and wrote it in Fortran. I post it here in case anyone else needs it ...
subroutine encrypt(num,k1,k2,k3,k4)
implicit none
integer*8num,sum,delta,number
integer*4n(2),k1,k2,k3,k4,n1,n2,i
equivalence(number,n)
!***********************************************************
number = num
delta = 2654435769
sum = 0
n1 = n(1)
n2 = n(2)
do i = 1,32
sum = sum + delta
n1 = n1 + xor(xor(isha(n2,4)+k1,n2+sum),isha(n2,-5)+k2)
n2 = n2 + xor(xor(isha(n1,4)+k3,n1+sum),isha(n1,-5)+k4)
end do
n(1) = n1
n(2) = n2
num = number
return
end
!!
subroutine decrypt(num,k1,k2,k3,k4)
implicit none
integer*8num,sum,delta,number
integer*4n(2),k1,k2,k3,k4,n1,n2,i
equivalence(number,n)
!***********************************************************
number = num
delta = 2654435769
sum = 3337565984
n1 = n(1)
n2 = n(2)
do i = 1,32
n2 = n2 - xor(xor(isha(n1,4)+k3,n1+sum),isha(n1,-5)+k4)
n1 = n1 - xor(xor(isha(n2,4)+k1,n2+sum),isha(n2,-5)+k2)
sum = sum - delta
end do
n(1) = n1
n(2) = n2
num = number
return
end
Links to the original paper and others, complete with theory, can be found at http://www.simonshepherd.supanet.com/tea.htm
In my version, num is an integer of up to 20 digits which is returned encrypted. The values k1,k2,k3,k4 are random integers (I used 9 digit integers) which must be the same for both encrypt and decrypt. I confess I don't understand the significance of the particular values for delta and sum as used in the original paper.
I'd be glad to hear of any comments or criticism.
Good luck
Mike
0 Kudos
1 Reply
Steven_L_Intel1
Employee
1,848 Views
I am not an expert in encryption, but many years ago I found a similar algorithm and wrote routines for it. It was pointed out to me that the algorithm wasn't very good, as encryption goes. Of course, it all depends on your needs - simple encryption for the purpose of making it annoyingly difficult for most people to gain unauthorized access may well be sufficient. I put something like this into the evaluation license code for CVF.

There is a CRYPTO sample in CVF 6.6, which also works in Intel Fortran. It is based on an MSDN sample and uses Windows' encryption services. Probably overkill for you. See ftp://ftp.compaq.com/pub/products/fortran/vf/supp/crypto.zip
0 Kudos
Reply