- 浏览: 46670 次
- 性别:
- 来自: 北京
文章分类
最新评论
/**
* 加密文件infilename,输出加密后的文件outfilename 返回AES加密密钥
*/
public static byte[] AesEnCrypt(String infilename, String outfilename)
throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey key = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("AES");
InputStream in = new FileInputStream(infilename);
DataOutputStream out = new DataOutputStream(new FileOutputStream(
outfilename));
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipher);
in.close();
out.close();
return key.getEncoded();
}
/**
* 用密钥k解密文件infilename,输出明文文件outfilename
*
* @param infilename
* @param outfilename
* @param k
*/
public static void AesDeCrypt(String infilename, String outfilename,
byte[] k) {
try {
Cipher cipher = Cipher.getInstance("AES");
SecretKey key = new javax.crypto.spec.SecretKeySpec(k, "AES");
OutputStream out = new FileOutputStream(outfilename);
DataInputStream in = new DataInputStream(new FileInputStream(
infilename));
cipher.init(Cipher.DECRYPT_MODE, key);
crypt(in, out, cipher);
in.close();
out.close();
} catch (GeneralSecurityException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
}
}
/**
* 自己定义的加 密函数
*
* @param in
* @param out
* @param cipher
* @throws IOException
* @throws GeneralSecurityException
*/
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 String crypt(String in,Cipher cipher)
throws IOException, GeneralSecurityException {
StringBuffer out = new StringBuffer();
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) {
for(int i = 0;i<in.getBytes().length;i++)
{
System.out.println(in.getBytes().length*8);
if(in.getBytes().length>=i*blockSize)
{
System.arraycopy(in, 0*blockSize, inBytes, 0, blockSize);
cipher.update(inBytes, 0, blockSize, outBytes);
out.append(outBytes);
}
else
{
inLength = in.getBytes().length - i*blockSize;
more = false;
}
}
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.append(outBytes);
return out.toString();
}
* 加密文件infilename,输出加密后的文件outfilename 返回AES加密密钥
*/
public static byte[] AesEnCrypt(String infilename, String outfilename)
throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey key = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("AES");
InputStream in = new FileInputStream(infilename);
DataOutputStream out = new DataOutputStream(new FileOutputStream(
outfilename));
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipher);
in.close();
out.close();
return key.getEncoded();
}
/**
* 用密钥k解密文件infilename,输出明文文件outfilename
*
* @param infilename
* @param outfilename
* @param k
*/
public static void AesDeCrypt(String infilename, String outfilename,
byte[] k) {
try {
Cipher cipher = Cipher.getInstance("AES");
SecretKey key = new javax.crypto.spec.SecretKeySpec(k, "AES");
OutputStream out = new FileOutputStream(outfilename);
DataInputStream in = new DataInputStream(new FileInputStream(
infilename));
cipher.init(Cipher.DECRYPT_MODE, key);
crypt(in, out, cipher);
in.close();
out.close();
} catch (GeneralSecurityException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
}
}
/**
* 自己定义的加 密函数
*
* @param in
* @param out
* @param cipher
* @throws IOException
* @throws GeneralSecurityException
*/
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 String crypt(String in,Cipher cipher)
throws IOException, GeneralSecurityException {
StringBuffer out = new StringBuffer();
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) {
for(int i = 0;i<in.getBytes().length;i++)
{
System.out.println(in.getBytes().length*8);
if(in.getBytes().length>=i*blockSize)
{
System.arraycopy(in, 0*blockSize, inBytes, 0, blockSize);
cipher.update(inBytes, 0, blockSize, outBytes);
out.append(outBytes);
}
else
{
inLength = in.getBytes().length - i*blockSize;
more = false;
}
}
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.append(outBytes);
return out.toString();
}
发表评论
-
java加密算法介绍
2011-05-20 14:06 1044参考: http://justsee.javaeye. ... -
JAVA加密算法(RSA)
2011-05-20 13:51 1234RSA 这种算法1978年就出现了,它是第一个既能用 ... -
JAVA加密算法(BASE64,MD5,SHA)
2011-05-20 13:46 3232这三种加密算法,可谓是非可逆加密,就是不可解密的加密方法。 ... -
JAVA加密算法(DES)
2011-05-20 13:41 1623DES DES-Data Encryption Standa ... -
JAVA加密算法(ECC )
2011-05-20 13:34 3209ECC-Elliptic Curves Cryptograph ... -
JAVA加密算法(DSA)
2011-05-20 13:33 6165接下来我们介绍DSA数字签名,非对称加密的另一种实现。 ... -
JAVA加密算法(DH)
2011-05-20 13:32 1865接下来我们分析DH加密算法,一种适用于网络交互的加密算法。 ... -
关JAVA加密算法的实现实例(基础知识)
2011-05-20 13:30 981基础知识 如基本的单向加密算法: BASE64 MD5 ... -
java加密算法(综合)
2011-05-20 13:27 967对象 参数 algorithm 如:"DSA&quo ... -
java加密算法(MD5withRSA)
2011-05-20 13:24 8947一、前言: 网络 ... -
java加密算法(AES)
2011-05-20 13:20 3133原文地址http://java.sun.com/develop ...
相关推荐
PrimaSoft Encryption Utility强大的enc文件加密解密工具 可成功运用在惊天动地enc
文件加密解密代码
文件加密解密系统
利用java实现将资源文件(包括图片、动画等类型)进行简单的加密、解密
利用汇编语言编写的文件加密解密程序,有详细解释说明、源代码
C#文件加密解密
米牛文件加密解密专家是一款专业强劲的文件加密解密软件,采用多线程加解密方式,加密速度快、安全性高、资源消耗低,不仅拥有文件/文件夹加密、解密、打开等功能,而且对本地加密/解密,更安全、更高效,并且软件...
Java 文件加密解密器 可对所有文件进行加密和解密,第一次运行加密,第二次运行解密。
一个小巧的文件加密解密工具,采用标准3DES算法,可对任意文件进行加密,加密强度极高。
文件的加密解密,一些流的操作,流的加密....DES;文件的加密解密,一些流的操作,流的加密....DES
移动定制版华为HG8546M配置文件加密解密工具
源代码,win32控制台,阐述文件加密解密功能,有注释,主要功能有文件的加密与解密和修改密码
对txt文件的加密解密,采用c++编写。
文件加密解密工具,使用多种算法,源代码注释详细
用VB实现文本文件加密解密的方法.pdf
文件加密解密 文件切割工具 等许多 文件加密解密 文件切割工具文件加密解密 文件切割工具文件加密解密 文件切割工具文件加密解密 文件切割工具文件加密解密 文件切割工具
程序对通达信所有文件加密解密。直接拖拽问件到界面即可。
这是基于VC2010平台下的对文本文件加密解密的一个程序,有整个工程源码
FileCryptor 是一个轻量级文件加密/解密工具。可以安装在一个文件夹里,文件夹及子文件夹里的文件将由应用程序管理。绿色软件,免安装,无痕迹运行,自免疫,操作简单,国密SM4算法,直观易用。
一个简单的文件加密解密例子