java 加密算法 RSA 实现代码

java 加密算法 RSA 实现代码

 

package  com.yill;

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.Key;
import  java.security.KeyPair;
import  java.security.NoSuchAlgorithmException;
import  java.security.PrivateKey;
import  java.security.PublicKey;
import  java.security.SecureRandom;

import  javax.crypto.BadPaddingException;
import  javax.crypto.Cipher;
import  javax.crypto.IllegalBlockSizeException;
import  javax.crypto.NoSuchPaddingException;

import  sun.security.rsa.RSAKeyPairGenerator;

public   class  YillRSA
{

    
/** *//**
     * 
@param args
     
*/

    
public static void main(String[] args)
    
{
        generateKeyPair();
        
byte[] info = "http://www.blogjava.net/yill/".getBytes();
        System.out.println(
"Original info is " + new String(info));
        System.out.println(
"encrypted info is " + new String(encode(info)));
        System.out.println(
"Decrypted info is "
                
+ new String(decode(encode(info))));
    }


    
public static void generateKeyPair()
    
{
        RSAKeyPairGenerator kg 
= new RSAKeyPairGenerator();
        kg.initialize(
1024new SecureRandom());

        KeyPair keyPair 
= kg.generateKeyPair();

        PrivateKey privateKey 
= keyPair.getPrivate();
        writeKeyToDisk(
"C:\\privateKey.dat", privateKey);

        PublicKey publicKey 
= keyPair.getPublic();
        writeKeyToDisk(
"C:\\publicKey.dat", publicKey);

    }


    
public static byte[] encode(byte[] info)
    
{
        PrivateKey privateKey 
= (PrivateKey) readKeyFromDisk("C:\\privateKey.dat");

        
try
        
{
            Cipher cipher 
= Cipher.getInstance("RSA");

            cipher.init(Cipher.ENCRYPT_MODE, privateKey, 
new SecureRandom());

            
return cipher.doFinal(info);
        }

        
catch (NoSuchAlgorithmException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (NoSuchPaddingException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (InvalidKeyException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (IllegalBlockSizeException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (BadPaddingException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }


        
return new byte[0];
    }


    
public static byte[] decode(byte[] info)
    
{
        PublicKey publicKey 
= (PublicKey) readKeyFromDisk("C:\\publicKey.dat");

        
try
        
{
            Cipher cipher 
= Cipher.getInstance("RSA");

            cipher.init(Cipher.DECRYPT_MODE, publicKey, 
new SecureRandom());

            
return cipher.doFinal(info);
        }

        
catch (NoSuchAlgorithmException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (NoSuchPaddingException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (InvalidKeyException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (IllegalBlockSizeException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (BadPaddingException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }


        
return new byte[0];
    }


    
private static void writeKeyToDisk(String file, Key key)
    
{
        
try
        
{
            ObjectOutputStream out 
= new ObjectOutputStream(
                    
new FileOutputStream(file));

            out.writeObject(key);

        }

        
catch (FileNotFoundException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (IOException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    
private static Key readKeyFromDisk(String file)
    
{
        
try
        
{
            ObjectInputStream in 
= new ObjectInputStream(new FileInputStream(
                    file));
            
return (Key) in.readObject();
        }

        
catch (FileNotFoundException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (IOException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
catch (ClassNotFoundException e)
        
{
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
return null;
    }


}

你可能感兴趣的:(java 加密算法 RSA 实现代码)