aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes crytography Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "crytography " Watch "crytography " New topic
Author

crytography

manu manee
Greenhorn

Joined: Jul 12, 2001
Posts: 2
just tell me the procedures how to encrypt the incoming string(i.e. any variable may be String ui="fdfdfd" and the information stored in the file.
------------------
raghavender rao
Ranch Hand

Joined: Jul 20, 2001
Posts: 45
this code wil work perfectly

import java.io.*;
import com.dstc.security.provider.*;
import javax.crypto.*;
import javax.crypto.Cipher.*;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;


public class EncTest {


public static void main(String args[]){


//String text = "Hello";


EncTest t = new EncTest();

}
public EncTest(){


try {
String text = "RAO";

System.out.println();
System.out.println();
System.out.println("Generating a DESede (TripleDES) key...");
// Create a TripleDES key
System.out.println();
System.out.println();
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyStore.getDefaultType());

//KeyGenerator keyGenerator = KeyGenerator.getInstance(getDefaultKey()"DES");
//KeyGenerator keyGenerator = KeyGenerator.getInstance("PBEwithMD5andDE5-CBC");

keyGenerator.init(56); // need to initialize with the keysize

Key key = keyGenerator.generateKey();

String keyy = key.toString();

System.out.println();
System.out.println();
System.out.println("Done generating the key and the key is ... "+keyy);

//System.out.println("The Security Manager is"+System.getSecurityManager());


//Create a cipher using that key to initialize it

// Cipher cipher = Cipher.getInstance("PBEwithMD5andDE5-CBC");


Cipher cipher = Cipher.getInstance("DES");


System.out.println();
System.out.println();
System.out.println("The Given Text is "+text);


cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] plaintext = text.getBytes("UTF8");

// Print System.out the bytes of the plaintext
//System.out.println("\nPlaintext: "+plaintext);


for (int i=0;i<plaintext.length;i++) {



System.out.print(plaintext[i]+" ");



}
// Perform the actual encryption

byte[] ciphertext = cipher.doFinal(plaintext);
// Print System.out the ciphertext

System.out.print("\n\nCiphertext: ");
String s=null;
for (int i=0;i<ciphertext.length;i++) {


System.out.print(ciphertext[i]+" ");


}

System.out.println();
System.out.println();
s = ciphertext.toString();
System.out.println("The CypherText is "+s);
// Re-initialize the cipher to decrypt mode
cipher.init(Cipher.DECRYPT_MODE, key);

// Perform the decryption
byte[] decryptedText = cipher.doFinal(ciphertext);
String output = new String(decryptedText,"UTF8");

System.out.println();
System.out.println();
System.out.println("\n\nDecrypted text: "+output);
}catch(Exception e){


System.out.println(e.getMessage());

}

}


}
//Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
//Cipher cipher = Cipher.getInstance("PBEwithMD5andDE5-CBC");

for further doubts mail me bk...
Rao


Rao<BR>Sun Certified Programmer for the Java��� 2 Platform
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: crytography