JAVA RSA

1. generate key

KeyRSA.java

import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

/**
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class KeyRSA {
private KeyPairGenerator kpg = null;
private KeyPair kp = null;
private PublicKey public_key = null;
private PrivateKey private_key = null;
private FileOutputStream public_file_out = null;
private ObjectOutputStream public_object_out = null;
private FileOutputStream private_file_out = null;
private ObjectOutputStream private_object_out = null;

/**
  */
public KeyRSA(int in, String address) throws NoSuchAlgorithmException,FileNotFoundException, IOException 
    {
        kpg = KeyPairGenerator.getInstance("RSA"); //generate pair
        kpg.initialize(in); //(512-2048)
        kp = kpg.genKeyPair(); //
        public_key = kp.getPublic(); //
        private_key = kp.getPrivate(); //
        //
        public_file_out = new FileOutputStream(address + "/public_key.dat");
        public_object_out = new ObjectOutputStream(public_file_out);
        public_object_out.writeObject(public_key);
        //
        private_file_out = new FileOutputStream(address + "/private_key.dat");
        
        private_object_out = new ObjectOutputStream(private_file_out);
        private_object_out.writeObject(private_key);
  }

  public static void main(String[] args) {
    try {
      System.out.println("generated");
      new KeyRSA(1024, "./");
    }
    catch (IOException ex) {
    }
    catch (NoSuchAlgorithmException ex) {
    }
  }
}
2. RAS encrypt

RSACoderSimple.java

import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;
//import org.bouncycastle.util.encoders.Hex;
public class RSACoderSimple {
 
    public static String byte2hex(byte[] b) {
        String hs = ""; 
        String stmp = ""; 
        for (int n = 0; n < b.length; n++) { 
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); 
            if (stmp.length() == 1) 
                hs = hs + "0" + stmp; 
            else 
                hs = hs + stmp; 
        } 
        return hs.toUpperCase(); 
    } 
    public static void main(String[] args) throws Exception {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        //numbers
        keyPairGen.initialize(1024);
        //key pair
        KeyPair keyPair = keyPairGen.generateKeyPair();
        //public key
        PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        //private key
        PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

        //cipher class
        Cipher cipher = Cipher.getInstance("RSA");//Cipher.getInstance("RSA/ECB/PKCS1Padding");
        byte[] plainText = "adcdef".getBytes();
        System.out.println("plain="+byte2hex(plainText));
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] enBytes = cipher.doFinal(plainText);

        //decode
        System.out.println("after en="+byte2hex(enBytes));
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[]deBytes = cipher.doFinal(enBytes);
        System.out.println("after de="+byte2hex(deBytes));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值