3DES


  


/** 
 * @param args在java中调用sun公司提供的3DES加密解密算法时,需要使 
 * 用到$JAVA_HOME/jre/lib/目录下如下的4个jar包: 
 *jce.jar 
 *security/US_export_policy.jar 
 *security/local_policy.jar 
 *ext/sunjce_provider.jar  
 */ 
  
private static final String Algorithm = "DESede"; //定义加密算法,可用 DES,DESede,Blowfish  
 
public static void main(String[] args) throws  UnsupportedEncodingException, GeneralSecurityException {  
    //添加新安全算法,如果用JCE就要把它添加进去   
      String srcString ="142fgdsgdsgfd";
 //     Security.addProvider(new com.sun.crypto.provider.SunJCE());   
    String password64 ="密码";
     String password=Base64Util.decode(password64);
    byte[] binaryData = password64.getBytes(Charset.forName("UTF-8"));
     byte[] data = Base64.decodeBase64(binaryData);
     System.out.println( new String(data, Charset.forName("UTF-8")));
     encrypt2(srcString, password64);
     encrypt(srcString, password64);
 
 
 byte[] encryptdata=Base64.decodeBase64("ExDIYVI6z03oaeBOx9bVxQ==");
 
 Cipher dcipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
 SecretKey dsecretKey = new SecretKeySpec(data,"DESede"); 
 dcipher.init(Cipher.DECRYPT_MODE, dsecretKey);
 byte[] dcrptBytes = dcipher.doFinal(encryptdata);
 System.out.println( new String(dcrptBytes, Charset.forName("UTF-8")));
 
 

    
    
    
 }

private static byte[] encrypt(String srcString, String password64)
  throws NoSuchAlgorithmException, NoSuchPaddingException,
  InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
  UnsupportedEncodingException {
 byte[] binaryData = password64.getBytes(Charset.forName("UTF-8"));
     byte[] data = Base64.decodeBase64(binaryData);
    
  Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
 SecretKey secretKey = new SecretKeySpec(data,"DESede"); 
 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
 byte[] encrptBytes = cipher.doFinal(srcString.getBytes(Charset.forName("UTF-8")));
 byte[] base64Bytes = Base64Util.encode(encrptBytes);
 System.out.println( new String(base64Bytes, Charset.forName("UTF-8")));
 return data;
}  


private static void encrypt2(String srcString, String password64)
  throws NoSuchAlgorithmException, NoSuchPaddingException,
  InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
  UnsupportedEncodingException, GeneralSecurityException {
 Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
 byte[]  base64data= Base64Util.decode(password64.getBytes());
 
     DESedeKeySpec dks = new DESedeKeySpec(base64data);
  SecretKeyFactory keyFactory= SecretKeyFactory.getInstance("DESede");
 SecretKey secretKey = keyFactory.generateSecret(dks);//new SecretKeySpec(data,"DESede"); 
 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
 byte[] encrptBytes = cipher.doFinal(srcString.getBytes(Charset.forName("UTF-8")));
 byte[] base64Bytes = Base64Util.encode(encrptBytes);
 System.out.println( new String(base64Bytes, Charset.forName("UTF-8")));
 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值