使用XTEA的Java加密

时间:2009-10-15 22:07:59

标签: java encryption

如何使用Java中的XTEA方案加密字符串。

由于

    public class run {
        public static void 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




        }
    }
|

如果填充正确,则可以正常工作。 谢谢你们

3 个答案:

答案 0 :(得分:2)

在Google上搜索后,我发现您可以使用BlockCipher界面手动实施XTEA方案。

H2 Database使用此界面实现了一个版本,您可以在此处找到:XTEA.JAVA on code.google.com

这里的问题是您需要修改加密/解密(byte [],byte [],int)方法以满足您的需求。

答案 1 :(得分:1)

首先,您需要此方法将String转换为字节数组:

 public static byte[] convertStringToByteArray(String stringToConvert) {
    byte[] theByteArray = stringToConvert.getBytes();

    return theByteArray;
}

然后,使用db4o项目中的this code,并调用其方法,如:

byte[] theBytes = convertStringToByteArray("the string");
encrypt(theBytes); //theBytes now contains the encrypted data

用于加密,

decrypt(theBytes); //theBytes now contains the decrypted data
String str = new String(theBytes); //decrypted String

答案 2 :(得分:0)

此:

http://www.google.com/search?q=java+encryption+XTEA给了我这个

http://en.wikipedia.org/wiki/XTEA最终给了我这个:

http://code.google.com/p/h2database/source/browse/trunk/h2/src/main/org/h2/security/XTEA.java

我认为你想要的是什么

修改

我不知道你是否已经读过这篇文章,但现在就去了。

Java security API提供加密所需的architecture

使用Cipher.getInstace()方法获得Cypher:

Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");

但默认情况下,java不提供XTEA

的密码

但是,您可以注册your own

我从来没有必要遵循完整的流程,我想你必须完成许多接口并注册你的实现。

如果你需要走那么远,你可以使用前面提到的实现。它是H2 DB使用的。

我希望这会有所帮助。