here is the code that i designed my self,i think it would be useful for encrypt important data or set a password for entering.
here is the class:
Using java Syntax Highlighting
- public class DES {
- private static Cipher pbeCipher;
- private static SecretKey pbeKey;
- public KeyGenerator kgen ;
- private static PBEKeySpec pbeKeySpec;
- private static PBEParameterSpec pbeParamSpec;
- private static SecretKeyFactory keyFac;
- byte[] ciphertext={} ;
- byte[] salt = {
- (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
- (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
- };
- int count=20;
- public DES(){
- try{
- pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
- }
- catch(NoSuchAlgorithmException e){
- Log.e("Ya Reza", e.toString());}
- catch(NoSuchPaddingException e){
- Log.e("Ya Reza", e.toString());}
- }
- public void setpass(String Key){
- pbeKeySpec = new PBEKeySpec(Key.toCharArray());
- try{
- keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");}
- catch(NoSuchAlgorithmException e){
- Log.e("Ya Reza", e.toString());}
- try{
- pbeKey = keyFac.generateSecret(pbeKeySpec);}
- catch(InvalidKeySpecException e){
- Log.e("Ya Reza", e.toString());
- }
- }
- public String encrypt(String data)throws Exception{
- try{
- pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);}
- catch(InvalidKeyException e){
- Log.e("Ya Reza", e.toString());
- }
- catch(InvalidAlgorithmParameterException e){
- Log.e("Ya Reza", e.toString());
- }
- try{
- ciphertext= pbeCipher.doFinal(data.getBytes());}
- catch(IllegalBlockSizeException e){
- Log.e("Ya Reza", e.toString());
- }
- catch(BadPaddingException e){
- Log.e("Ya Reza", e.toString());
- }
- return new String(ciphertext);
- public String decrypt(String data)throws Exception{
- try{
- pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);}
- catch(InvalidKeyException e){
- Log.e("Ya Reza", e.toString());
- }
- catch(InvalidAlgorithmParameterException e){
- Log.e("Ya Reza", e.toString());
- }
- try{
- ciphertext= pbeCipher.doFinal(data.getBytes());}
- catch(IllegalBlockSizeException e){
- Log.e("Ya Reza", e.toString());
- }
- catch(BadPaddingException e){
- Log.e("Ya Reza", e.toString());
- }
- return new String(ciphertext);
- }
- }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
in main class you have to call the constructor and after it call setpass() function to send your Key.for encrypt and decrypot you just need to send your data,of course you have to know your data and your key must be String type.hope it would be benefit for you
Yours Sincerely
Armin



