How to encrypt a string
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sabu_vs
Posted On:   Monday, October 15, 2001 01:54 AM

Hi



I have a string with my name "sabu"


I want to encrypt this string with my initial "vs".


Then using the the same key ,ie "vs" I want to decrypt the string.


could you pls help me to solve the problem.


could you pls include a sample code.






thanks


sabu

Re: How to encrypt a string

Posted By:   ashokbhargav_skunk  
Posted On:   Friday, November 30, 2001 01:23 PM



So what is the problem.
Just include a main function like this





public static void main(String[] args) {

Security.addProvider(new com.sun.crypto.provider.SunJCE());
byte [] encryptedByts = PBEncryptionClass.PBEncryption("sabu", "vs");
System.out.println("Encrypted String" + encryptedByts );
byte [] decryptedByts = PBEncryptionClass.PBDecryption(encryptedByts,"vs");
System.out.println("Encrypted String " + new String(decryptedByts) );

}




Where "PBEncryptionClass" will be the class where all the above mentioned methods and main function will be.



Just remember to convert the decrypted byte array back to a string by new String() method before printing as if u system.out.println(byte array) it will not look like the orginal string u encrypted . I hope this helps. Good luck
Ashok

Re: How to encrypt a string

Posted By:   skyyoung_shmilu  
Posted On:   Monday, October 15, 2001 02:27 AM

java.sun.com/jce example:


import java.security.*;

import java.security.spec.*;

import java.security.interfaces.*;

import javax.crypto.*;

import javax.crypto.spec.*;

import javax.crypto.interfaces.*;

import com.sun.crypto.provider.SunJCE;


public static byte[] PBEncryption(String rStr,String rPassword)

{

try

{

PBEKeySpec pbeKeySpec;

PBEParameterSpec pbeParamSpec;

SecretKeyFactory keyFac;

// Salt

byte[] salt = {

(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,

(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99

};

// Iteration count

int count = 20;

// Create PBE parameter set

pbeParamSpec = new PBEParameterSpec(salt, count);

// Prompt user for encryption password.

// Collect user password as char array (using the

// "readPasswd" method from above), and convert

// it into a SecretKey object, using a PBE key

// factory.

pbeKeySpec = new PBEKeySpec(rPassword.toCharArray());

keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");

SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);



// Create PBE Cipher

Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");



// Initialize PBE Cipher with key and parameters

pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);



// Our cleartext

byte[] cleartext = rStr.getBytes();



// Encrypt the cleartext

byte[] ciphertext = pbeCipher.doFinal(cleartext);

return ciphertext;

}catch(Exception e)

{

e.printStackTrace();

return new byte[0];

}

}



public static String PBDecryptStr(String rStr, String rPassword)

{

byte[] lDecByte = PBDecryption(rStr.getBytes(), rPassword);



return (new String(lDecByte));

}



public static byte[] PBDecryption(byte[] rByte,String rPassword)

{

try

{

PBEKeySpec pbeKeySpec;

PBEParameterSpec pbeParamSpec;

SecretKeyFactory keyFac;

// Salt

byte[] salt = {

(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,

(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99

};

// Iteration count

int count = 20;

// Create PBE parameter set

pbeParamSpec = new PBEParameterSpec(salt, count);

// Prompt user for encryption password.

// Collect user password as char array (using the

// "readPasswd" method from above), and convert

// it into a SecretKey object, using a PBE key

// factory.

pbeKeySpec = new PBEKeySpec(rPassword.toCharArray());

keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");

SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);



// Create PBE Cipher

Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");



// Initialize PBE Cipher with key and parameters

pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);



// Decrypt the cleartext

byte[] ciphertext = pbeCipher.doFinal(rByte);

return ciphertext;

}catch(Exception e)

{

e.printStackTrace();

return new byte[0];

}

}
About | Sitemap | Contact