0
点赞
收藏
分享

微信扫一扫

jsp新代码第225课

mm_tang 2022-05-14 阅读 52


new225.java

package pack03;

import java.security.Key;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;


public class new225
{
Key key1;

public new225()
{

}

public new225(String str)
{
setKey(str);
}

public Key getKey()
{
return key1;
}

public void setKey(Key key)
{
this.key1 = key;
}

public void setKey(String str)
{
try
{
KeyGenerator _geGenerator = KeyGenerator.getInstance("DES");
_geGenerator.init(new SecureRandom(str.getBytes()));
this.key1 = _geGenerator.generateKey();
_geGenerator = null;

}
catch (Exception e)
{
throw new RuntimeException("Error initalizing sqlmap class cause:" + e);
}
}

public String encryptStr(String strMing)
{
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";

Encoder encoder = Base64.getEncoder();

try
{
byteMing = strMing.getBytes("utf8");
byteMi = this.encryptByte(byteMing);
strMi = encoder.encodeToString(byteMi);
}
catch (Exception e)
{
throw new RuntimeException("Error initializing sqlmap class cause" + e);
}
finally
{
encoder = null;
byteMing = null;
byteMi = null;
}

return strMi;
}

private byte[] encryptByte(byte[] byteMing)
{
byte[] byteFina = null;
Cipher cipher;
try
{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key1);
byteFina = cipher.doFinal(byteMing);
}
catch (Exception e)
{
throw new RuntimeException("Error initializing sqlmap class cause" + e);
}
finally
{
cipher = null;
}
return byteFina;
}

public String decryptStr(String strMi)
{
Decoder decoder = Base64.getDecoder();

byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";

try
{
byteMi = decoder.decode(strMi);
byteMing = this.decryptByte(byteMi);
strMing = new String(byteMing,"utf8");

}
catch (Exception e)
{
throw new RuntimeException("Error initializing sqlmap class cause:" + e);
}
finally
{
decoder = null;
byteMing = null;
byteMi = null;
}

return strMing;
}

private byte[] decryptByte(byte[] byteMi)
{
Cipher cipher;
byte[] byteFina = null;
try
{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key1);
byteFina = cipher.doFinal(byteMi);

}
catch (Exception e)
{
throw new RuntimeException("Error initializing sqlmap class cause:" + e);
}
finally
{
cipher = null;

}
return byteFina;
}

public void encryptFile(String file,String destFile) throws Exception
{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, this.key1);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);

byte[] buffer = new byte[1024];
int r = 0;
while ((r = cis.read(buffer)) > 0)
{
out.write(buffer,0,r);
}
cis.close();
is.close();
out.close();
}

public void decryptFile(String file,String destFile) throws Exception
{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, this.key1);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
CipherOutputStream cos = new CipherOutputStream(out, cipher);

byte[] buffer = new byte[1024];
int r = -1;
while ((r = is.read(buffer)) >= 0)
{
cos.write(buffer,0,r);
}
cos.close();
out.close();
is.close();
}
}


举报

相关推荐

0 条评论