zoukankan      html  css  js  c++  java
  • RAS 加密 解密

    写个RAS加密解密例子加深一下印象

    package cheng.test.cipher;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.security.InvalidKeyException;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;
    
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    
    public class RSADemo {
        
         /**
         * 公钥
         */  
        private RSAPublicKey publicKey;  
        /**
         * 私钥
         */  
        private RSAPrivateKey privateKey;  
        /**
         * 密文的长度
         */  
        private int encrytLength = 256;  
        /**
         * 持久化的公钥文件
         */
        private static final String publicKeyFile = "./public.key";
        /**
         * 持久化的私钥文件
         */
        private static final String privateKeyFile = "./private.key";
        /**
         * generate public key and private key
         * @throws NoSuchAlgorithmException
         */
        public void genKey() throws NoSuchAlgorithmException {
            KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA");
            kg.initialize(encrytLength * 8);
            KeyPair kp = kg.generateKeyPair();
            
            publicKey = (RSAPublicKey) kp.getPublic();
            privateKey = (RSAPrivateKey) kp.getPrivate();
            //serialize the public key and the private key
            serailizeKey();
        }
        
        private void serailizeKey() {
            ObjectOutputStream pulicKeyOop = null;
            ObjectOutputStream privateKeyOop = null;
            try {
                pulicKeyOop = new ObjectOutputStream(new FileOutputStream(publicKeyFile));
                pulicKeyOop.writeObject(publicKey);
                privateKeyOop = new ObjectOutputStream(new FileOutputStream(privateKeyFile));
                privateKeyOop.writeObject(privateKey);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(null != pulicKeyOop) {
                        pulicKeyOop.close();
                    }
                    if(null != privateKeyOop) {
                        privateKeyOop.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        private RSAPublicKey getPublicKey() {
            ObjectInputStream ois = null;
            RSAPublicKey key = null;
            try {
                ois = new ObjectInputStream(new FileInputStream(publicKeyFile));
                key = (RSAPublicKey)ois.readObject();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(null != ois) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return key;
        }
    
        private RSAPrivateKey getPrivateKey() {
            RSAPrivateKey key = null;
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream(privateKeyFile));
                key = (RSAPrivateKey)ois.readObject();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(null != ois) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return key;
        }
    
        public byte[] encrypt(byte [] origin) {
            Cipher cipher = null;
            byte[] cn = null;
            try {
                cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.ENCRYPT_MODE, getPrivateKey());
                cn = cipher.doFinal(origin);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            }
            return cn;
        }
        
        public byte[] decrypt(byte[] enc) {
            Cipher cipher = null;
            byte[] cn = null;
            try {
                cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE, getPublicKey());
                cn = cipher.doFinal(enc);
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            }
            return cn;
        }
    
    
        public static void main(String[] args) {
            RSADemo rs = new RSADemo();
    //            rs.genKey();
            String content = "hello world...";
            byte[] encryptedContent = rs.encrypt(content.getBytes());
            System.out.println(new String(encryptedContent));
            byte[] decryptedContent = rs.decrypt(encryptedContent);
            System.out.println("
    " + new String(decryptedContent));
        }
    }
  • 相关阅读:
    【使用 DOM】使用 DOM 元素
    【使用 DOM】使用 Window 对象
    【使用 DOM】使用 Document 对象
    Groovy中的脚本与类
    Groovy操作符
    Android开源天气预报应用Weather-Lite
    进击的RecyclerView入门三(要是能拖动就好了)
    进击的RecyclerView入门二(来点小装饰?)
    进击的RecyclerView入门一(简单上手)
    Android 5.0+删除Sdcard文件
  • 原文地址:https://www.cnblogs.com/zexu-cheng/p/6373559.html
Copyright © 2011-2022 走看看