Winanjaya
Jan 16th, 2010, 09:33 AM
Hello All,
Could any body please share how to string encrypt / decryption?
thanks a lot in advance
Regards
ComputerJy
Jan 16th, 2010, 11:17 AM
Here is a sample. If this is your first time you need to do a whole lot of readingimport 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);
}
}
Winanjaya
Jan 17th, 2010, 06:43 AM
Hi .. but it's become more complex if I separated encrypt & decrypt into 2 method.. any idea?
ComputerJy
Jan 17th, 2010, 06:59 AM
Ok, here is another sample using the XTea algorithm: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;
}
}