+ Reply to Thread
Results 1 to 2 of 2

Thread: Encrypt and Decrypt Text

  1. #1
    Junior Member
    Join Date
    Jan 2012

    Encrypt and Decrypt Text

    I`d like to know how can i encrypt and decrypt strings?
    Using AES [SHA1]

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Here you are :
    public static byte[] encrypt() throws Exception {
            String HASH = "SuperSecretPassword";
            String ourtext = "Hello world , lets play around!";
            //API for generating symmetric cryptographic keys
            KeyGenerator KG = KeyGenerator.getInstance("AES");
            //SHA1 algorithm.
            SecureRandom SR = SecureRandom.getInstance("SHA1PRNG");
            //Reseeds this SecureRandom instance with the specified HASH.
            //Initializes this KeyGenerator instance for the specified 
            //key size (in bits) using the specified randomness source.
            KG.init(128, SR);
            //Generates a secret key.
            SecretKey seckey = KG.generateKey();
            //Returns the encoded form of the key.
            byte[] rawKey = seckey.getEncoded();
            //Create a new SecretKeySpec for the key data and AES algorithm.
            SecretKeySpec SKS = new SecretKeySpec(rawKey, "AES");
            //Creates a new Cipher for the specified transformation. 
            Cipher cipher = Cipher.getInstance("AES");
    	//1- Encrypt
            cipher.init(Cipher.ENCRYPT_MODE, SKS);
    	//2- Decrypt
    	cipher.init(Cipher.DECRYPT_MODE, SKS);
    	// Use only one either encrypt or decrypt , you can`t use both....
            byte[] encrypted = cipher.doFinal(ourtext.getBytes());
            return encrypted;

+ Reply to Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts