The Artima Developer Community
Sponsored Link

Java Answers Forum
String encryption decryption

3 replies on 1 page. Most recent reply: Apr 20, 2002 5:50 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
patrick

Posts: 23
Nickname: patrick
Registered: Mar, 2002

String encryption decryption Posted: Apr 19, 2002 10:24 AM
Reply to this message Reply
Advertisement
hi,
i have a string that i want to encrypt than decrypt how can i do that


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: String encryption decryption Posted: Apr 19, 2002 10:45 AM
Reply to this message Reply
I have developed a Java tool implementing RSA algorithms for encryption and decryption. Please refer to this url.
http://www.artima.com/forums/flat.jsp?forum=1&thread=844

If you want to see application running and also want to see the source code, please donwload rsa.ZIP from
http://www.homefusion.net/~ksharan/rsa/rsa.htm.

After downloading rsa.zip, unzip it in a folder and then run runrsa.bat file from same folder.
Thanks
Kishori

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: String encryption decryption Posted: Apr 20, 2002 1:49 PM
Reply to this message Reply
IBM is selling an home bread product that allows you to encrypt XML's
You should give a deep look into http://www.alphaworks.ibm.com
Beware that the tool I'm talking about is under (non GPL) licence !

Thomas,

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: String encryption decryption Posted: Apr 20, 2002 5:50 PM
Reply to this message Reply
 
/* 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;
}

}

Flat View: This topic has 3 replies on 1 page
Topic: forwarding page in jsp Previous Topic   Next Topic Topic: JTextPane in Overwrite mode.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use