用java实现对字符串的IDEA加密

首先引入bcprov-ext-jdk14-140.jar这个包,可以从http://www.bouncycastle.org上下载,下面是代码

/**
* 加密工具类
*/

package net.risesoft.platform.util;

import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.engines.IDEAEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;

/**
* 加密工具类
*
* @author xiaosong
*
*/
public class IDEAEncrypt
{

private PaddedBufferedBlockCipher cipher = null;

/**
* 密钥的长度
*/
private int keylength;

/**
* 设置Block Cipher的加密引擎, 如IDEA_Engine
*
* @param block_cipher_engine
*            加密引擎
*/
public void setEngine(BlockCipher block_cipher_engine)
{
   /*
   * Setup the DESede cipher engine, create a PaddedBufferedBlockCipher in
   * CBC mode.
   */
   cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
     block_cipher_engine));
}

/**
* 设置Block Cipher的密钥长度
*
* @param length
*/
public void setKeyLength(int length)
{
   this.keylength = length;
}

/**
* 设置密钥
*
* @param encrypt
*            加密还是解密
* @param keyStr
*            密钥字符串
*/
public void init(boolean encrypt, String keyStr)
{
   /**
   * 初始化密钥
   */
   byte[] keybyte = new byte[this.keylength];

   keybyte = Hex.decode(keyStr);// 将密钥加密
   /**
   * encrypt = true 加密 encrypt = false 解密
   */
   cipher.init(encrypt, new KeyParameter(keybyte));
}

/**
* 加密
*
* @param 要加密的字符串
* @return 加密后的密文
*/
public String Encrypt(String mingwen)
{
   byte[] inblock = mingwen.getBytes();
   byte[] outblock = new byte[cipher.getOutputSize(inblock.length)];
   int outL;
   String s1 = "";
   outL = cipher.processBytes(inblock, 0, inblock.length, outblock, 0);
   try
   {
    outL = cipher.doFinal(outblock, outL);
   } catch (CryptoException ce)
   {
    ce.printStackTrace();
   }
   byte[] result = Hex.encode(outblock);
   s1 = new String(result).trim();
   return s1;

}

/**
* 解密
* @param miwen 密文
* @return 明文
*/
public String Decrypt(String miwen)
{
   byte[] inblock = null;
   byte[] outblock = null;
   String s1 = "";
   int outL;
   inblock = Hex.decode(miwen);
   outblock = new byte[cipher.getOutputSize(inblock.length)];
   outL = cipher.processBytes(inblock, 0, inblock.length, outblock, 0);
   try
   {
    outL = cipher.doFinal(outblock, outL);
    s1 = new String(outblock).trim();
   } catch (CryptoException ce)
   {
    ce.printStackTrace();
   }
   return s1;
}

public static void main(String[] args)
{
   IDEAEncrypt cipherTool = new IDEAEncrypt();
   cipherTool.setEngine(new IDEAEngine());
   cipherTool.setKeyLength(128);
   String keyStr = "838jf84fr8jfdfrgrdgdfg";
   cipherTool.init(true, keyStr);
   String miwen = cipherTool.Encrypt("1234567890");
   System.out.println(miwen);


   cipherTool.init(false, keyStr);
   System.out.println(cipherTool.Decrypt(miwen));
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java实现字符串的AES加密和解密可以使用Java Cryptography Extension (JCE)提供的API。以下是一个简单的示例代码: 加密: ``` import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AESEncryption { public static String encrypt(String plainText, String key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } } ``` 解密: ``` import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AESDecryption { public static String decrypt(String encryptedText, String key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes); } } ``` 其中,`encrypt`方法接受两个参数:待加密的明文和密钥,返回加密后的密文;`decrypt`方法接受两个参数:待解密的密文和密钥,返回解密后的明文。这里使用的是AES算法,采用ECB模式和PKCS5Padding填充方式。在实际应用中,需要注意密钥的安全性和密文的传输安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值