Results 1 to 5 of 5

Thread: please share how to string encrypt / decryption?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Question please share how to string encrypt / decryption?

    Hello All,

    Could any body please share how to string encrypt / decryption?

    thanks a lot in advance

    Regards

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: please share how to string encrypt / decryption?

    Here is a sample. If this is your first time you need to do a whole lot of reading
    Code:
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    public class Test {
    
    	public static void main(final String[] args) throws Exception {
    		final byte[] input = "Hello World!".getBytes();
    		final byte[] keyBytes = new byte[]{0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd,
    			(byte) 0xef, 0x14, 0x9, 0x12, 0x34, 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd,
    			(byte) 0xef, 0x14, 0x9, 0x12, 0x34};
    
    		final SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede");
    		final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    		System.out.println("input : " + new String(input));
    
    		// encrypt message
    		cipher.init(Cipher.ENCRYPT_MODE, key);
    		final byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    		int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    		ctLength += cipher.doFinal(cipherText, ctLength);
    		System.out.println("cipher: " + new String(cipherText) + " bytes: " + ctLength);
    
    		// decrypt message
    		cipher.init(Cipher.DECRYPT_MODE, key);
    		final byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
    		int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
    		ptLength += cipher.doFinal(plainText, ptLength);
    		System.out.println("plain : " + new String(plainText) + " bytes: " + ptLength);
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Re: please share how to string encrypt / decryption?

    Hi .. but it's become more complex if I separated encrypt & decrypt into 2 method.. any idea?

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: please share how to string encrypt / decryption?

    Ok, here is another sample using the XTea algorithm:
    Code:
    public class Test
    {
    	public static void main(final String[] args) throws Exception
    	{
    		final String str = "Hello World!";
    		final String key = "password";
    		final String encryptedString = encrypt(str, key);
    		System.out.println(encryptedString);
    		final String decryptedString = decrypt(encryptedString, key);
    		System.out.println(decryptedString);
    	}
    
    	public static String encrypt(String data, final String key) throws Exception
    	{
    		if (data.length() == 0)
    			throw new Exception("Data must be at least 1 character in length.");
    
    		final long[] formattedKey = formatKey(key);
    
    		if (data.getBytes().length % 2 != 0)
    			data += '\0';
    
    		final byte[] dataBytes = data.getBytes();
    		String cipher = "";
    		final int[] tempData = new int[2];
    
    		for (int i = 0; i < dataBytes.length; i += 2)
    		{
    			tempData[0] = dataBytes[i];
    			tempData[1] = dataBytes[i + 1];
    			code(tempData, formattedKey);
    			cipher += convertUIntToString(tempData[0]) + convertUIntToString(tempData[1]);
    		}
    
    		return cipher;
    	}
    
    	public static String decrypt(final String data, final String key) throws Exception
    	{
    		final long[] formattedKey = formatKey(key);
    		int x = 0;
    		final int[] tempData = new int[2];
    		final byte[] dataBytes = new byte[data.length() / 8 * 2];
    
    		for (int i = 0; i < data.length(); i += 8)
    		{
    			tempData[0] = convertStringToUInt(data.substring(i, i + 4));
    			tempData[1] = convertStringToUInt(data.substring(i + 4, i + 8));
    			decode(tempData, formattedKey);
    			dataBytes[x++] = (byte) tempData[0];
    			dataBytes[x++] = (byte) tempData[1];
    		}
    
    		String decipheredString = new String(dataBytes, 0, dataBytes.length);
    
    		if (decipheredString.charAt(decipheredString.length() - 1) == '\0')
    			decipheredString = decipheredString.substring(0, decipheredString.length() - 1);
    
    		return decipheredString;
    	}
    
    	private static void code(final int[] v, final long[] k)
    	{
    		int y = v[0];
    		int z = v[1];
    		int sum = 0;
    		final int delta = 0x9E3779B9;
    		int n = 32;
    
    		while (n-- > 0)
    		{
    			y += ((z << 4 ^ z >>> 5) + z) ^ (sum + k[sum & 3]);
    			sum += delta;
    			z += ((y << 4 ^ y >>> 5) + y) ^ (sum + k[sum >> 11 & 3]);
    		}
    
    		v[0] = y;
    		v[1] = z;
    	}
    
    	private static void decode(final int[] tempData, final long[] k)
    	{
    		int y = tempData[0];
    		int z = tempData[1];
    		int sum = 0xC6EF3720;
    		final int delta = 0x9E3779B9;
    		int n = 32;
    
    		while (n-- > 0)
    		{
    			z -= ((y << 4 ^ y >>> 5) + y) ^ (sum + k[sum >> 11 & 3]);
    			sum -= delta;
    			y -= ((z << 4 ^ z >>> 5) + z) ^ (sum + k[sum & 3]);
    		}
    
    		tempData[0] = y;
    		tempData[1] = z;
    	}
    
    	private static String convertUIntToString(final int Input)
    	{
    		final StringBuilder output = new StringBuilder();
    		output.append((char) ((Input & 0xFF)));
    		output.append((char) ((Input >> 8) & 0xFF));
    		output.append((char) ((Input >> 16) & 0xFF));
    		output.append((char) ((Input >> 24) & 0xFF));
    		return output.toString();
    	}
    
    	private static int convertStringToUInt(final String Input)
    	{
    		int output;
    		output = Input.charAt(0);
    		output += (Input.charAt(1) << 8);
    		output += (Input.charAt(2) << 16);
    		output += (Input.charAt(3) << 24);
    		return output;
    	}
    
    	private static long[] formatKey(String Key) throws Exception
    	{
    		if (Key.length() == 0)
    			throw new Exception("Key must be between 1 and 16 characters in length");
    		while (Key.length() < 16)
    			Key += "a";
    
    		Key = Key.substring(0, 16);
    
    		final long[] formattedKey = new long[4];
    
    		// Get the key into the correct format for TEA usage.
    		int j = 0;
    
    		for (int i = 0; i < Key.length(); i += 4)
    			formattedKey[j++] = convertStringToUInt(Key.substring(i, i + 4));
    
    		return formattedKey;
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Re: please share how to string encrypt / decryption?

    Thanks a lot ..

Posting Permissions

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



Click Here to Expand Forum to Full Width