一个用JAVA开发的会话密钥程序 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-25
vate static byte[] passwordEncrypt(char[] password,byte[] privateKeyBytes)throws Exception{//create 8 byte saltbyte[] salt=new byte[8];Random random=new Random();random.nextBytes(salt);//create a PBE key and cipherPBEKeySpec 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.ENCRYPT_MODE,key,paramSpec);//Encrypt the byte[]byte[] cipherPriKey=cipher.doFinal(privateKeyBytes);//write out salt ,and then the cipherPriKeyByteArrayOutputStream 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的CIpherCipher 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 cip |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于一个用JAVA开发的会话密钥程序 - 编程入门网的所有评论