如下代码:
public byte[] getEncrypt(byte[] key,byte[] session){
// byte[] message=asBin(session);
// System.out.println("session="+message.length);
byte[] encrypted=null;
try{
SecretKeySpec skeySpec = new SecretKeySpec(key,"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
encrypted = cipher.doFinal(session);
}catch (Exception e) {
System.out.println("AES"+ "获取加密串出错," + e.getMessage());
}
//System.out.println(encrypted.length);
return encrypted;
}
public static void main(String[] args)
{
byte[] key={(byte)0xCD,(byte)0xC5,...............};//key是一个128位的密钥。 byte[] session={(byte)0x6C,(byte)0xD4,.............};//session是一个十六个字节的明文。 byte[] testing=getEncrypted(key, session);//得到的密文testing竟然是32个字节。 }
小弟刚刚接触AES,对其不熟悉,请各位大侠帮忙看一下代码有什么问题,该怎样改正。
public byte[] getEncrypt(byte[] key,byte[] session){
// byte[] message=asBin(session);
// System.out.println("session="+message.length);
byte[] encrypted=null;
try{
SecretKeySpec skeySpec = new SecretKeySpec(key,"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
encrypted = cipher.doFinal(session);
}catch (Exception e) {
System.out.println("AES"+ "获取加密串出错," + e.getMessage());
}
//System.out.println(encrypted.length);
return encrypted;
}
public static void main(String[] args)
{
byte[] key={(byte)0xCD,(byte)0xC5,...............};//key是一个128位的密钥。 byte[] session={(byte)0x6C,(byte)0xD4,.............};//session是一个十六个字节的明文。 byte[] testing=getEncrypted(key, session);//得到的密文testing竟然是32个字节。 }
小弟刚刚接触AES,对其不熟悉,请各位大侠帮忙看一下代码有什么问题,该怎样改正。
http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html
import java.security.*;import javax.crypto.*;/**
* AES算法生成密鑰和對文件加解密的實現。
* @author Richard
* AES--DES算法的后續版本,由于DES算法可以通過窮舉法破譯,因此不推薦使用。
*/
public class AESImpl {
//產生AES密鑰
public void createKey() {
try{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("aesKey.txt"));
out.writeObject(key);
out.close();
}catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public void run(String code){
int mode = Cipher.ENCRYPT_MODE;
String inputFileName = "encode.txt";//要加密的文件名
String outputFileName = "aesDecode.txt";//加密后的文件名
if("DECODE".equals(code)){
mode = Cipher.DECRYPT_MODE;
inputFileName = "aesDecode.txt";//要解密的文件名
outputFileName = "encode.txt";//解密后的文件名
}
try{
//讀入AES密鑰文件
ObjectInputStream keyin = new ObjectInputStream(new FileInputStream("aesKey.txt"));
Key key = (Key)keyin.readObject();
keyin.close();
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, key);
//加解密
crypt(in,out,cipher);
in.close();
out.close();
}catch (Exception e) {
e.printStackTrace();
}
} public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, ShortBufferException, GeneralSecurityException {
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while(more){
inLength = in.read(inBytes);
if(inLength == blockSize){
int outLength = cipher.update(inBytes, 0,blockSize,outBytes);
out.write(outBytes,0,outLength);
}
else more = false;
}
if(inLength > 0){
outBytes = cipher.doFinal(inBytes,0,inLength);
}
else{
outBytes = cipher.doFinal();
}
out.write(outBytes);
}
public static void main(String[] args){
AESImpl _aes = new AESImpl();
//_aes.createKey();
_aes.run("DECODE");
}}
先看一下AES算法的介绍
import java.security.*;import javax.crypto.*;/**
* AES算法生成密鑰和對文件加解密的實現。
* @author Richard
* AES--DES算法的后續版本,由于DES算法可以通過窮舉法破譯,因此不推薦使用。
*/
public class AESImpl {
//產生AES密鑰
public void createKey() {
try{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("aesKey.txt"));
out.writeObject(key);
out.close();
}catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public void run(String code){
int mode = Cipher.ENCRYPT_MODE;
String inputFileName = "encode.txt";//要加密的文件名
String outputFileName = "aesDecode.txt";//加密后的文件名
if("DECODE".equals(code)){
mode = Cipher.DECRYPT_MODE;
inputFileName = "aesDecode.txt";//要解密的文件名
outputFileName = "encode.txt";//解密后的文件名
}
try{
//讀入AES密鑰文件
ObjectInputStream keyin = new ObjectInputStream(new FileInputStream("aesKey.txt"));
Key key = (Key)keyin.readObject();
keyin.close();
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, key);
//加解密
crypt(in,out,cipher);
in.close();
out.close();
}catch (Exception e) {
e.printStackTrace();
}
} public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, ShortBufferException, GeneralSecurityException {
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while(more){
inLength = in.read(inBytes);
if(inLength == blockSize){
int outLength = cipher.update(inBytes, 0,blockSize,outBytes);
out.write(outBytes,0,outLength);
}
else more = false;
}
if(inLength > 0){
outBytes = cipher.doFinal(inBytes,0,inLength);
}
else{
outBytes = cipher.doFinal();
}
out.write(outBytes);
}
public static void main(String[] args){
AESImpl _aes = new AESImpl();
//_aes.createKey();
_aes.run("DECODE");
}}
在学习中,......................