本文介绍了RSA加密Javascript和解密Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用不同的组合花了差不多2天。我在java中使用RSA算法生成非对称密钥对(公有和私有),并尝试使用javascript中的公钥来加密一些文本,并在服务器端的Java中进行解密。尝试解密javascript中加密的字符串时,我得到javax.crypto.IllegalBlockSizeException:数据不能超过128个字节异常。感谢一些帮助...

Spent almost 2 days with different combinations.I am generating a asymmetric key pair (public and private) in java using RSA algorithm and trying to use the public key in javascript to encrypt some text and decrypt back in java on server side. I am getting "javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes" exception while trying to decrypt back the string encrypted in javascript. Would appreciate some help...

使用这个JavaScript库进行加密。

var publicKeyString =//在java中生成的base64encoded公钥字符串

var publicKeyString = ""// base64encoded public key string generated in java

这是我的JavaScript代码

var EncryptionResult = cryptico.encrypt("somestring", publicKeyString);
console.log("Encrypted status-"+EncryptionResult.status);
console.log("Encrypted String-"+EncryptionResult.cipher);

它正在成功加密字符串。

It is successfully encrypting the string.

Java密钥生成和解密

Cipher cipher = Cipher.getInstance("RSA");
KeyFactory fact = KeyFactory.getInstance("RSA");
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024); // 1024 used for normal

KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();

FileOutputStream fos = null;
ObjectOutputStream oos = null;

将密钥存储在用于在解密方法中解密的文件中的代码。

Code to store the private key in file which is used to decrypt in decrypt method.

  RSAPrivateKeySpec rsaPrivKeySpec = fact.getKeySpec(privateKey,
                                                     RSAPrivateKeySpec.class);
 System.out.println("Writing private key...");
 fos = new FileOutputStream(PRIVATE_KEY_FILE);
 oos = new ObjectOutputStream(new BufferedOutputStream(fos));
 oos = new ObjectOutputStream(new BufferedOutputStream(fos));
 oos.writeObject(rsaPrivKeySpec.getModulus());
 oos.writeObject(rsaPrivKeySpec.getPrivateExponent());
 oos.close();

解密方法

public String decrypt(String ciphertext)
      throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException
       {
             if (ciphertext.length() == 0) return null;
             byte[] dec = org.apache.commons.codec.binary.Base64.decodeBase64(ciphertext);
             try {
             System.out.println("Private Key file name----"+PRIVATE_KEY_FILE);
         privateKey = readPrivateKeyFromFile(PRIVATE_KEY_FILE);
        } catch (IOException e) {
        e.printStackTrace();
        }
             cipher.init(Cipher.DECRYPT_MODE, privateKey);
             byte[] decrypted = cipher.doFinal(dec);
             return new String(decrypted, PLAIN_TEXT_ENCODING);
  }

 //reading private key from file

public PrivateKey readPrivateKeyFromFile(String fileName)
    throws IOException {
  FileInputStream fis = null;
  ObjectInputStream ois = null;
   try {
    fis = new FileInputStream(new File(fileName));
    ois = new ObjectInputStream(fis);
    System.out.println("Private Key file-"+fileName);

    BigInteger modulus = (BigInteger) ois.readObject();
    BigInteger exponent = (BigInteger) ois.readObject();

    // Get Private Key
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(modulus, exponent);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = fact.generatePrivate(rsaPrivateKeySpec);
    return privateKey;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ois != null) {
            ois.close();
            if (fis != null) {
                fis.close();
            }
        }
    }
    return null;
 }


推荐答案

感谢Oleg的详细信息。我一定会看看它。

Thanks Oleg for the detailed information. I will definitely take a look into it.

现在我切换到jsencrypt,似乎工作正常。

For now I switched to jsencrypt and it seems to work fine.

编辑

如何获取js加密的编码公钥?

How you get the encoded public key for the js encrypt?

这篇关于RSA加密Javascript和解密Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 22:17