我有一个项目来对java中的字符串输入进行加密和解密。我已经被困了一个星期做一些研究。如果您有我可以在我的项目中使用的 java 中的算法 AES 和算法 Twofish 的示例源代码或函数方法,我真的很感激。我真的需要你的帮助......希望有人可以成为我的救星。非常感谢。
问问题
2704 次
1 回答
1
对于 AES,您可以使用 java 的库。
闲置的代码会给你一个开始的想法。
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class AES {
public void run() {
try {
String text = "Hello World";
String key = "1234567891234567";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println("Encrypted text: " + new String(encrypted));
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.out.println("Decrypted text: " + decrypted);
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AES app = new AES();
app.run();
}
}
于 2014-07-10T08:40:08.613 回答