现代密码学之对称加密-DES及AES算法

提示:
本文章基于上一篇基础理论知识介绍 链接: 现代密码学之描述概要(一)

DES加密

示例代码 des加密算法

Cipher :文档 https://docs.oracle.com/javase/8/docs/api/javax/crypto/Cipher.html#getInstance-java.lang.String-

package com.atguigu.desaes;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class DesAesDemo {
   
    public static void main(String[] args) throws Exception{
   
        // 原文
        String input = "硅谷";
        // des加密必须是8位
        String key = "12345678";
        // 算法
        String algorithm = "DES";
        
        String transformation = "DES";
        // Cipher:密码,获取加密对象
        // transformation:参数表示使用什么类型加密
        Cipher cipher = Cipher.getInstance(transformation);
        // 指定秘钥规则
        // 第一个参数表示:密钥,key的字节数组
        // 第二个参数表示:算法
        SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
        // 对加密进行初始化
        // 第一个参数:表示模式,有加密模式和解密模式
        // 第二个参数:表示秘钥规则
        cipher.init(Cipher.ENCRYPT_MODE,sks);
        // 进行加密
        byte[] bytes = cipher.doFinal(input.getBytes());
        // 打印字节,因为ascii码有负数,解析不出来,所以乱码
//        for (byte b : bytes) {
   
//            System.out.println(b);
//        }
        // 打印密文
        System.out.println(new String(bytes));
    }
}

运行:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值