public void encryptFile(String file,String destFile,Key key) throws Exception{
key=(new EncryptionUtil(uuidKey()).getKey());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,key);
//判断密钥状态
if(getKeyState(uuidKey())==1){
InputStream is =new FileInputStream(file);
OutputStream os = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte [] buffer = new byte [1024];
int r;
while ((r = cis.read(buffer)) > 0) {
os.write(buffer, 0, r);
}
cis.close();
os.close();
is.close();
}else{
System.out.println("------密钥已过期-----");
}
}
怎么改?求讲解或代码,在线等
3 个解决方案
#1
做个页面让别人上传文件 和 key 处理完成后把新文件让他下载
#2
你是要改什么?不知道如何做rest,还是?
#3
@Path("encryptRest")
@Component
@Scope("request")
public class encryptResource
{
@POST
@Path("/encry")
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String encryptFile(
@PathParam("file")String file,
@PathParam("destFile")String destFile,Key key) throws Exception
{
String Success = "Encryption of success";
String Failure = "Key has expired";
key=(new EncryptionUtil(uuidKey()).getKey());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,key);
//判断密钥状态
if(getKeyState(uuidKey())==1){
InputStream is =new FileInputStream(file);
OutputStream os = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte [] buffer = new byte [1024];
int r;
while ((r = cis.read(buffer)) > 0) {
os.write(buffer, 0, r);
}
cis.close();
os.close();
is.close();
return Success;
}else{
System.out.println("------密钥已过期-----");
return Failure;
}
}
}
@Component
@Scope("request")
public class encryptResource
{
@POST
@Path("/encry")
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String encryptFile(
@PathParam("file")String file,
@PathParam("destFile")String destFile,Key key) throws Exception
{
String Success = "Encryption of success";
String Failure = "Key has expired";
key=(new EncryptionUtil(uuidKey()).getKey());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,key);
//判断密钥状态
if(getKeyState(uuidKey())==1){
InputStream is =new FileInputStream(file);
OutputStream os = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte [] buffer = new byte [1024];
int r;
while ((r = cis.read(buffer)) > 0) {
os.write(buffer, 0, r);
}
cis.close();
os.close();
is.close();
return Success;
}else{
System.out.println("------密钥已过期-----");
return Failure;
}
}
}
#1
做个页面让别人上传文件 和 key 处理完成后把新文件让他下载
#2
你是要改什么?不知道如何做rest,还是?
#3
@Path("encryptRest")
@Component
@Scope("request")
public class encryptResource
{
@POST
@Path("/encry")
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String encryptFile(
@PathParam("file")String file,
@PathParam("destFile")String destFile,Key key) throws Exception
{
String Success = "Encryption of success";
String Failure = "Key has expired";
key=(new EncryptionUtil(uuidKey()).getKey());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,key);
//判断密钥状态
if(getKeyState(uuidKey())==1){
InputStream is =new FileInputStream(file);
OutputStream os = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte [] buffer = new byte [1024];
int r;
while ((r = cis.read(buffer)) > 0) {
os.write(buffer, 0, r);
}
cis.close();
os.close();
is.close();
return Success;
}else{
System.out.println("------密钥已过期-----");
return Failure;
}
}
}
@Component
@Scope("request")
public class encryptResource
{
@POST
@Path("/encry")
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String encryptFile(
@PathParam("file")String file,
@PathParam("destFile")String destFile,Key key) throws Exception
{
String Success = "Encryption of success";
String Failure = "Key has expired";
key=(new EncryptionUtil(uuidKey()).getKey());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,key);
//判断密钥状态
if(getKeyState(uuidKey())==1){
InputStream is =new FileInputStream(file);
OutputStream os = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte [] buffer = new byte [1024];
int r;
while ((r = cis.read(buffer)) > 0) {
os.write(buffer, 0, r);
}
cis.close();
os.close();
is.close();
return Success;
}else{
System.out.println("------密钥已过期-----");
return Failure;
}
}
}