请问如何 使用 现有的公钥私钥进行加密解密??

解决方案 »

  1.   


    import java.io.ByteArrayOutputStream;
    import java.math.BigInteger;
    import java.security.Key;
    import java.security.KeyFactory;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.SecureRandom;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;
    import java.security.spec.RSAPrivateKeySpec;
    import java.security.spec.RSAPublicKeySpec;
    import javax.crypto.Cipher;
    import junit.framework.TestCase;
    import org.bouncycastle.jce.provider.BouncyCastleProvider;public class Debug extends TestCase
    {
      public static final String KeyAlgorithm = "RSA";
      private static final int KEY_SIZE = 1024;//加密块的大小
      
      public void test() throws Exception
      {
        //该示例需要bcprov-ext-jdk16-146.jar包支持, 下载地址: http://download.csdn.net/detail/sushisysu/4380311
        //别问我modulus,publicExponent,privateExponent是从哪里来的,这是generateKeyPair()产生的密钥对,也就是已知的公钥私钥数据;
        byte[] modulus = new BigInteger("103432847531025726180943259002646396222147431986095387510020045242560750369545537521308113486377206887347234700563584476713083970274378809207513294713248302724266912888856793196555093500984810407835229905025764369627980946779265566969289837069313942032036233327642809344319090153936308859895921473063664398793").toByteArray();
        byte[] publicExponent = new BigInteger("65537").toByteArray();//公钥系数一般取值(3,16,65537)
        byte[] privateExponent = new BigInteger("68916827946614895175904440100531916075385078849151277696569042153392744348794497565389953638549727530246909343874605846233886777392485152839838305693629747151549525435955272804689292459163376296006711113775300181469979619755093712130257216863539474472868362864818213372100673093264927045666157005291196724481").toByteArray();
        RSAPrivateKey priKey = generateRSAPrivateKey(modulus, privateExponent);
        RSAPublicKey pubKey = generateRSAPublicKey(modulus, publicExponent);
        
        String source = "你想加密啥东东,请输入到这里吧,加密前原始的明文字符串数据!!!";
        byte[] srcData = source.getBytes();
        byte[] raw = encrypt(pubKey, srcData);
        byte[] tagData = decrypt(priKey, new BigInteger(raw).toByteArray());    
        
        System.out.println("src(原始的明文)="+new String(srcData));
        System.out.println("raw(大数字密文)="+new BigInteger(raw));
        System.out.println("raw(二进制密文)="+new String(raw));
        System.out.println("tag(还原后明文)="+new String(tagData));
      }
      
      public static KeyPair generateKeyPair() throws Exception
      {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KeyAlgorithm,new BouncyCastleProvider());
        keyPairGen.initialize(KEY_SIZE, new SecureRandom());
        KeyPair keyPair = keyPairGen.genKeyPair();
        RSAPublicKey pubKey = (RSAPublicKey)keyPair.getPublic();
        RSAPrivateKey priKey = (RSAPrivateKey)keyPair.getPrivate();
        System.out.println("modulus="+pubKey.getModulus().toString());
        System.out.println("publicExponent="+pubKey.getPublicExponent().toString());
        System.out.println("privateExponent="+priKey.getPrivateExponent());
        return keyPair;
      }
      public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent) throws Exception
      {
        KeyFactory keyFac = KeyFactory.getInstance(KeyAlgorithm, new BouncyCastleProvider());
        RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger(privateExponent));
        return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);
      }
      public static RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent) throws Exception
      {
        KeyFactory keyFac = KeyFactory.getInstance(KeyAlgorithm, new BouncyCastleProvider());
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
        return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
      }
      public static byte[] encrypt(Key prikey, byte[] data) throws Exception
      {
        Cipher cipher = Cipher.getInstance(KeyAlgorithm, new BouncyCastleProvider());
        cipher.init(Cipher.ENCRYPT_MODE, prikey);
        int blockSize = cipher.getBlockSize();//获得加密块大小
        System.out.println("加密的BlockSize=" + blockSize);
        int outputSize = cipher.getOutputSize(data.length);// 获得加密块加密后块大小
        int leavedSize = data.length % blockSize;
        int blocksSize = leavedSize != 0 ? data.length / blockSize + 1 : data.length / blockSize;
        byte[] raw = new byte[outputSize * blocksSize];
        int i = 0;
        while (data.length - i * blockSize > 0)
        {
          if (data.length - i * blockSize > blockSize)
            cipher.doFinal(data, i * blockSize, blockSize, raw, i * outputSize);
          else
            cipher.doFinal(data, i * blockSize, data.length - i * blockSize, raw, i * outputSize);
          i++;
        }    System.out.println("###[1]明文大小:" + data.length);
        System.out.println("###[1]密文大小:" + raw.length);
        return raw;
      }
      public static byte[] decrypt(Key pubkey, byte[] raw) throws Exception
      {
        Cipher cipher = Cipher.getInstance(KeyAlgorithm, new BouncyCastleProvider());
        cipher.init(Cipher.DECRYPT_MODE, pubkey);
        int blockSize = cipher.getBlockSize();//获得加密块大小
        System.out.println("解密的BlockSize=" + blockSize);
        ByteArrayOutputStream bout = new ByteArrayOutputStream(64);
        int j = 0;
        while (raw.length - j * blockSize > 0)
        {
          bout.write(cipher.doFinal(raw, j * blockSize, blockSize));
          j++;
        }
        return bout.toByteArray();
      }
      
      public static void main(String[] args) throws Exception
      {
        Debug d = new Debug();
        d.test();
      }
    }