Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
encryption and decryption
09-10-2009, 06:12 AM
Post: #1
encryption and decryption
hey guys,
i am working on a project regarding encryption and decryption. Can anybody tell me how can i encrypt and decrypt a given text.....
Find all posts by this user
Quote this message in a reply
09-10-2009, 08:17 PM
Post: #2
RE: encryption and decryption
Try this code for a simple encryption
Code:
public static String encrypt(String str) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
        KeySpec keySpec = new DESedeKeySpec(key.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        javax.crypto.SecretKey secretKey = keyFactory.generateSecret(keySpec);
        
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte ciphertext[] = cipher.doFinal(str.getBytes("UTF8"));
        BASE64Encoder base64encoder = new BASE64Encoder();
        
        return base64encoder.encode(ciphertext);
    }

    public static String decrypt(String encryptedString) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, IOException {
    
        KeySpec keySpec = new DESedeKeySpec(key.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        javax.crypto.SecretKey secretKey = keyFactory.generateSecret(keySpec);
        
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        
        BASE64Decoder base64decoder = new BASE64Decoder();
        byte ciphertext[] = cipher.doFinal(base64decoder.decodeBuffer(encryptedString));
        return new String(ciphertext);
        
    
    }



Java encryption tutorial is available at

http://java.sun.com/j2se/1.4.2/docs/guid...Guide.html
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: