当前位置: 首页 > news >正文

小清新个人网站/爱链接购买链接

小清新个人网站,爱链接购买链接,如何用asp做网站,wordpress生成原生app{{TOC}}Mac属于消息摘要的一种,但它不同于一般的消息摘要(如MessageDigest),仅通过输入数据无法获得消息摘要,必须有一个发送方和接收方共享的秘密密钥才能生成最终的消息摘要-安全信息摘要。安全信息摘要也称为消息认证(鉴别)码。1&#xff…

{{TOC}}

Mac

属于消息摘要的一种,但它不同于一般的消息摘要(如MessageDigest),仅通过输入数据无法获得消息摘要,必须有一个发送方和接收方共享的秘密密钥才能生成最终的消息摘要-安全信息摘要。安全信息摘要也称为消息认证(鉴别)码。

1,Mac类的实例。

public static final Mac getInstance(String algorithm)

public static final Mac getInstance(String algorithm, String provider)

public static final Mac getInstance(String algorithm, Provider provider)

目前Java8支持HmacMD5、HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384、HmacSHA512六种。

2,获得Mac实例后,需要对Mac对象进行初始化

public final void init(Key key)

public final void init(Key key, AlgorithmParameterSpec spec)

3,更新操作

// 使用指定的字节更新摘要

public void update(byte input)

// 使用指定的字节数组更新摘要

public void update(byte[] input)

// 使用指定的字节数组,从指定的偏移量开始更新摘要

public void update(byte[] input, int offset, int len)

// 使用指定的字节缓冲区更新摘要

public void update(ByteBuffer input)

4,完成操作

public final byte[] doFinal()

public final byte[] doFinal(byte[] input)

public final void doFinal(byte[] output, int outOffset)

5,相关方法

// 重置摘要以供再次使用

public void reset()

// 返回算法名称,比如HmacMD5

public final String getAlgorithm()

// 返回此摘要对象的提供者

public final Provider getProvider()

6,示例代码

byte[] input = "hello world".getBytes();

KeyGenerator kg = KeyGenerator.getInstance("HmacMD5");

Mac mac = Mac.getInstance(kg.getAlgorithm());

mac.init(kg.generateKey());

mac.update(input);

byte[] bytes1 = mac.doFinal();

System.err.println(Base64.getEncoder().encodeToString(bytes1));

byte[] bytes2 = mac.doFinal(input);

System.err.println(Base64.getEncoder().encodeToString(bytes2));

运行结果

bBHZzrFsAyOf944uTecAag==

bBHZzrFsAyOf944uTecAag==

KeyGenerator

与KeyPairGenerator类相似,KeyGenerator用来生成秘密密钥,我们称它为秘密密钥生成器。

1,KeyGenerator类的实例。

public static final KeyGenerator getInstance(String algorithm)

public static final KeyGenerator getInstance(String algorithm, String provider)

public static final KeyGenerator getInstance(String algorithm, Provider provider)

支持Blowfish、AES、DES和DESede等多种对称加密算法实现,以及HmacMD5、HmacSHA1、HmacSHA512等安全消息摘要算法实现。

2,与算法无关的初始化

public final void init(int keysize)

public final void init(SecureRandom random)

public final void init(int keysize, SecureRandom random)

3,特定于算法的初始化

public final void init(AlgorithmParameterSpec spec)

public final void init(AlgorithmParameterSpec spec, SecureRandom random)

4,完成初始化后,获得秘密密钥

public final SecretKey generateKey()

5,示例代码

KeyGenerator kg = KeyGenerator.getInstance("HmacSHA256");

SecretKey key = kg.generateKey();

System.err.println(Base64.getEncoder().encodeToString(key.getEncoded()));

运行结果

4P2j7/b7BBKFNDQm+faw8DS2LRik9lYENzkku7GoXV8=

KeyAgreement

提供密钥协定的功能,是个引擎类,我们称它为密钥协定。

1,KeyAgreement类的实例。

public static final KeyAgreement getInstance(String algorithm)

public static final KeyAgreement getInstance(String algorithm, String provider)

public static final KeyAgreement getInstance(String algorithm, Provider provider)

2,与算法无关的初始化

public final void init(Key key)

public final void init(Key key, SecureRandom random)

3,特定于算法的初始化

public final void init(Key key, AlgorithmParameterSpec spec)

public final void init(Key key, AlgorithmParameterSpec var2, SecureRandom random)

4,执行计划

public final Key doPhase(Key key, boolean lastPhase)

5,最终生成密钥

public final byte[] generateSecret()

public final SecretKey generateSecret(String algorithm)

public final int generateSecret(byte[] shareSeccet, int offset)

6,示例代码

KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH");

KeyPair keyPair = kpg.genKeyPair();

KeyAgreement agreement = KeyAgreement.getInstance(kpg.getAlgorithm());

agreement.init(keyPair.getPrivate());

agreement.doPhase(keyPair.getPublic(),true);

SecretKey secretKey = agreement.generateSecret("DES");

System.err.println(Base64.getEncoder().encodeToString(secretKey.getEncoded()));

运行结果

UsLlIynWQF0=

SecretKeyFactory

与KeyFactory相对应,是个引擎类,它用于产生秘密密钥,我们称它为秘密密钥工厂。

1,SecretKeyFactory类的实例。

public static final SecretKeyFactory getInstance(String algorithm)

public static final SecretKeyFactory getInstance(String algorithm, String provider)

public static final SecretKeyFactory getInstance(String algorithm, Provider provider)

2,相关方法

// 根据提供的密钥规范(密钥材料)生成SecretKey对象

public final SecretKey generateSecret(KeySpec var1)

3,示例代码

KeyGenerator kg = KeyGenerator.getInstance("DES");

SecretKey secretKey = kg.generateKey();

byte[] encoded1 = secretKey.getEncoded();

System.err.println("encoded1:" + Base64.getEncoder().encodeToString(encoded1));

SecretKeyFactory factory = SecretKeyFactory.getInstance(kg.getAlgorithm());

SecretKey secret = factory.generateSecret(new DESKeySpec(encoded1));

byte[] encoded2 = secret.getEncoded();

System.err.println("encoded2:" + Base64.getEncoder().encodeToString(encoded2));

运行结果

encoded1:7EoOkZ0s4OM=

encoded2:7EoOkZ0s4OM=

Cipher

为加密和解密提供密码功能。

1,Cipher类的实例。

public static final Cipher getInstance(String algorithm)

public static final Cipher getInstance(String algorithm, String provider)

public static final Cipher getInstance(String algorithm, Provider provider)

2,算法/工作模式/填充模式,不同的算法支持不同的工作模式及填充模式

Cipher.getInstance("DES") // 简单实现

Cipher.getInstance("DES/CBC/PKCS5Padding")

3,相关模式常量

// 加密模式

public static final int ENCRYPT_MODE = 1;

// 解密模式

public static final int DECRYPT_MODE = 2;

// 包装模式

public static final int WRAP_MODE = 3;

// 解包模式

public static final int UNWRAP_MODE = 4;

// 要解包的密钥为“公钥”常量

public static final int PUBLIC_KEY = 1;

// 要解包的密钥为“私钥”常量

public static final int PRIVATE_KEY = 2;

// 要解包的密钥为“秘密密钥”常量

public static final int SECRET_KEY = 3;

4,示例代码

KeyGenerator kg = KeyGenerator.getInstance("DES");

SecretKey secretKey = kg.generateKey();

System.err.println(Base64.getEncoder().encodeToString(secretKey.getEncoded()));

// 包装密钥

Cipher cipher = Cipher.getInstance(kg.getAlgorithm());

cipher.init(Cipher.WRAP_MODE, secretKey);

byte[] wrap_key = cipher.wrap(secretKey);

// 解包密钥

cipher.init(Cipher.UNWRAP_MODE, secretKey);

Key key = cipher.unwrap(wrap_key, cipher.getAlgorithm(), Cipher.SECRET_KEY);

System.err.println(Base64.getEncoder().encodeToString(key.getEncoded()));

// 加密

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

byte[] bytes = cipher.doFinal("hello world".getBytes());

// 解密

cipher.init(Cipher.DECRYPT_MODE, secretKey);

byte[] doFinal = cipher.doFinal(bytes);

System.err.println(new String(doFinal));

运行结果

4wEabp4xlxA=

4wEabp4xlxA=

hello world

CipherOutputStream/CipherInputStream

同属Cipher类的扩展,统称为密钥流。按流的输入和输出分为密钥输入流和密钥输出流。

示例代码

KeyGenerator kg = KeyGenerator.getInstance("DES");

SecretKey secretKey = kg.generateKey();

Cipher cipher = Cipher.getInstance(kg.getAlgorithm());

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

// 写入并加密数据

CipherOutputStream cos = new CipherOutputStream(new FileOutputStream("test"), cipher);

DataOutputStream dos = new DataOutputStream(cos);

dos.writeUTF("hello world");

dos.flush();

dos.close();

// 读取并解密数据

cipher.init(Cipher.DECRYPT_MODE, secretKey);

CipherInputStream cis = new CipherInputStream(new FileInputStream("test"), cipher);

DataInputStream dis = new DataInputStream(cis);

String result = dis.readUTF();

System.err.println(result);

运行结果

hello world

http://www.lbrq.cn/news/1311841.html

相关文章:

  • 怎么做网站的seo优化/站长统计app网站
  • 怎么做简单地网站/网站收录网
  • 杭州网站建设案例/关键词优化公司哪家效果好
  • 企业网站建设服务/兰州网站优化
  • 个人网站模板html免费/微商如何引流与推广
  • 洛阳网站建设/比较有名的个人网站
  • 网站建设的一般流程是/seo网站推广简历
  • 制作app需要先做网站/广告联盟怎么做
  • 10个零网站建设/百度怎么进入官方网站
  • 建设银行网站总是崩溃/电商运营工作内容
  • 赤峰做网站开发/百度网首页
  • 装饰公司师大排名/电池优化大师下载
  • 网站上的图片一般多大合适/百度电脑版网页
  • 青岛建手机网站公司/优化疫情防控
  • wordpress 最快的版本/网站优化包括哪些内容
  • 长沙网站优化外包/实时热榜
  • 昔阳做网站公司/seo怎么做推广
  • 梅州免费建站/网络营销外包收费
  • 网站产品页排名怎么做/seo入门教程视频
  • 做服装外单的网站/seo顾问多少钱
  • 前海网站建设/互联网营销案例
  • 沈阳微信网站制作价格/shodan搜索引擎
  • 沧州有做网站的吗/友情链接交换要注意哪些问题
  • seo网站建设是什么意思/百度引流推广怎么做
  • 政府网站都是谁做的/关键词代发排名首页
  • 在线做网站视频在线观看/拍照搜索百度识图
  • 做网络传销网站犯法吗/网站seo什么意思
  • 建设网站公司哪好/网站优化关键词价格
  • icp网站建设/app下载推广平台
  • 乌兰察布做网站/google seo是什么啊
  • 数字IC后端培训教程之数字后端项目典型项目案例解析
  • 智慧公厕系统打造洁净、安全的公共空间
  • CSS手写题
  • 云原生核心技术解析:Docker vs Kubernetes vs Docker Compose
  • 大数据领域开山鼻祖组件Hadoop核心架构设计
  • 一键获取android公钥/ios公钥工具