求口令加密源代码

topbit 2003-08-26 09:49:21
求口令加密源代码。
...全文
32 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
NetixChina 2003-08-27
  • 打赏
  • 举报
回复
package sample.crypto;

import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.BadPaddingException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.interfaces.PBEKey;

public final class PasswordCryptographerSample
extends Object
{
public static void main(String[] args)
throws IOException,
BadPaddingException,
InvalidKeyException,
ClassNotFoundException,
NoSuchPaddingException,
InvalidKeySpecException,
NoSuchAlgorithmException,
IllegalBlockSizeException,
InvalidAlgorithmParameterException
{
SecretKeyFactory keyFactory = null;
PBEParameterSpec parameterSpec = null;
PBEKeySpec key = getPBEKey();
SecretKey secretKey = null;
Cipher cipher = null;
byte[] source = null;
byte[] result = null;
byte[] salt = getSalt();

String sourceFileName = null;
String resultFileName = null;
String secretKeyFileName = null;

for (int i = 0; i<CRYPTOGRAPHERS.length; i++)
{
resultFileName =
RESULT_FILE_NAME+CRYPTOGRAPHERS[i]+".txt";
secretKeyFileName =
KEY_FILE_NAME+CRYPTOGRAPHERS[i]+".txt";
sourceFileName =
SOURCE_FILE_NAME+CRYPTOGRAPHERS[i]+".txt";

keyFactory =
SecretKeyFactory.getInstance(CRYPTOGRAPHERS[i]);
secretKey = keyFactory.generateSecret(key);
saveSecretKey(secretKey, secretKeyFileName);

cipher = Cipher.getInstance(CRYPTOGRAPHERS[i]);
parameterSpec = new PBEParameterSpec(salt, 1000);
cipher.init(
Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
source = loadSource(getSourceFile());
result = cipher.doFinal(source);

saveResult(result, resultFileName);

salt = getSalt();
parameterSpec = new PBEParameterSpec(salt, 1000);
secretKey = loadSecretKey(secretKeyFileName);
result = loadResult(resultFileName);

cipher = Cipher.getInstance(CRYPTOGRAPHERS[i]);
cipher.init(
Cipher.DECRYPT_MODE, secretKey, parameterSpec);
source = cipher.doFinal(result);

saveSource(source, sourceFileName);
}
}

public static PBEKeySpec getPBEKey()
{
String password =
"This is my first password cryptograph program.";
return new PBEKeySpec(password.toCharArray());
}

public static byte[] getSalt()
throws IOException,
ClassNotFoundException
{
File saltFile = new File(getSaltFileName());
if (saltFile.exists())
return loadSalt(getSaltFileName());
Random random = new Random();
byte[] salt = new byte[8];
random.nextBytes(salt);
saveSalt(salt, getSaltFileName());
return salt;
}

private static void saveSource(
byte[] source,
String fileName)
throws IOException
{
FileOutputStream outFile =
new FileOutputStream(fileName);
outFile.write(source);
outFile.close();
}

private static byte[] loadSource(File file)
throws IOException
{
FileInputStream inFile = new FileInputStream(file);
ByteArrayOutputStream outByteArray =
new ByteArrayOutputStream();
int i = -1;
while ((i = inFile.read())!=-1)
outByteArray.write((byte)i);
return outByteArray.toByteArray();
}

private static void saveSalt(
byte[] salt,
String fileName)
throws IOException
{
FileOutputStream outFile =
new FileOutputStream(fileName);
outFile.write(salt);
outFile.close();
}

private static byte[] loadSalt(
String fileName)
throws IOException,
ClassNotFoundException
{
FileInputStream inFile =
new FileInputStream(fileName);
ByteArrayOutputStream outByteArray =
new ByteArrayOutputStream();
int i = -1;
while ((i = inFile.read())!=-1)
outByteArray.write((byte)i);
return outByteArray.toByteArray();
}

private static void saveResult(
byte[] result,
String fileName)
throws IOException
{
FileOutputStream outFile =
new FileOutputStream(fileName);
outFile.write(result);
outFile.close();
}

private static byte[] loadResult(
String fileName)
throws IOException
{
FileInputStream inFile = new FileInputStream(fileName);
ByteArrayOutputStream outByteArray =
new ByteArrayOutputStream();
int i = -1;
while ((i = inFile.read())!=-1)
outByteArray.write((byte)i);
return outByteArray.toByteArray();
}

private static void saveSecretKey(
SecretKey secretKey,
String keyFileName)
throws IOException
{
FileOutputStream outFile =
new FileOutputStream(keyFileName);
ObjectOutputStream outObject =
new ObjectOutputStream(outFile);

outObject.writeObject(secretKey);

outObject.close();
outFile.close();
}

private static SecretKey loadSecretKey(
String keyFileName)
throws IOException,
ClassNotFoundException
{
FileInputStream inFile =
new FileInputStream(keyFileName);
ObjectInputStream inObject =
new ObjectInputStream(inFile);
Object object = inObject.readObject();
return (SecretKey)object;
}

private static File getSourceFile()
throws IOException
{
return new File("Source.txt");
}

private static String getSaltFileName()
{
return "Salt.txt";
}

private static final String[] CRYPTOGRAPHERS =
new String[]
{
"PBEWithMD5AndDES"
};

private static final String KEY_FILE_NAME =
"PasswordKey_";

private static final String RESULT_FILE_NAME =
"PasswordResult_";

private static final String SOURCE_FILE_NAME =
"PasswordSource_";

private PasswordCryptographerSample()
{
super();
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧