Anda di halaman 1dari 8

'''Digital image steganography based on the Python Imaging Library (PIL) Any message can be hidden, provided the

image is large enough. The message is packed into the least significant bits of the pixel colour bands. A 4 byte header (packed in the same way) carries the message payload length. ''' import Image import itertools as its def n_at_a_time(items, n, fillvalue): '''Returns an iterator which groups n items at a time. Any final partial tuple will be padded with the fillvalue >>> list(n_at_a_time([1, 2, 3, 4, 5], 2, 'X')) [(1, 2), (3, 4), (5, 'X')] ''' it = iter(items) return its.izip_longest(*[it] * n, fillvalue=fillvalue) def biterator(data): '''Returns a biterator over the input data. >>> list(biterator(chr(0b10110101))) [1, 0, 1, 1, 0, 1, 0, 1] ''' return ((ord(ch) >> shift) & 1 for ch, shift in its.product(data, range(7, -1, -1))) def header(n): '''Return n packed in a 4 byte string.''' bytes = (chr(n >> s & 0xff) for s in range(24, -8, -8)) return ('%s' * 4) % tuple(bytes) def setlsb(cpt, bit): '''Set least significant bit of a colour component.''' return cpt & ~1 | bit def hide_bits(pixel, bits): '''Hide a bit in each pixel component, returning the resulting pixel.''' return tuple(its.starmap(setlsb, zip(pixel, bits))) def hide_bit(pixel, bit): '''Similar to the above, but for single band images.''' return setlsb(pixel, bit[0]) def unpack_lsbits_from_image(image): '''Unpack least significant bits from image pixels.''' # Return depends on number of colour bands. See also hide_bit(s) if len(image.getbands()) == 1: return (px & 1 for px in image.getdata()) else: return (cc & 1 for px in image.getdata() for cc in px)

def call(f): # (Used to defer evaluation of f) return f() def disguise(image, data): '''Disguise data by packing it into an image. On success, the image is modified and returned to the caller. On failure, None is returned and the image is unchanged. ''' payload = '%s%s' % (header(len(data)), data) npixels = image.size[0] * image.size[1] nbands = len(image.getbands()) print len(payload) * 8 , npixels * nbands if len(payload) * 8 <= npixels * nbands: new_pixel = hide_bit if nbands == 1 else hide_bits pixels = image.getdata() bits = n_at_a_time(biterator(payload), nbands, 0) new_pixels = its.starmap(new_pixel, its.izip(pixels, bits)) image.putdata(list(new_pixels)) return image def reveal(image): '''Returns any message disguised in the supplied image file, or None.''' bits = unpack_lsbits_from_image(image) def accum_bits(n): return reduce(lambda a, b: a << 1 | b, its.islice(bits, n), 0) def next_ch(): return chr(accum_bits(8)) npixels = image.size[0] * image.size[1] nbands = len(image.getbands()) data_length = accum_bits(32) if npixels * nbands > 32 + data_length * 8: return ''.join(its.imap(call, its.repeat(next_ch, data_length))) if __name__ == "__main__": import urllib droste = urllib.urlopen("http://is.gd/cHqT").read() open("droste.png", "wb").write(droste) droste = Image.open("droste.png") while droste: droste.show() droste = reveal(droste) if droste: open("droste.png", "wb").write(droste) droste = Image.open("droste.png")

RSA python algo

def encrypt(key, msg): encryped = [] for i, c in enumerate(msg): key_c = ord(key[i % len(key)]) msg_c = ord(c) encryped.append(chr((msg_c + key_c) % 127)) return ''.join(encryped) def decrypt(key, encryped): msg = [] for i, c in enumerate(encryped): key_c = ord(key[i % len(key)]) enc_c = ord(c) msg.append(chr((enc_c - key_c) % 127)) return ''.join(msg) if __name__ == '__main__': key = 'a' msg = 'Hello world' encrypted = encrypt(key, msg) decrypted = decrypt(key, encrypted) print 'Message:', repr(msg) print 'Key:', repr(key) print 'Encrypted:', repr(encrypted) print 'Decrypted:', repr(decrypted)

SJBIT
#include<stdio.h> #include<stdlib.h> #include<string.h> long int e,d,n; long int val[50]; char decode(long int ch) { int i; long int temp=ch; for(i=1;i<d;i++) ch=temp*ch%n; return(ch); } int gcd(long a,long b) { if(b==0) return a; else return(gcd(a,a%b)); } int prime(int a) { int i; for(i=2;i<a;i++) { if((a%i)==0) return 0; } return 1; } int encode(char ch) { int i; long int temp=ch; for(i=1;i<e;i++) temp=temp*ch%n; return(temp); } int main()

{ int i; long int p,q,phi,c[50]; char text[50],ctext[50]; printf("Enter the text to be encoded \n"); gets(text); printf("%s \n",text); do { p=rand()%30; }while(!prime(p)); do { q=rand()%30; }while(!prime(q)); n=p*q; phi=(p-1)*(q-1); printf("\n p=%d\t q=%d n=%d phi=%d \n",p,q,n,phi); do { e=rand()%phi; }while(!gcd(e,phi)); do { d=rand()%phi; } while((d*e%phi)!=1); printf("\n**** encoding message*** \n"); sleep(2); for(i=0;text[i]!='\0';i++) val[i]=encode(text[i]); val[i]=-999; printf("\n**** encoded message *** \n"); for(i=0;val[i]!=-999;i++) printf("%ld",val[i]); printf("\n decoded message"); sleep(2); for(i=0;val[i]!=-999;i++) ctext[i]=decode(val[i]); ctext[i]='\0'; printf("\n decoded message :%s\n\n\n",ctext); }

Program 7
Write a program for simple RSA algorithm to encrypt and decrypt the data key generation algorithm RSA
#include<stdio.h> #include<math.h> double min(double x, double y) { return(x<y?x:y); } double max(double x,double y) { return(x>y?x:y); } double gcd(double x,double y) { if(x==y) return(x); else return(gcd(min(x,y),max(x,y)-min(x,y))); } long double modexp(long double a,long double x,long double n) { long double r=1; while(x>0) { if ((int)(fmodl(x,2))==1) { r=fmodl((r*a),n); } a=fmodl((a*a),n); x/=2; } return(r); }

int main() { long double p,q,phi,n,e,d,cp,cq,dp,dq,mp,mq,sp,sq,rp,rq,qInv,h; long double ms,es,ds; do{ printf("\n Enter prime numbers p and q:"); scanf(" %Lf %Lf",&p,&q); } while(p==q); n=p*q; phi=(p-1)*(q-1); do{ printf("\n Enter prime value of e:"); scanf(" %Lf",&e); } while((gcd(e,phi)!=1)&&e>phi); /*for e being relatively prime to phi */ for(d=1;d<phi;++d) { if(fmod((e*d),phi)==1)

break; } printf("\n D within main = %Lf",d); /* public key is {n,e} private key is d */ printf("\n Enter the message:"); scanf(" %Lf",&ms); es=modexp(ms,e,n); ds=modexp(es,d,n); printf("\n Original Message : %Lf",ms); printf("\n Encrypted Message : %Lf",es); printf("\n Decrypted Message : %Lf\n",ds); return(0); }

OUTPUT:
Enter prime numbers p and q: 223 101 Enter prime value of e: 2213 D within main = 18077.000000 Enter the message:123 Original Message : 123.000000 Encrypted Message : 18415.000000 Decrypted Message : 123.000000

Anda mungkin juga menyukai