Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: String encryption decryption
|
Posted: Apr 20, 2002 5:50 PM
|
|
/* DESEncryption.java * @author: Charles Bell * @version: Feb 28, 2001 */
import java.io.*; import java.security.*; import java.security.spec.*; import javax.crypto.*; import javax.crypto.interfaces.*; import javax.crypto.spec.*; import com.sun.crypto.provider.SunJCE;
public class DESEncryption{
String message = "George W. Bush is the president of the United States of America."; Cipher desCipher; SecretKey desKey;
public static void main(String[] args){
DESEncryption desencryption = new DESEncryption(); // Add SunJCE to the list of providers SunJCE jce = new SunJCE(); Security.addProvider(jce);
desencryption.init(); desencryption.test1(); desencryption.test2(); desencryption.test3(); desencryption.test4(); }
public void init(){ try{ System.out.println("Initializing Cipher and keys. Standby..."); KeyGenerator keygenerator = KeyGenerator.getInstance("DES"); desKey = keygenerator.generateKey(); desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); System.out.println("Cipher and keys initialized."); }catch(NoSuchAlgorithmException nsae){ System.out.println("NoSuchAlgorithmException:" + nsae.getMessage()); }catch(NoSuchPaddingException nspe){ System.out.println("NoSuchPaddingException:" + nspe.getMessage()); } }
public void test1(){ try{ System.out.println("test 1"); System.out.println(message); desCipher.init(Cipher.ENCRYPT_MODE, desKey); byte[] encryptedbytes = desCipher.doFinal(message.getBytes()); desCipher.init(Cipher.DECRYPT_MODE, desKey); byte[] plainbytes = desCipher.doFinal(encryptedbytes); System.out.println(new String(encryptedbytes)); System.out.println(new String(plainbytes)); }catch(InvalidKeyException ike){ System.out.println("InvalidKeyException:" + ike.getMessage()); }catch(IllegalBlockSizeException ibse){ System.out.println("IllegalBlockSizeException:" + ibse.getMessage()); }catch(BadPaddingException bpe){ System.out.println("BadPaddingException: 222" + bpe.getMessage()); } }
public void test2(){ System.out.println("test 2"); System.out.println(new String(decrypt(encrypt(message.getBytes())))); }
public void test3(){ System.out.println("test 3"); byte[] b1 = message.getBytes(); System.out.println(new String(b1)); byte[] b2 = encrypt(b1); System.out.println(new String(b2)); byte[] b3 = decrypt(b2); System.out.println(new String(b3)); }
/** Encrypts an arbitrary file, decrypts to a new file same * which has the same bytes.*/ public void test4(){ try{ File file = new File("Saturn.gif"); //change your file here long filelength = file.length(); FileInputStream fis = new FileInputStream(file); DataInputStream dis = new DataInputStream(fis); byte[] inputbuffer = new byte[(int)filelength]; for (int i = 0;i < (int)filelength;i++){ inputbuffer[i] = dis.readByte(); } byte[] b1 = inputbuffer; byte[] b2 = encrypt(b1); FileOutputStream fos1 = new FileOutputStream("Saturn.enc"); DataOutputStream dos1 = new DataOutputStream(fos1); for (int i = 0;i < b2.length;i++){ dos1.write(b2[i]); } fos1.close(); byte[] b3 = decrypt(b2); FileOutputStream fos2 = new FileOutputStream("SaturnNew.gif"); DataOutputStream dos2 = new DataOutputStream(fos2);
for (int i = 0;i < b3.length;i++){ dos2.write(b3[i]); } fos2.close(); }catch(IOException ioe){ System.out.println("IOException: " + ioe.getMessage()); } }
public byte[] encrypt(byte[] b){ byte[] encryptedbytes = b; try{ desCipher.init(Cipher.ENCRYPT_MODE, desKey); encryptedbytes = desCipher.doFinal(b); }catch(InvalidKeyException ike){ System.out.println("InvalidKeyException:" + ike.getMessage()); }catch(IllegalBlockSizeException ibse){ System.out.println("IllegalBlockSizeException:" + ibse.getMessage()); }catch(BadPaddingException bpe){ System.out.println("BadPaddingException: 222" + bpe.getMessage()); } return encryptedbytes; }
public byte[] decrypt(byte[] b){ byte[] decryptedbytes = b; try{ desCipher.init(Cipher.DECRYPT_MODE, desKey); decryptedbytes = desCipher.doFinal(b); }catch(InvalidKeyException ike){ System.out.println("InvalidKeyException:" + ike.getMessage()); }catch(IllegalBlockSizeException ibse){ System.out.println("IllegalBlockSizeException:" + ibse.getMessage()); }catch(BadPaddingException bpe){ System.out.println("BadPaddingException: 222" + bpe.getMessage()); } return decryptedbytes; } }
|
|