zoukankan      html  css  js  c++  java
  • 使用java实现对称加密解密(AES),非对称加密解密(RSA)

    对称加密:双方采用同样的秘钥进行加密和解密。特点是速度快,但是安全性没有非对称加密高

    非对称加密:接收方生成的公有秘钥公布给发送方,发送方使用该公有秘钥加密之后,发送给接收方,然后接收方使用私有秘钥解密 如果接收方需要返回消息给发送方,同样也可以接受发送方生成的公有秘钥,使用它加密后发送给发送方。

    (对称加密和非对称加密都是可逆加密,像SHA,MD4,MD5都是不可逆加密,通常用来检验数据在传输的过程中是否被修改)

    AES,RSA加密及解密代码如下:

    import java.security.Key;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.SecureRandom;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
    
    
    public class DigestTest{
        public static void main(String[] args){
            try{
                AESEncodeTest();
                RSAEncodeTest();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
        /**
         * AES对称加密 
         * @throws Exception
         */
        private static void AESEncodeTest() throws Exception{
            String content = "content";
            String key = "key"; //可以将该key存入本地文件,相当于一个秘钥。这个秘钥是不会通过网络传播的。
            //加密
            KeyGenerator kg = KeyGenerator.getInstance("AES");
            kg.init(128, new SecureRandom(key.getBytes()));
            SecretKey secretKey = kg.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
            Cipher enCipher = Cipher.getInstance("AES");
            enCipher.init(Cipher.ENCRYPT_MODE, keySpec);
            byte[] result = enCipher.doFinal(content.getBytes());
            System.out.println("AES加密后:" + new HexBinaryAdapter().marshal(result));
            //解密
            //keyGenerator可以重复使用,但是每次使用前都需要调用init方法
            kg.init(128, new SecureRandom(key.getBytes()));
            SecretKeySpec dekeySpec = new SecretKeySpec(kg.generateKey().getEncoded(),"AES");
            Cipher deCipher = Cipher.getInstance("AES");
            deCipher.init(Cipher.DECRYPT_MODE, dekeySpec);
            System.out.println("AES解密后:" + new String(deCipher.doFinal(result)));
        }
        
        /**
         * RSA非对称加密</br>
         * 接收方生成的publicKey公布给发送方,发送方使用该公有秘钥加密之后,发送给接收方,然后接收方使用私有秘钥解密</br>
         * 如果接收方需要返回消息给发送方,同样也可以接受发送方生成的公有key,使用它加密后发送给发送方
         */
        private static void RSAEncodeTest() throws Exception{
            String content = "content";
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(1024, new SecureRandom());
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            
            //这儿模拟接收方生成了公、私有key。这些key可以存到文件里,要使用的时候读取出来即可
            Key privateKey = keyPair.getPrivate();
            Key publicKey = keyPair.getPublic();
            
            //这儿模拟发送方已经获取了接收方给的publicKey,并且使用它来加密要发送的数据
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(cipher.ENCRYPT_MODE, publicKey);
            byte[] result = cipher.doFinal(content.getBytes());
            System.out.println("RSA加密后:" + new HexBinaryAdapter().marshal(result));
            
            //这儿模拟接收方使用自己生成的私钥进行解密
            Cipher deCipher = Cipher.getInstance("RSA");
            deCipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] deResult = deCipher.doFinal(result);
            System.out.println("RSA解密后:" + new String(deResult));
        }
    }
  • 相关阅读:
    Codeforces Round #536 E. Lunar New Year and Red Envelopes /// 贪心 记忆化搜索 multiset取最大项
    牛客网暑期ACM多校训练营(第五场) F
    关于线段树or 树状树状 在二维平面搞事情!Orz
    ZOJ 3822 ( 2014牡丹江区域赛D题) (概率dp)
    HDU4336 Card Collector (概率dp+状压dp)
    POJ 2151 Check the difficulty of problems(概率DP)
    CF E2
    HDU4089 Activation(概率DP+处理环迭代式子)
    HDU4035 Maze (概率DP)
    牛客 Rabbit的数列 (线段树维护值为x的个数+区间覆盖)
  • 原文地址:https://www.cnblogs.com/hithlb/p/4357749.html
Copyright © 2011-2022 走看看