html页面代码结合Java加密,加密的HTML文件和文件,用在Java应用程序解密

本文提供了一个使用AES算法在Java中进行加密和解密的简单示例。代码首先生成AES128位密钥,然后用它来初始化加密和解密的Cipher对象。如果要使用256位密钥,需要下载并安装Java加密扩展(JCE)无限制强制管辖权策略文件。示例代码将字符串加密,解密,并展示如何在内存中操作加密和解密的字节数组。
摘要由CSDN通过智能技术生成

有此链接上写着:

使用AES与Java技术(无法使用的联系,通常是由于域名的存在一个数字):http://192.9.162.55/developer/technicalArticles/Security/AES/AES_v1.html

默认情况下,你可以直至AES 128位。

为了使用256位AES密钥,您必须从here下载并安装“Java加密扩展(JCE)无限制强制管辖权策略文件”。

下面是一个简单的例子进行加密的解密使用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这样的方式来进行修改/字节数组等,解密它们(只在内存中)完成所需的工作,然后重新加密StringBuffer/data/bytes并将其写入文件。

另一个伟大的Cryptograpic API是:

有可以为充气城堡API也可以找到许多例子:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值