zoukankan      html  css  js  c++  java
  • 【转】Java Cipher类 DES算法(加密与解密)

    Java Cipher类 DES算法(加密与解密)

    1.加密解密类

    import java.security.*;
    
    import javax.crypto.*;
    
    import java.io.*;
    
    //对称加密器
    public class CipherMessage {
        private String algorithm; // 算法,如DES
        private Key key; // 根据算法对应的密钥
        private String plainText; // 明文
    
        KeyGenerator keyGenerator;
        Cipher cipher;
    
        // 函数进行初始化
        CipherMessage(String alg, String msg) {
            algorithm = alg;
            plainText = msg;
        }
    
        // 加密函数,将原文加密成密文
        public byte[] CipherMsg() {
            byte[] cipherText = null;
    
            try {
                // 生成Cipher对象
                cipher = Cipher.getInstance(algorithm);
                // 用密钥加密明文(plainText),生成密文(cipherText)
                cipher.init(Cipher.ENCRYPT_MODE, key); // 操作模式为加密(Cipher.ENCRYPT_MODE),key为密钥
                cipherText = cipher.doFinal(plainText.getBytes()); // 得到加密后的字节数组
                // String str = new String(cipherText);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return cipherText;
        }
    
        // 解密函数,将密文解密回原文
        public String EncipherMsg(byte[] cipherText, Key k) {
            byte[] sourceText = null;
    
            try {
                cipher.init(Cipher.DECRYPT_MODE, k); // 操作模式为解密,key为密钥
                sourceText = cipher.doFinal(cipherText);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new String(sourceText);
    
        }
    
        // 生成密钥
        public Key initKey() {
            try {
                // 初始化密钥key
                keyGenerator = KeyGenerator.getInstance(algorithm);
                keyGenerator.init(56); // 选择DES算法,密钥长度必须为56位
                key = keyGenerator.generateKey(); // 生成密钥
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return key;
        }
    
        
    
        // 获取Key类型的密钥
        public Key getKey() {
            return key;
        }
    
        // 获取Key类型的密钥
        public Key getKey(byte[] k) {
            try {
                key = cipher.unwrap(k, algorithm, Cipher.DECRYPT_MODE);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return key;
        }
    
        // 获取密钥包装成byte[]类型的
        public byte[] getBinaryKey(Key k) {
            byte[] bk = null;
            try {
                bk = cipher.wrap(k);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return bk;
        }
    }

    2.测试

    import java.security.*;
    
    import javax.crypto.*;
    
    import java.io.*;
    
    public class TestMain {
    
        public static void main(String[] args) {
            String algorithm = "DES"; // 定义加密算法,可用 DES,DESede,Blowfish
            String message = "Hello World. 这是待加密的信息"; // 生成个DES密钥
            Key key;
    
            CipherMessage cm = new CipherMessage(algorithm, message);
            key = cm.initKey();
            byte[] msg = cm.CipherMsg();
            System.out.println("加密后的密文为:" + new String(msg));
            // System.out.println("密钥为:"+new String(cm.getBinaryKey(key)));
    
            
            System.out.println(cm.getBinaryKey(key));
            System.out.println("解密密文为:" + cm.EncipherMsg(msg, key));
    
        }
    
    }
  • 相关阅读:
    maven第三章 maven使用入门
    各个软件产生的原因
    maven的pom.xml深入理解
    批量同步订单信息(包括状态)到订单中心心得
    数据库连接超时和go away、如何检测数据库的最大连接数
    记录错误日志的技巧
    架构思想总结同步和事务的具体应用
    业务逻辑复杂性处理
    日志系统总结
    php捕获异常的处理
  • 原文地址:https://www.cnblogs.com/jinhuazhe2013/p/5012234.html
Copyright © 2011-2022 走看看