使用线程的RSA算法 [英] RSA algorithm using threads

查看:81
本文介绍了使用线程的RSA算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用并发Java进行项目.如果有人知道并发Java或使用线程中的RSA算法.
找到我的顺序RSA算法代码.任何人都可以更改此代码的伪装.

Hi,

I am doing project in concurrent Java. If anyone know RSA algorithm in concurrent Java or using threads.
Find my code for the sequential RSA algorithm. Can anyone change paralleize this code.

import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.PrivateKey;
import javax.crypto.Cipher;
public class rsa {
  private static byte[] encrypt(byte[] inpBytes, PublicKey key,
      String xform) throws Exception {
    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(inpBytes);
  }
  private static byte[] decrypt(byte[] inpBytes, PrivateKey key,
      String xform) throws Exception{
    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.DECRYPT_MODE, key);
    return cipher.doFinal(inpBytes);
  }
  public static void main(String[] unused) throws Exception {
   long starttime =System.currentTimeMillis();
   
   String xform = "RSA/NONE/PKCS1PADDING";
    // Generate a key-pair
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(1024); // 1024 is the keysize.
    KeyPair kp = kpg.generateKeyPair();
    PublicKey pubk = kp.getPublic();
    PrivateKey prvk = kp.getPrivate();
    byte[] dataBytes =
        "J2EE Security for Servlets, EJBs and Web Services".getBytes();
    byte[] encBytes = encrypt(dataBytes, pubk, xform);
    System.out.println(encBytes);
    byte[] decBytes = decrypt(encBytes, prvk, xform);
    System.out.println(decBytes);
    String res=new String(decBytes);
    
    boolean expected = java.util.Arrays.equals(dataBytes, decBytes);
    System.out.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));
    System.out.println(res);
    long endtime=System.currentTimeMillis();
System.out.println("Time required for sequential rsa algorithm:"+(endtime-starttime)+"millisecond");
  }
}

推荐答案

Dude,您的代码未进行实际的编码.您要做的只是使用Java库中内置的加密类.您根本没有实现算法!

此外,RSA是一种顺序算法.如果不知道前一个数据块的结果,则无法对下一个数据块进行编码.仅此一项就说明您无法并行化.

您唯一能做的就是将RSA类的每个实例放在单独的线程上,以便您可以同时加密/解密不同的数据集.
Dude, your code isn''t doing the actual encoding. All you''re doing is using the encryption classes built into the Java libraries. You''re not implementing the algorithm at all!

Besides, RSA is a sequential algorithm. You cannot encode the next block of data without knowing the result of the previous block. That alone says you can''t parallelize it.

About the only thing you can do is put each instance of your RSA class on a seperate thread so you can encrypt/decrypt different datasets at the same time.


这篇关于使用线程的RSA算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆