zoukankan      html  css  js  c++  java
  • Java DES 测试

    package com.des.test;
    
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.KeySpec;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESedeKeySpec;
    import org.apache.commons.codec.binary.Base64;
    
    /**
     *
     * @author y
     */
    public class DESUtil {
        private static final String CHARSET_TYPE    = "UTF-8";
        private static final String ENCRYPT_KEY     = "Z7CKBCcc5KwlZqjbunr1yhZG5nM";
        private static final String ENCRYPT_SCHEME  = "DESede";
        
        byte[] keyAsBytes;
        
        private final KeySpec myKeySpec;
        private final SecretKeyFactory mySecretKeyFactory;
        private final Cipher cipher;
        private final SecretKey key;
        
        public DESUtil() throws InvalidKeyException, UnsupportedEncodingException, 
                NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException{
            
            keyAsBytes = ENCRYPT_KEY.getBytes(CHARSET_TYPE);
            myKeySpec = new DESedeKeySpec(keyAsBytes);
            mySecretKeyFactory = SecretKeyFactory.getInstance(ENCRYPT_SCHEME);
            cipher = Cipher.getInstance(ENCRYPT_SCHEME);
            key = mySecretKeyFactory.generateSecret(myKeySpec);
        }
        
        public String encrypt(String unencryptedString) {
            String encryptedString = null;
            try {
                cipher.init(Cipher.ENCRYPT_MODE, key);
                
                byte[] plainText = unencryptedString.getBytes(CHARSET_TYPE);
                byte[] encryptedText = cipher.doFinal(plainText);
                
                Base64 base64 = new Base64();
                
                encryptedString = base64.encodeAsString(encryptedText);
            } catch (InvalidKeyException ex) {
                Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalBlockSizeException ex) {
                Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BadPaddingException ex) {
                Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            return encryptedString;
        }
        
        public String decrypt(String encryptedString) {
            String decryptedText=null;
            try {
                cipher.init(Cipher.DECRYPT_MODE, key);
                
                Base64 base64 = new Base64();
                
                byte[] encryptedText = base64.decode(encryptedString);
                byte[] plainText = cipher.doFinal(encryptedText);
                
                decryptedText = new String(plainText,CHARSET_TYPE);
            } catch (InvalidKeyException ex) {
                Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalBlockSizeException ex) {
                Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BadPaddingException ex) {
                Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            return decryptedText;
        }
        
        public static void main(String[] args) throws Exception {
            String str = "hello,这是Java DES加密测试数据";
            
            DESUtil desUtil = new DESUtil();
            
            System.out.println("==desc before:"+str);
            String encryptStr = desUtil.encrypt(str);
            System.out.println("==desc after:"+encryptStr);
            System.out.println("==descrypt:"+desUtil.decrypt(encryptStr));
        }
    }
  • 相关阅读:
    Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test)
    maven打包报错:Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test
    关于log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).的问题
    maven-source 1.3 中不支持注释请使用 -source 5 或更高版本以启用注释
    <Android基础>(二) Activity Part 1
    <Android基础>(一)
    数制
    第二次实验报告:使用Packet Tracer分析应用层协议
    在Windows Server 2003中搭建DNS服务器
    第一次作业:使用Packet Tracer分析HTTP包
  • 原文地址:https://www.cnblogs.com/yshyee/p/4448178.html
Copyright © 2011-2022 走看看