谁帮忙用C#实现下这个加解密函数


秘钥类:
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
public class Key
{
  private static String encryptKey = "7EV/Zzutjzg=";
  public static SecretKey loadKey()
    throws Exception
  {
    BASE64Decoder d = new BASE64Decoder();
    byte[] b = d.decodeBuffer(encryptKey);
    DESKeySpec dks = new DESKeySpec(b);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    return keyFactory.generateSecret(dks);
  }
}
加密解密类:
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Properties;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import org.springframework.beans.factory.FactoryBean;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DBEncrypt
  implements FactoryBean
{
  private Properties properties;
  public Object getObject()
    throws Exception
  {
    return getProperties();
  }
  public Class getObjectType() {
    return Properties.class;
  }
  public boolean isSingleton() {
    return true;
  }
  public Properties getProperties() {
    return this.properties;
  }
  public void setProperties(Properties inProperties) {
    this.properties = inProperties;
    String originalUsername = this.properties.getProperty("user");
    String originalPassword = this.properties.getProperty("password");
    if (originalUsername != null) {
      String newUsername = deEncryptUsername(originalUsername);
      this.properties.put("user", newUsername);
    }
    if (originalPassword != null) {
      String newPassword = deEncryptPassword(originalPassword);
      this.properties.put("password", newPassword);
    }
  }
  private String deEncryptUsername(String originalUsername) {
    return dCode(originalUsername.getBytes());
  }
  private String deEncryptPassword(String originalPassword) {
    return dCode(originalPassword.getBytes());
  }
  public String eCode(String needEncrypt) {
    byte[] result = (byte[])null;
    try {
      Cipher enCipher = Cipher.getInstance("DES");
      SecretKey key = Key.loadKey();
      enCipher.init(1, key);
      result = enCipher.doFinal(needEncrypt.getBytes());
      BASE64Encoder b = new BASE64Encoder();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      b.encode(result, bos);
      result = bos.toByteArray();
    } catch (Exception e) {
      throw new IllegalStateException("System doesn't support DES algorithm.");
    }
    return new String(result);
  }
  public String dCode(byte[] result) {
    String s = null;
    try {
      Cipher deCipher = Cipher.getInstance("DES");
      deCipher.init(2, Key.loadKey());
      BASE64Decoder d = new BASE64Decoder();
      result = d.decodeBuffer(new String(result));
      byte[] strByte = deCipher.doFinal(result);
      s = new String(strByte);
    } catch (Exception e) {
      throw new IllegalStateException("System doesn't support DES algorithm.");
    }
    return s;
  }
  public static void main(String[] args)
  {
    String s = "test";
    DBEncrypt p = new DBEncrypt();
    String afterE = p.eCode(s);
    System.out.println(afterE);
    System.out.println(p.dCode(afterE.getBytes()));
  }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值