一个用JAVA开发的会话密钥程序(二)

2014-11-23 20:09:06 · 作者: · 浏览: 32
t ,and then the cipherPriKey
ByteArrayOutputStream baos=new ByteArrayOutputStream();
baos.write(salt);
baos.write(cipherPriKey);
return baos.toByteArray();
}



//用会话密钥加密给定的文件,然后用公钥加密会话密钥,并存入文件中
//最后加密后的文件由密钥长度+已加密的密钥(会话密钥)+密文
public static void encrypt()throws Exception{

//转换成RSA密钥
X509EncodedKeySpec keySpec=new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
PublicKey publickey=keyFactory.generatePublic(keySpec);
//打开存贮密文的文件
DataOutputStream output=new DataOutputStream(new FileOutputStream(ENCRPTOR_TEXT_FILE));
//创建RSA的CIpher
Cipher rsaCipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsaCipher.init(Cipher.ENCRYPT_MODE,publickey);
//创建会话密钥(Rijndael)
KeyGenerator rijndaelKeyGenerator=KeyGenerator.getInstance("Rijndael");
rijndaelKeyGenerator.init(256);
Key rijndaelKey=rijndaelKeyGenerator.generateKey();
//公钥加密会话密钥
byte[] encodedKeyBytes=rsaCipher.doFinal(rijndaelKey.getEncoded());
output.writeInt(encodedKeyBytes.length);
output.write(encodedKeyBytes);
//产生IV向量
SecureRandom random=new SecureRandom();
byte[] iv=new byte[16];
random.nextBytes(iv);
output.write(iv);

//加密正文
IvParameterSpec spec=new IvParameterSpec(iv);
Cipher symmetricCipher=Cipher.getInstance("Rijndael/CBC/PKCS5Padding");
symmetricCipher.init(Cipher.ENCRYPT_MODE,rijndaelKey,spec);
CipherOutputStream cos=new CipherOutputStream(output,symmetricCipher);
FileInputStream input=new FileInputStream(TEXT_FILE);

int theByte=0;
while((theByte=input.read())!=-1){
cos.write(theByte);
}
input.close();
cos.close();
return;
}



//得到私钥
private static byte[] passwordDecrypt(char[] password,byte[] ciphertext)
throws Exception{
byte[] salt=new byte[8];
ByteArrayInputStream bais=new ByteArrayInputStream(ciphertext);
bais.read(salt,0,8);
byte[] remainingCiphertext=new byte[ciphertext.length-8];
bais.read(remainingCiphertext,0,ciphertext.length-8);
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
SecretKey key=keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec=new PBEParameterSpec(salt,ITERATIONS);
Cipher cipher=Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher.init(Cipher.DECRYPT_MODE,key,paramSpec);
return cipher.doFinal(remainingCiphertext);
}


//解密加密的文件
public static void decrypt()
throws Exception{
FileInputStream fis=new FileInputStream(ENCRYPT_PRIVATEKEY_FILE);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int theByte=0;
while((theByte=fis.read())!=-1){
baos.write(theByte);
}
fis.close();
//得到被加密的私钥
byte[] keyBytes=baos.toByteArray();
baos.close();
//得到私钥
byte[] sKey=passwordDecrypt(password.toCharArray(),keyBytes);
//产生RSA私钥
PKCS8EncodedKeySpec keySpec=new PKCS8EncodedKeySpec(sKey);
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
PrivateKey privateKey=keyFactory.generatePrivate(keySpec);
Cipher rsaCipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");

DataInputStream dis=new DataInputStream(new FileInputStream(ENCRPTOR_TEXT_FILE));
//读密文中密码长度和密码
byte[] encryptedKeyBytes=new byte[dis.readInt()];
dis.readFully(encryptedKeyBytes);
rsaCipher.init(Cipher.DECRYPT_MODE,privateKey);
byte[] rijdaelKeyBytes=rsaCipher.doFi