can anyone give me two programs for encryption and decryption using c or java!!!!!!!!!
can anyone give me two programs for encryption and decryption using c or java!!!!!!!!!
R.I.P. Ronnie James DIO
This is for encryption and decryption of files using C programming-
Code:encryption and decryption of files In main function we declare required variable. printf(" enter choice 1)encode 2)view ecoded file 3) decode 4)view decoced file 5)exit ------"); // this is to print the main menu scanf("%d",&ch); // entering of choice while(ch!=5) // until choice is exit { if(ch==1) //if choice is to encrypt { scanf("%s",name); // accept file name scanf("%c",&ff); //accept the choice of change // the file position or not if(ff=='c'||ff=='C') {strcpy(encri,ruf);scanf("%s",encri);} // so as to change enter file path cscanf("%s",pass); // enter password l=strlen(pass); // l =string length of password st=fopen(name,"r"); // open the file to be encrypted ed=fopen(encri,"w"); // open the file to where to be // encrypted while(!feof(st)) // until end of file to be encrypted { x=fgetc(st); // read each character into X fputc(x+pass[i++%l],ed); // write the sum of X and password } // char fclose(st); // close first file fclose(ed); // close second file } if(ch==3) // if choice is decryption { scanf(“%sâ€,encri); //enter the file name to be decrypted ed=fopen(encri,"r"); //open the above mentioned file scanf("%c",&ff); //accept the choice of change // the file position or not if(ff=='c'||ff=='C') { strcpy(decri,ruf); scanf("%s",decri); // so as to change enter file path } cscanf("%s",pass); // enter password de=fopen(decri,"w"); //open a file to store decoded data while(!feof(ed)) // until end of file to be encrypted { x=fgetc(ed); // read each character into X fputc(x-pass[i++%l],de); // write the sum of X and password } // char fclose(ed); // close first file fclose(de); // close second file } if(ch==2) //if choice is to view the contents encoded file { ed=fopen(encri,"r"); //open that file and read each character while(!feof(ed)) printf("%c",fgetc(ed)); //print them until end of file fclose(ed); // close this file } if(ch==4) //if choice is to view the contents decoded file { ed=fopen(decri,"r"); //open that file and read each character while(!feof(ed)) printf("%c",fgetc(ed)) ; //print them until end of file fclose(ed) ; // close this file } scanf("%d",&ch); // enter the choice from menu } // end while } // end main
Heres the 3des encryption and decryption using Java:
Code:import java.security.MessageDigest;import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec; public class TripleDESTest { public static void main(String[] args) throws Exception { String text = "kyle boon"; byte[] codedtext = new TripleDESTest().encrypt(text); String decodedtext = new TripleDESTest().decrypt(codedtext); System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array System.out.println(decodedtext); // This correctly shows "kyle boon" } public byte[] encrypt(String message) throws Exception { final byte[] digestOfPassword = md.digest("HG58YZ3CR9" .getBytes("utf-8")); final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); for (int j = 0, k = 16; j < 8;) { keyBytes[k++] = keyBytes[j++]; } final SecretKey key = new SecretKeySpec(keyBytes, "DESede"); final IvParameterSpec iv = new IvParameterSpec(new byte[8]); final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); final byte[] plainTextBytes = message.getBytes("utf-8"); final byte[] cipherText = cipher.doFinal(plainTextBytes); // final String encodedCipherText = new sun.misc.BASE64Encoder() // .encode(cipherText); return cipherText; } public String decrypt(byte[] message) throws Exception { final MessageDigest md = MessageDigest.getInstance("md5"); final byte[] digestOfPassword = md.digest("HG58YZ3CR9" .getBytes("utf-8")); final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); for (int j = 0, k = 16; j < 8;) { keyBytes[k++] = keyBytes[j++]; } final SecretKey key = new SecretKeySpec(keyBytes, "DESede"); final IvParameterSpec iv = new IvParameterSpec(new byte[8]); final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); decipher.init(Cipher.DECRYPT_MODE, key, iv); // final byte[] encData = new // sun.misc.BASE64Decoder().decodeBuffer(message); final byte[] plainText = decipher.doFinal(message); return new String(plainText, "UTF-8"); } }
javax.crypto is not there.........
R.I.P. Ronnie James DIO
the java program has errors how can i fix it....
errors are-----------------------------------------
C:\Sun\AppServer\jdk\bin>javac TripleDESTest.java
TripleDESTest.java:15: cannot resolve symbol
symbol : variable md
location: class TripleDESTest
final byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("u
tf-8"));
^
TripleDESTest.java:16: cannot resolve symbol
symbol : method copyOf (byte[],int)
location: class java.util.Arrays
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
^
TripleDESTest.java cannot resolve symbol
symbol : method copyOf (byte[],int)
location: class java.util.Arrays
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
^
3 errors
C:\Sun\AppServer\jdk\bin>
Could any1 pls help? Am new here
Bookmarks