📄 javaencode.txt
字号:
// 导入 JAVA 标准包
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
// 加密及散列类 Encode
class Encode
{
// 属性定义
private byte code[]; // 密文
private byte digest[]; // 明文散列结果
private byte pass[]; // 用户密钥
private byte text[]; // 明文
private SecretKeySpec key; // 加密密钥
// 构造器
public Encode()
// 默认构造器
{
}
public Encode(String string1, String string2)
// 重载构造器
// 用 string1 初始化 text,用string2 初始化 pass
{
try
{
text=string1.getBytes("UTF8");
pass=string2.getBytes("UTF8");
generateKey();
}
catch (Exception e)
{
System.out.println("构造器发生异常 " + e.getMessage());
}
}
// 公有方法
public static void main(String args[])
// 用于测试类的代码
{
try
{
System.out.println("加密及散列类 Encode 测试代码");
System.out.println(" 使用 SHA-256 对输入字符串进行散列");
System.out.println(" 使用 MD5 对用户密钥进行散列创建加密密钥");
System.out.println(" 使用 AES-128 对输入字符串进行加密处理\n");
String strText="";
String strPass="";
byte temp[]=new byte[255];
System.out.print("请输入明文:");
System.in.read(temp);
for (int i=0; i<temp.length; i++)
{
if ((int)temp[i] == 0) break;
strText+=(char)temp[i];
}
System.out.print("请输入密钥:");
System.in.read(temp);
for (int i=0; i<temp.length; i++)
{
if ((int)temp[i] == 0) break;
strPass+=(char)temp[i];
}
Encode encode=new Encode(strText, strPass);
temp=encode.getDigest();
System.out.println("散列结果:");
for (int i=0; i<temp.length; i++)
{
System.out.print(temp[i] + " ");
if (i%8 == 0)
System.out.println();
}
temp=encode.getCoded();
System.out.println("\n加密结果:");
for (int i=0; i<temp.length; i++)
{
System.out.print(temp[i] + " ");
if (i%8 == 0)
System.out.println();
}
temp=encode.getDecoded();
System.out.println("\n解密结果:");
String result="";
for (int i=0; i<temp.length; i++)
result+=(char)temp[i];
System.out.println(result);
}
catch (Exception e)
{
System.out.println("验证代码段发生异常 "+e.getMessage());
}
}
public byte[] getCoded()
// 返回加密结果
{
cipherMessage();
return code;
}
public byte[] getDecoded()
// 返回解密结果
{
decipherMessage();
return text;
}
public byte[] getDigest()
// 返回散列结果
{
digestMessage();
return digest;
}
// 私有方法
private void cipherMessage()
// 使用 AES 加密明文
{
try
{
Cipher cipher=Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
code=cipher.doFinal(text);
}
catch (Exception e)
{
System.out.println("加密时发生异常 "+e.getMessage());
}
}
private void decipherMessage()
// 使用 AES 解密密文
{
try
{
Cipher cipher=Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
text=cipher.doFinal(code);
}
catch (Exception e)
{
System.out.println("解密时发生异常 "+e.getMessage());
}
}
private void digestMessage()
// 使用 SHA-256 对明文进行散列处理
{
try
{
// 创建散列对象
MessageDigest sha=MessageDigest.getInstance("SHA-256");
// 使用 SHA-256 进行散列处理
sha.update(text);
digest=sha.digest();
}
catch (Exception e)
{
System.out.println("散列时发生异常 "+e.getMessage());
}
}
private void generateKey()
// 对用户密码进行 MD5 散列创建加密密钥
{
try
{
// 创建散列对象
MessageDigest md5=MessageDigest.getInstance("MD5");
// 使用 MD5 进行散列处理
md5.update(pass);
byte temp[]=md5.digest();
// 创建密钥
key=new SecretKeySpec(temp, "AES");
}
catch (Exception e)
{
System.out.println("产生密钥时发生异常 "+e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -