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

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;
    }
}

 

 

 

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));

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值