public class AESTest {
public static void main(String[] args) {
try {
if (args[0].equals("-genkey")) {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(args[1]));
out.writeObject(key);
out.close();
} else {
int mode;
if (args[0].equals("-encrypt"))
mode = Cipher.ENCRYPT_MODE;
else
mode = Cipher.DECRYPT_MODE; ObjectInputStream keyIn = new ObjectInputStream(
new FileInputStream(args[3]));
Key key = (Key) keyIn.readObject();
keyIn.close(); InputStream in = new FileInputStream(args[1]);
OutputStream out = new FileOutputStream(args[2]);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, key); crypt(in, out, cipher);
in.close();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} /**
* Uses a cipher to transform the bytes in an input stream and sends the
* transformed bytes to an output stream.
*
* @param in
* the input stream
* @param out
* the output stream
* @param cipher
* the cipher that transforms the bytes
*/
public static void crypt(InputStream in, OutputStream out, Cipher cipher)
throws IOException, 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) {
try {
if (args[0].equals("-genkey")) {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(args[1]));
out.writeObject(key);
out.close();
} else {
int mode;
if (args[0].equals("-encrypt"))
mode = Cipher.ENCRYPT_MODE;
else
mode = Cipher.DECRYPT_MODE; ObjectInputStream keyIn = new ObjectInputStream(
new FileInputStream(args[3]));
Key key = (Key) keyIn.readObject();
keyIn.close(); InputStream in = new FileInputStream(args[1]);
OutputStream out = new FileOutputStream(args[2]);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, key); crypt(in, out, cipher);
in.close();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} /**
* Uses a cipher to transform the bytes in an input stream and sends the
* transformed bytes to an output stream.
*
* @param in
* the input stream
* @param out
* the output stream
* @param cipher
* the cipher that transforms the bytes
*/
public static void crypt(InputStream in, OutputStream out, Cipher cipher)
throws IOException, 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);
}
}
请问上面的代码如何运行。
解决方案 »
- List对象之间的赋值
- 代碼中遇到的問題:如圖所示,,如果分別打jar包,工程jar包中的myProject.jar如何讀取conf目錄下的*.properties文件呢?
- 求教,根据一个xml文件定义的内容,生成相应图片
- 帮忙看个程序,无效的游标状态!
- 请问如何创建MDI主窗体,以及子窗体,谢谢指点
- !!!!!指点指点我吧!了解snmp通信常识#####确实很急!帮忙一下拉!
- tomcat配置的迷茫
- 工程中.java和.class文件有啥关系、区别?
- 数据库驱动程序的问题?请各位大侠帮帮我!!
- c语言可以写大数,我也要写大数
- java 菜单,以字符数组的方式添加菜单项时如何给菜单项添加事件处理
- java,向下转型的疑问!
复制完, 导入所有的包后,
命令行输入 java AESTest -genkey ,
或者 java AESTest -encrypt
当然, 运行前要先javac AESTest.java
没有看仔细, 这个程序运行需要两个参数, 第二个参数是保存在某个文件里.
比如,你在D盘根目录下有个a.txt文件, 则你运行 java AESTest -genkey d:/a.txt
或 java AESTest -encrypt d:/a.txt
若 -encrypt, 则四个参数, 分别为: -encrypt,需要加密文件的路径,加密后文件保存路径,密钥文件.
若 其他任意参数, 也为四个参数, 同上.只不过,这里代表解密.
-encrypt加密, 其他字符就解密. 不知道有没解释清楚... 不明白再问吧..