可能重复:
encrypt the html files for client side and browse them in a swing application
我正在开发一个swing应用程序,其中客户端必须访问本地存储在机器中的html文件,但我希望客户端不应该直接访问html文件,所以想要使用java加密整个html文件的文件夹在Java应用程序中,我会编写硬编码来解密加密文件夹中的html文件。还有一件事可以在加密文件夹中进行更新,以便将来在客户端合并加密文件。
我被困在这里并没有解决我的问题的线索,对我的问题的任何帮助表示赞赏。
答案 0 :(得分:2)
- 我会要求您使用Cipher
,CipherInputStream
和CipherOutputStream
进行加密和解密< /强>
- 您可以循环浏览文件夹中的文件,然后对每个文件进行加密,类似地,您可以遍历文件夹中的文件进行解密。
请参阅此链接:
答案 1 :(得分:2)
阅读此链接:
http://192.9.162.55/developer/technicalArticles/Security/AES/AES_v1.html
默认情况下,您最多可以使用AES 128位。
要使用256位AES密钥,您必须从here下载并安装“Java Cryptography Extension(JCE)Unlimited Strength Jurisdiction Policy Files”。
这是一个简单的例子,它使用AES加密解密java中的String消息:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
/**
* This program generates a AES key, retrieves its raw bytes, and
* then reinstantiates a AES key from the key bytes.
* The reinstantiated key is used to initialize a AES cipher for
* encryption and decryption.
*/
public class AES {
/**
* Turns array of bytes into string
*
* @param buf Array of bytes to convert to hex string
* @return Generated hex string
*/
public static String asHex (byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
public static void main(String[] args) throws Exception {
String message="This is just an example";
// Get the KeyGenerator
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may not be available
// Generate the secret key specs.
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
// Instantiate the cipher
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted =
cipher.doFinal((args.length == 0 ?
"This is just an example" : args[0]).getBytes());
System.out.println("encrypted string: " + asHex(encrypted));
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original =
cipher.doFinal(encrypted);
String originalString = new String(original);
System.out.println("Original string: " +
originalString + " " + asHex(original));
}
}
上述代码必须以这样的方式进行修改:您将加密文件读入StringBuffer
/字节数组等,对它们进行解密(仅在内存中)执行所需的工作然后重新加密StringBuffer
/ data / bytes并将其写入文件。
另一个伟大的Cryptograpic API是:
Bouncy Castle API也有许多例子: