How would I encrypt a string using the XTEA scheme in Java.
Thanks
public class run {
public static vo开发者_JAVA技巧id main(String[] args) throws Exception{
XTEA2 x= new XTEA2("keykey");
String s = "hi there";
byte[] theBytes = s.getBytes();
System.out.println("Plaintext: " + new String(theBytes));
x.encrypt(theBytes); //theBytes now contains the encrypted data
System.out.println("Crypo Text: " + new String(theBytes));
x.decrypt(theBytes); //theBytes now contains the decrypted data
System.out.println("Decrypted: " + new String(theBytes));
String str = new String(theBytes); //decrypted String
}
}
|
Works if it is padded properly. Thanks guys
After searching on google I found out that you can manually implement a XTEA scheme with using the BlockCipher interface.
H2 Database implemented a version with this interface which you can find here: XTEA.JAVA on code.google.com
The problem here is that you'll need to modify the encrypt/decrypt(byte[], byte[], int) methods to match your needs.
First, you need this method to convert the String to a byte array:
public static byte[] convertStringToByteArray(String stringToConvert) {
byte[] theByteArray = stringToConvert.getBytes();
return theByteArray;
}
then, use this code from the db4o project, and call its methods like:
byte[] theBytes = convertStringToByteArray("the string");
encrypt(theBytes); //theBytes now contains the encrypted data
for encryption, and
decrypt(theBytes); //theBytes now contains the decrypted data
String str = new String(theBytes); //decrypted String
This:
http://www.google.com/search?q=java+encryption+XTEA gave me this
http://en.wikipedia.org/wiki/XTEA which finally gave me this:
http://code.google.com/p/h2database/source/browse/trunk/h2/src/main/org/h2/security/XTEA.java
Which I think is what you want
EDIT
I don't know if you have read this already but here it goes.
Java security API provide the architecture needed for cryptography
You get a Cypher using Cipher.getInstace() method:
like
Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
But by default java do not provide a cipher for XTEA
However you are given the means to register your own .
I've never had the need to follow the complete process, I guess you have to fulfill a number of interfaces and register your implementation.
In case you need to go that far, you can use the implementation mentioned earlier. It is the one used by H2 DB.
I hope this helps.
精彩评论