java加密

这个是jdk的公钥和私钥的生成与使用


``` python

package cn.itcast.controller;

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.security.Key;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey; import javax.crypto.Cipher; import org.junit.Test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/PublicPrivateKey")public class PublicPrivateKey {    private static final String ALGOGRITHM = "RSA";    private static final String PUBLIC_KEY_PATH = "public.key";    private static final String PRIVATE_KEY_PATH = "private.key";         @Test    public void testGenerate() throws Exception {        //KeyPairGenerator引擎类用于产生密钥对,JDK(7)默认支持的算法有,DiffieHellman、DSA、RSA、EC        KeyPairGenerator generator = KeyPairGenerator.getInstance(ALGOGRITHM);        //产生密钥对        KeyPair keyPair = generator.generateKeyPair();        //获取公钥        PublicKey publicKey = keyPair.getPublic();        //获取私钥        PrivateKey privateKey = keyPair.getPrivate();                 //将公钥与私钥写入文件,以备后用        writeKey(PUBLIC_KEY_PATH, publicKey);        writeKey(PRIVATE_KEY_PATH, privateKey);    }                     @RequestMapping("/ecrypt.do")    public void ecrypt() throws Exception {        Cipher cipher = Cipher.getInstance(ALGOGRITHM);        //读取私钥,进行加密        PrivateKey privateKey = (PrivateKey) readKey("E:\\Users\\Administrator\\workspace\\springmvc_mybatis\\private.key");        cipher.init(Cipher.ENCRYPT_MODE, privateKey);        //加密        String mingwen = "我的明文";                //加密后的字节数组        byte[] results = cipher.doFinal(mingwen.getBytes());              String recvInfo = new String(results);        System.out.println(recvInfo);                                PublicKey publicKey = (PublicKey) readKey("E:\\Users\\Administrator\\workspace\\springmvc_mybatis\\public.key");        cipher.init(Cipher.DECRYPT_MODE, publicKey);        //解密        byte[] deciphered = cipher.doFinal(results);        //得到明文        String publicvalue = new String(deciphered);        System.out.println(publicvalue);    }              public void writeKey(String path, Key key) throws Exception {        FileOutputStream fos = new FileOutputStream(path);        ObjectOutputStream oos = new ObjectOutputStream(fos);        oos.writeObject(key);        oos.close();    }         public static Key readKey(String path) throws Exception {        FileInputStream fis = new FileInputStream(path);        ObjectInputStream bis = new ObjectInputStream(fis);        Object object = bis.readObject();        bis.close();        return (Key) object;  













```

```

```

















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值