我正在编写代码来使用种子加密和解密文件,以及需要验证文件的散列以确保它没有被修改。哈希计算得非常好,并附加到明文文件中。加密也像我用相同的种子输入解密密文一样,我得到原始明文以及一些哈希值。但是,这个哈希与原始文件完全相同<(我甚至没有打开密文文件,差别是1个字节)。有人可以帮帮我吗?谢谢,
以下是使用命令行参数调用的加密[plaintext file] [种子]
public static KeyGenerator key_gen = null;
public static SecretKey sec_key = null;
public static byte[] raw = null;
private static SecretKeySpec sec_key_spec = null;
private static Cipher sec_cipher = null;
//for DSA
private static KeyPairGenerator keypairgen = null;
private static KeyPair keypair = null;
private static DSAPrivateKey private_key = null;
private static DSAPublicKey public_key = null;
private static Signature dsa_sig = null;
private static SecureRandom secRan = null;
public static void main(String args[]){
FileInputStream in_file = null;
FileInputStream in_file2 = null;
FileOutputStream out_file = null;
byte[] sha_hash = null;
//byte[] hmac_hash = null;
byte[] aes_ciphertext = null;
int read_bytes = 0;
try{
//open files
in_file = new FileInputStream(args[0]);
out_file = new FileOutputStream("CipherText.txt");
byte[] seed = args[1].getBytes();
//read file into a byte array
byte[] msg = new byte[in_file.available()];
read_bytes = in_file.read(msg);
//SHA-1 Hash
sha_hash = sha1_hash(msg);
byte[] concatenatedMSG = new byte[msg.length + sha_hash.length];
for (int i = 0; i < concatenatedMSG.length; i++)
{
if(i < msg.length)
concatenatedMSG[i] = msg[i];
else
concatenatedMSG[i] = sha_hash[i - msg.length];
}
//print out hash in hex
System.out.println("SHA-1 Hash: " + toHexString(sha_hash));
//encrypt file with AES
//key setup - generate 128 bit key
SecureRandom rand = new SecureRandom();
rand.setSeed(seed);
key_gen = KeyGenerator.getInstance("AES");
key_gen.init(128, rand);
sec_key = key_gen.generateKey();
//get key material in raw form
raw = sec_key.getEncoded();
sec_key_spec = new SecretKeySpec(raw, "AES");
//create the cipher object that uses AES as the algorithm
sec_cipher = Cipher.getInstance("AES");
//do AES encryption
aes_ciphertext = aes_encrypt(concatenatedMSG);
out_file.write(aes_ciphertext);
out_file.close();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if (in_file != null){
in_file.close();
}
if(out_file != null){
out_file.close();
}
if(in_file2 != null){
in_file2.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
输出是:
SHA-1 Hash: 67:0E:EF:99:0B:62:F4:BA:40:78:8C:34:8F:61:9E:40:E4:CF:65:06
解密文件:使用CipherText.txt调用[加密中的相同种子]
private static KeyGenerator key_gen = null;
private static SecretKey sec_key = null;
private static byte[] raw = null;
private static SecretKeySpec sec_key_spec = null;
private static Cipher sec_cipher = null;
//for DSA
private static KeyPairGenerator keypairgen = null;
private static KeyPair keypair = null;
private static DSAPrivateKey private_key = null;
private static DSAPublicKey public_key = null;
private static Signature dsa_sig = null;
private static SecureRandom secRan = null;
public static void main(String args[])
{
FileInputStream in_file = null;
FileInputStream in_file2 = null;
FileOutputStream out_file = null;
String decrypted_str = new String();
int read_bytes = 0;
try{
//open files
in_file = new FileInputStream(args[0]);
byte[] seed = args[1].getBytes();
//encrypt file with AES
//key setup - generate 128 bit key
SecureRandom rand = new SecureRandom();
rand.setSeed(seed);
key_gen = KeyGenerator.getInstance("AES");
key_gen.init(128, rand);
sec_key = key_gen.generateKey();
//get key material in raw form
raw = sec_key.getEncoded();
sec_key_spec = new SecretKeySpec(raw, "AES");
//create the cipher object that uses AES as the algorithm
sec_cipher = Cipher.getInstance("AES");
//decrypt file
byte[] ciphtext = new byte[in_file.available()];
in_file.read(ciphtext);
decrypted_str = aes_decrypt(ciphtext);
byte[] decryptedBytes = decrypted_str.getBytes();
byte[] decryptedMSG = new byte[decryptedBytes.length - 20];
byte[] decryptedHash = new byte[20];
System.out.print(decrypted_str);
System.out.print("\n");
for(int i = 0; i < decryptedMSG.length; i++)
decryptedMSG[i] = decryptedBytes[i];
for(int i = 0; i < decryptedHash.length; i++)
decryptedHash[i] = decryptedBytes[i+ decryptedMSG.length];
String decryptedCalcSHA = toHexString(sha1_hash(decryptedMSG));
String fileSHA = toHexString(decryptedHash);
System.out.println("DEC:" +decryptedCalcSHA);
System.out.println("FIL:" +fileSHA);
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if (in_file != null){
in_file.close();
}
if(out_file != null){
out_file.close();
}
if(in_file2 != null){
in_file2.close();
}
} catch(Exception e2)
{
e2.printStackTrace();
}
}
}
解密输出:
This is a test message to see if the file decrypts properly. Nothing special
in this file.
Juts a pure test to see if my code works. Two chemists walk into a bar...
gï™bôº@xŒ4?až@äÏe
DEC:67:0E:EF:99:0B:62:F4:BA:40:78:8C:34:8F:61:9E:40:E4:CF:65:06
FIL:67:0E:EF:99:0B:62:F4:BA:40:78:8C:34:3F:61:9E:40:E4:CF:65:06
不同之处在于8F与3F,即使文件未手动修改。
编辑:FIL SHA哈希,3F的哈希值不正确,它应该是8F。