⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsa.java

📁 QQ聊天工具的源代码
💻 JAVA
字号:
/*
 * RSA.java
 *
 * Created on 2007年12月21日, 下午3:53
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.security.*;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.*;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.io.*;
import java.math.BigInteger;
/**
 * @author deadknight
 */
public class RSA 
{
    
    /** Creates a new instance of RSA */
    public RSA() 
    {
        //
    }
    public static void main(String args[])
    {
	    String N = "BE211D1627D0FA0EDE557CB342256C9B6B7CEDB5A727BF732499393428EF2EC8704ADE733C65B2340B9C1DA282A57EC3B8352D6382E3DC32951EE741181A60FF";
	    String d="463367CFF4679ECCBA9F09EA0E344F439B8E871E99577DC2FFF26B42AD12934CDE8A43617F97D277F856A42EA37D9936A2CE81351A432EB9B8A4DF818DA506F1";
	    String e  = "10001";
	    try
	    {
		    String encryptText = "No matter what it is,it will be OK";
		    
		    KeyFactory  keyFac = KeyFactory.getInstance("RSA");
		    
		    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(N,16), new BigInteger(d,16));
		    RSAPublicKey publicKey = (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
		    RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(N,16), new BigInteger(e,16));
		    RSAPrivateKey privateKey= (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);


		    byte []en = RSA.encrypt(publicKey,encryptText.getBytes());
		    byte []de = RSA.decrypt(privateKey,en);
		    
		    System.out.println(new String(en));
		    System.out.println(new String(de));
	    }catch(Exception exe)
	    {
		    exe.printStackTrace();
	    }
    }
    
    
    public static byte[] encrypt(RSAPublicKey publicKey,byte[]obj)
    {
        if(publicKey != null)
        {
            try
            {
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.ENCRYPT_MODE,publicKey);
                return cipher.doFinal(obj);
                
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        return null;
    }
    public static byte[] decrypt(RSAPrivateKey privateKey,byte[]obj)
    {
        if(privateKey != null)
        {
            try
            {
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE,privateKey);
                return cipher.doFinal(obj);
                
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        return null;
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -