包含java通用rsa解密的词条

博主:adminadmin 2023-03-19 08:44:09 490

本篇文章给大家谈谈java通用rsa解密,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

有一段用java实现rsa加解密的程序看不懂,希望高手帮我做下注释,详细些,谢谢

//引入文件

import java.security.*;

import javax.crypto.*;

/**

* RSACryptography

* RSACryptography use the privated key to encrypt the plain text and decrypt

* the cipher text with the public key

*/

public class RSACryptography {

Cipher cipher;

/**

构造函数,就是你每次new这个对象RSACryptography 时候就会执行里面的方法

返回一个Cipher对象(其实他就是用来加密解密的)

*/

public RSACryptography() {

try {

cipher = Cipher.getInstance("RSA");//返回一个cipher对象,该类

//应该是单例的

} catch (NoSuchAlgorithmException e) {//抛出异常,没什么说的

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

}

}

/**

好了,重点来了,你需要加密解密的就调用这个方法encrypt_decrypt(),传入一个byte[]的类型值byteInput,,就是你要加密的东西,在传入一个key,这个key 就像钥匙一样,你根据这个key进行加密,也可以根据这个key进行解密的,boolean 类型的 crypto,如果true就是加密,false就是解密

*/

public byte[] encrypt_decrypt(byte[] byteInput, Key key, boolean crypto) {

try {

if(crypto){

cipher.init(Cipher.ENCRYPT_MODE,key);//加密前初始化

}else{

cipher.init(Cipher.DECRYPT_MODE,key);//解密前初始化

}

byte[] cipherByte = cipher.doFinal(byteInput);//进行加密或解密

return cipherByte;//返回你的加密或者解密值类型为byte[]

} catch (InvalidKeyException e) {//抛出异常

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

}

return null;

}

}

java中RSA用私钥加密公钥解密问题

公钥和私钥可以互换的用,用公钥加密私钥解密,用私钥加密公钥解密都ok,方法一样

怎么在ios进行rsa公钥加密,java做rsa私钥解密

1、用公钥加密,用私钥解密。

2、给别人发信息,就从服务器上拉下来别人的公钥,加密后发给他。

3、对方拿到信息后用自己的私钥解密。

4、这样,公钥加密后除了私钥持有人,别人都看不到信息。

5、若是用私钥加密,那么公钥都能解密,还有何安全性可言?

6、私钥加密的场合只有一个,那就是数字签名,用来表明这个信息来源于你。

java RSA 加解密

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 PKCS EncodedKeySpec;

import java security spec X EncodedKeySpec;

import javax crypto Cipher;

import sun misc BASE Decoder;

import sun misc BASE Encoder;

public class RSACoder {

/**

* 得到公钥

* @param key 密钥字符串(经过base 编码)

* @throws Exception

*/

public static PublicKey getPublicKey(String key) throws Exception {

byte[] keyBytes;

keyBytes = (new BASE Decoder()) decodeBuffer(key);

X EncodedKeySpec keySpec = new X EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory getInstance( RSA );

PublicKey publicKey = keyFactory generatePublic(keySpec);

return publicKey;

}

/**

* 得到私钥

* @param key 密钥字符串(经过base 编码)

* @throws Exception

*/

public static PrivateKey getPrivateKey(String key) throws Exception {

byte[] keyBytes;

keyBytes = (new BASE Decoder()) decodeBuffer(key);

PKCS EncodedKeySpec keySpec = new PKCS EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory getInstance( RSA );

PrivateKey privateKey = keyFactory generatePrivate(keySpec);

return privateKey;

}

/**

* 得到密钥字符串(经过base 编码)

* @return

*/

public static String getKeyString(Key key) throws Exception {

byte[] keyBytes = key getEncoded();

String s = (new BASE Encoder()) encode(keyBytes);

return s;

}

public static void main(String[] args) throws Exception {

KeyPairGenerator keyPairGen = KeyPairGenerator getInstance( RSA );

//密钥位数

keyPairGen initialize( );

//密钥对

KeyPair keyPair = keyPairGen generateKeyPair();

// 公钥

PublicKey publicKey = (RSAPublicKey) keyPair getPublic();

// 私钥

PrivateKey privateKey = (RSAPrivateKey) keyPair getPrivate();

String publicKeyString = getKeyString(publicKey);

System out println( public:\n + publicKeyString);

String privateKeyString = getKeyString(privateKey);

System out println( private:\n + privateKeyString);

//加解密类

Cipher cipher = Cipher getInstance( RSA );//Cipher getInstance( RSA/ECB/PKCS Padding );

//明文

byte[] plainText = 我们都很好!邮件 getBytes();

//加密

cipher init(Cipher ENCRYPT_MODE publicKey);

byte[] enBytes = cipher doFinal(plainText);

//通过密钥字符串得到密钥

publicKey = getPublicKey(publicKeyString);

privateKey = getPrivateKey(privateKeyString);

//解密

cipher init(Cipher DECRYPT_MODE privateKey);

byte[]deBytes = cipher doFinal(enBytes);

publicKeyString = getKeyString(publicKey);

System out println( public:\n +publicKeyString);

privateKeyString = getKeyString(privateKey);

System out println( private:\n + privateKeyString);

String s = new String(deBytes);

System out println(s);

}

lishixinzhi/Article/program/Java/hx/201311/25516

Java RSA 加密解密中 密钥保存并读取,数据加密解密并保存读取 问题

帮你完善了下代码。

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.io.Reader;

import java.util.Map;

public class Test {

    static String publicKey;

    static String privateKey;

    public Test() throws Exception {

        // TODO Auto-generated constructor stub

        MapString, Object keyMap = RSAUtils.genKeyPair();

        publicKey = RSAUtils.getPublicKey(keyMap);

        privateKey = RSAUtils.getPrivateKey(keyMap);

        // 保存密钥,名字分别为publicKey。txt 和privateKey。txt;

        PrintWriter pw1 = new PrintWriter(new FileOutputStream(

                "D:/publicKey.txt"));

        PrintWriter pw2 = new PrintWriter(new FileOutputStream(

                "D:/privateKey.txt"));

        pw1.print(publicKey);

        pw2.print(privateKey);

        pw1.close();

        pw2.close();

        // 从保存的目录读取刚才的保存的公钥,

        String pubkey = readFile("D:/publicKey.txt");// 读取的公钥内容;

        String data = readFile("D:/1.txt"); // 需要公钥加密的文件的内容(如D:/1.txt)

        byte[] encByPubKeyData = RSAUtils.encryptByPublicKey(data.getBytes(),

                pubkey);

        //将加密数据base64后写入文件

        writeFile("D:/Encfile.txt", Base64Utils.encode(encByPubKeyData).getBytes("UTF-8"));

        // 加密后的文件保存在

        String prikey = readFile("D:/privateKey.txt");// 从保存的目录读取刚才的保存的私钥,

        String Encdata = readFile("D:/Encfile.txt");// 刚才加密的文件的内容;

        byte[] encData = Base64Utils.decode(Encdata);

        byte[] decByPriKeyData = RSAUtils.decryptByPrivateKey(encData, prikey);

        // 解密后后的文件保存在D:/Decfile.txt

        writeFile("D:/Decfile.txt", decByPriKeyData);

    }

    private static String readFile(String filePath) throws Exception {

        File inFile = new File(filePath);

        long fileLen = inFile.length();

        Reader reader = new FileReader(inFile);

        char[] content = new char[(int) fileLen];

        reader.read(content);

        System.out.println("读取到的内容为:" + new String(content));

        return new String(content);

    }

    private static void writeFile(String filePath, byte[] content)

            throws Exception {

        System.out.println("待写入文件的内容为:" + new String(content));

        File outFile = new File(filePath);

        OutputStream out = new FileOutputStream(outFile);

        out.write(content);

        if (out != null) out.close();

    }

    public static void main(String[] args) throws Exception {

        // TODO Auto-generated method stub

        new Test();

    }

}

测试结果:

读取到的内容为:MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVr9D9hYPD4kS5k86dRm+utyt5XGOSTPwT0YeoBnQmgeISkNsbtUFkY6txyodVl26IM1H5iwJ1jMQ63+lXfZxzNpeA+rHaxmeQ2qI+5ES9AF7G6KIwjzakKsA08Ly+1y3dp0BnoyHF7/Pj3AS28fDmE5piea7w36vp4E3Ts+F9vwIDAQAB

读取到的内容为:锘县ahaha

Java RSA解密

根据已知的公钥m与e生成PublicKey,然后加密,需要用到bouncycastle这个库,大致代码如下:

// 生成m与e

byte[] mBytes = Hex.decode("C535AD4F...略");

BigInteger m = new BigInteger(1, mBytes);

BigInteger e = BigInteger.valueOf(0x10001);

// 恢复公钥

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PublicKey publicKey = keyFactory.generatePublic(new RSAPublicKeySpec(m, e));

/* 根据经验,那个JS加密后的密文用Java解出来是反转的字符串,

 * 所以如果想要达到与JS完全一致的效果的话,需要将明文先反转,

 * 即将 "admin" 变成 "nimda"

 */

byte[] data = new StringBuilder("admin").reverse().toString().getBytes();

// 现在可以用publicKey加密了

Cipher cipher = Cipher.getInstance("RSA", new BouncyCastleProvider());

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

byte[] enc = cipher.doFinal(data);

System.out.println(Hex.toHexString(enc));

理论上代码大概是这样。加密结果是:

0d42e0ab31391a536346a1a1af7696e21734ab6004b33f4ce1add778d0c3b8b93141cf0422ef3e086e41d0dcf96c9908e30dd9b993c1eea0d0a392f2ecf4347e5ed7d9b3451796ef04203248536ebca02120a9c3fa520f88ca43ec0df30d2210026ac98e0dcd460bd3bd38b4ddd30a5fe0a0c103a5bd02b2e7eb9ae3a2ddab68

关于java通用rsa解密和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。