⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 temp.txt

📁 自己写的java版的加密解密网络算法应用。有对称
💻 TXT
字号:
//私钥加密类
	private Cipher cipher; 
	private Key key;
	private KeyPair keypair;
	/*public Encrypt()
	{
	}*/

	/**
	 * 产生一个私钥,
	 * @param alg 加密算法
	 * @param len 加密长度
	 * @return 返回也key
	 * @throws Exception 如果不存在alg这个加密算法,抛出exception
	 */
    public Key getKey(String alg,int len)throws Exception
    {
    	cipher = Cipher.getInstance(alg); 
    	KeyGenerator keyGen = KeyGenerator.getInstance(alg);
    	keyGen.init(len);
    	key = keyGen.generateKey();
    	return key;   	
    }
    /**
     * 使用私钥加密
     * @param text 明文
     * @return 返回加密密文
     * @throws Exception 异常
     */
    public byte[] privateEncrypt(byte[] plainText)throws Exception
    {
    	cipher.init(Cipher.ENCRYPT_MODE,key);
    	byte[]cipherText = cipher.doFinal(plainText);
        return cipherText;  
    }
    /**
     * 使用私钥解密
     * @param cipherText 密文
     * @return 解密后的明文
     * @throws Exception 解密结果
     */
    public byte[] privateDecrypt(byte[] cipherText)throws Exception
    {
    	cipher.init(Cipher.DECRYPT_MODE,key);
    	byte[]plainText = cipher.doFinal(cipherText);
    	return plainText;
    }
	/**
	 * 产生一个对称密钥,
	 * @param alg 加密算法
	 * @param len 加密长度
	 * @return 返回也key
	 * @throws Exception 如果不存在alg这个加密算法,抛出exception
	 */
    public KeyPair getPair(String alg,int len)throws Exception
    {
    	cipher = Cipher.getInstance(alg); 
    	KeyPairGenerator keygen = KeyPairGenerator.getInstance(alg);
    	keygen.initialize(len);
    	keypair = keygen.generateKeyPair();
    	return keypair;
    }
    /**
     * 使用公钥加密
     * @param text 明文
     * @param mode mode=0用公钥加密 否则用私钥加密
     * @return 返回加密密文
     * @throws Exception 异常
     */
    public byte[] publicEncrypt(byte[] plainText,int mode)throws Exception
    {
    	if(mode==0)
    	{
    		cipher.init(Cipher.ENCRYPT_MODE,keypair.getPublic());
    	}
    	else
    	{
    		cipher.init(Cipher.ENCRYPT_MODE,keypair.getPrivate());
    	}
    	byte[]cipherText = cipher.doFinal(plainText);
        return cipherText;  
    }
    /**
     * 使用公钥加密
     * @param text 明文
     * @return 返回加密密文
     * @throws Exception 异常
     */
    public byte[] publicDecrypt(byte[] plainText,int mode)throws Exception
    {
    	if(mode==0)
    	{
    		cipher.init(Cipher.DECRYPT_MODE,keypair.getPrivate());
    	}
    	else
    	{
    		cipher.init(Cipher.DECRYPT_MODE,keypair.getPublic());
    	}
    	byte[]cipherText = cipher.doFinal(plainText);
        return cipherText;  
    }
    
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -