def enc():

			#	this function is imported from tkFileDialog
			#	you can specify the filetypes of your choice 
			#	there is an other function called LoadFileDialog
			#	which also
			#	performs the same function.
					
					# RETURNS THE PATH OF THE INPUT FILE
					# TO BE ENCRYPTED
	path_ip=askopenfilename(filetypes=[("Path of file to be Encrypted","*"),("pythonfiles","*.py")])
	
					# CHECKS IF ENCRYPTION IS CANCELLED
	if path_ip == ():
		e1.insert(0,"   Encryption cancelled   ")
		return
	
					
					# RETURNS THE PATH OF THE INTERMEDIATE
					# FILE TO WRITE THE ENCRYPTED DATA
	path_im=asksaveasfilename(title="Path of Encrypted File",defaultextension=".smt",filetypes=[("Path of Encrypted file","*.smt")])
	
	
	fip=open(path_ip,'r')
	fim=open(path_im,'w')
	fim.close()
	fim=open(path_im,'a')
	START='1010101010101010'
	fim.write(START)
					# A ARRAY OF PRIME NO 2 B CREATED
	
	k=random.randint(1,34)
	j=random.randint(1,34)
	
	PRIME=packages.prime()
	
	extnd = 1000000000000000
	while k == j :
		k=random.randint(0,34)
		j=random.randint(0,34)

						# RANDOM PRIME NUMBERS
	p=PRIME[k]
	q=PRIME[j]
	
	print "p is obtained........ "#,p
	print "q is obtained........ "#,q
						# CALCULATE n VALUE
	n=p*q
	print "n is calculated...... "#,n
						# CALCULATE PHI
	PHI=(p - 1)*(q - 1)
	print "PHI is calculated.... ",#PHI
	PHI_bin=packages.a2b(PHI)
	PHI_bin = PHI_bin + extnd
						# CALCULATE e ( PUBLIC KEY )
	e=packages.slcinte(PHI)
	print "e is selected........",#e
	e_bin=packages.a2b(e)
	e_bin = e_bin + extnd
	e_chr=str(e_bin)
						# RANDOM INDEX FOR e IN FILE
	indx=random.randint(3,23)
	indx_bin=packages.a2b(indx)
	indx_bin = indx_bin + extnd
	INDX_chr=str(indx_bin)
	fim.write(INDX_chr)
						# INPUT PHI TO FILE
	PHI_chr=str(PHI_bin)
	#print "PHI : ",PHI_chr
	fim.write(PHI_chr)
	
						# WRITE INDEXED e AND ALL ELSE
						# RANDOM VALUES
	for l in range(3,24) :
		if l == indx :
			fim.write(e_chr)
		else :
			o = random.randint(0,255)
			o = packages.a2b(o)
			o = o + extnd
			o_chr=str(o)
			fim.write(o_chr)
	
						# WRITE n TO FILE
	n_bin =packages.a2b(n)
	n_bin = n_bin + extnd
	n_chr = str(n_bin)
	fim.write(n_chr)
	
	
						# LOOP TILL EOF OF INPUT FILE
						# AND ENCRYPT
	input=fip.read()
	for i in input:
		M=ord(i)
		C=packages.de_en(M,e,n)
		a_bin=packages.a2b(C)
		a_bin = a_bin + extnd
		a_chr=str(a_bin)
		fim.write(a_chr)
	
	
	
	
	fim.write('1111111111111111')
	fim.close()
	print "\n	ENCRYPTION COMPLETED/n"
	
	
	
						# WINDOW OPPERATIONS

	e1.delete(0, END)
	e1.insert(0, "encryption n = %d"%n)
def dec():
				
	n_USER=e2.get()				# GET THE USER INPUT n VALUE
	if n_USER == "Enter n":
		return
	if n_USER == "   Decryption cancelled   " :
		v.set("Enter n")
		return
	n_USR=int(n_USER)
	if n_USR == 0:
		e2.delete(0,END)
		v.set("Enter n")
		return

					# RETURNS THE PATH OF THE INTERMEDIATE						# TO READ THE ENCRYPTED DATA
	path_im=askopenfilename(title="Path of the Encrypted File",filetypes=[("Encrypted file","*.smt"),("pythonfiles","*.py")])
			#this function is imported from tkFileDialog
			#you can specify the filetypes of your choice 
			#there is an other function called LoadFileDialog which also
					# CHECKS IF DECRYPTION IS CANCELLED		
	if path_im == () :
		v.set("   Decryption cancelled   ")
		return
	fim=open(path_im,'r')
	fim.seek(0)
					
					# check the file if decryption possible
					# genuine intermediary file
					# encrypted using the same program
	binLen = 16
	sf_cd = 1010101010101010


	bin=int(fim.read(binLen))
	if bin != sf_cd :
		print "\nnot safe\n"
		return
	else :
		print "\nsafe\n"
	
						# RETREIVE INDEX TO FIND e
						# ( PUBLIC KEY )
	bin=int(fim.read(binLen))
	index = packages.b2a(bin)
	print "index is retrieved....."#,index
	
						# RETREIVE PHI VALUE
	bin=int(fim.read(binLen))
	PHI = packages.b2a(bin)
	print "PHI is retrieved......"#,PHI
	
						# RETREIVE e ( PUBLIC KEY )
	fim.seek(binLen*index)
	bin=int(fim.read(binLen))
	e = packages.b2a(bin)
	print "e is retrieved........"#,e
						# CALCULATE d ( PRIVATE KEY )
	d=packages.get_d(e,PHI)
	
	print "d is evaluated........"#,d
	
	
	fim.seek(binLen*24)			# REACH THE  n VALUE
	bin=int(fim.read(binLen))
	n = packages.b2a(bin)

					# RECEIVE THE USER INPUT n VALUE
		
					# CHECKS AUTHORISATION
	if n_USR == n :
					# RETURNS THE PATH OF THE OUTPUT FILE
					# TO GET THE DECRYPTED ACTUAL OUTPUT
		path_op=asksaveasfilename(title="Path of the decrypted File",filetypes=[("Text","*"),("Images","*.jpg"),("BitMapImage","*.bmp"),("pythonfiles","*.py")])
		
			#this function is imported from tkFileDialog
			#you can specify the filetypes of your choice 
			#there is an other function called LoadFileDialog which also 
			
		fop=open(path_op,'a')
		fop.seek(0)
		
		
		eoff = 1111111111111111
		
		
	
		bin=int(fim.read(binLen))
		while bin != eoff :
			C = packages.b2a(bin)
			M = packages.de_en(C,d,n)
			txt=chr(M)
			fop.write(txt)
			bin = int(fim.read(binLen))
	
		fim.close()
		fop.close()
		e2.delete(0,END)
		v.set("0")
	else :
		print "Wrong n entered"
		e2.delete(0,END)
		v.set("0")
    # path_op=raw_input("ENTER THE PATH OF THE DECRYPTED FILE :\n")
    # root=Tk()
    path_op = askopenfilename(filetypes=[("Path were the file should be Decrypted", "*"), ("pythonfiles", "*.py")])
    # print "file found..............."

    fop = open(path_op, "a")
    fop.seek(0)

    eoff = 1111111111111111

    bin = int(fim.read(binLen))
    while bin != eoff:
        # 	print "bin : ",bin
        C = packages.b2a(bin)
        # 	print "C : ",C
        M = packages.de_en(C, d, n)
        # 	print "M : ",M
        txt = chr(M)
        # 	print "txt :",txt
        fop.write(txt)
        bin = int(fim.read(binLen))

    fim.close()
    fop.close()

    print "                     DECRYPTION COMPLETED............"
else:
    print "Wrong n entered"
# $Id: filebrowser2.py,v 1.2 2004/03/18 05:52:22 mandava Exp $
# this is a program that uses askopenfilename function to open files.
# this function is imported from tkFileDialog