我正在开发一个swing应用程序,其中客户端必须访问本地存储在机器中的html文件,但我希望客户端不应该直接访问html文件,因此希望使用java加密html文件的整个文件夹,而在Java应用程序中,我将编写硬代码从加密文件夹中解密html文件。还有一件事,更新应该是可能的,在加密的文件夹,以便加密的文件可以在未来的客户端合并。
我一直被困在这里,对我的问题没有任何线索,任何帮助我的问题都是感激的。
发布于 2012-11-21 07:23:27
- --我想请您使用Cipher、CipherInputStream和CipherOutputStream进行加密和解密。
-您可以循环遍历文件夹中的文件,然后对每个文件进行加密,类似地,您也可以遍历文件夹中的文件来解密它。
查看此链接:
http://www.flexiprovider.de/examples/ExampleCrypt.html
发布于 2012-11-21 07:25:13
阅读此链接:
http://192.9.162.55/developer/technicalArticles/Security/AES/AES_v1.html默认情况下,您最多可以使用AES 128位。
为了使用256位AES密钥,您必须从这里下载并安装“无限强权限策略文件”。
下面是一个简单的示例,它使用AES加密java中的字符串消息:
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/byte数组等,取消加密它们(仅在内存中),然后重新加密StringBuffer/数据/字节并将其写入文件。
另一个伟大的密码API是:
有许多例子也可以找到Bouncy:
https://stackoverflow.com/questions/13488155
复制相似问题