当前位置: 首页 > 知识库问答 >
问题:

使用jersey的客户端加密和服务器端解密不起作用

宗苗宣
2023-03-14

我正在尝试像这样在客户端加密一个字符串

public static void main(String[] args) throws Exception {
    Client client = Client.create();
    Form form = new Form();
    String en = getRsaEcryptedData();
    form.add("params", getRsaEcryptedData());
    WebResource webResource = client.resource("http://localhost:8080/Server/request/data");
    ClientResponse response = webResource.type(MediaType.APPLICATION_OCTET_STREAM).post(ClientResponse.class, form);
    Reader r = new InputStreamReader(response.getEntityInputStream());  
    StringWriter sw = new StringWriter();  
    char[] buffer = new char[1024];  
    for (int n; (n = r.read(buffer)) != -1; )  
        sw.write(buffer, 0, n);  
    String str = sw.toString();  
    System.out.println(str);


}

private static String getRsaEcryptedData() {
    AsymetricKeyCryptography ac = new AsymetricKeyCryptography();
    String source = "helloworld";
    return new String(ac.encrypt(source));
}

我试着在服务器端像这样解密。我正在使用jersey公开一个web服务。

@POST
@Path("data")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public String getMsg(@FormParam("params") String params) {
    System.out.println("params--> "+params);
    return getRsaDecryptedData(params);
}

private static String getRsaDecryptedData(String data) {
    AsymetricKeyCryptography ac = new AsymetricKeyCryptography();
    return new String(ac.decrypt(data.getBytes()));
}

AysmetricKeyCryptography类如下所示

public class AsymetricKeyCryptography {


public AsymetricKeyCryptography() {
    super();
    if (!areKeysPresent()) {
        generateKey();
    }
}



/**
 * Generate key which contains a pair of private and public key using 1024
 * bytes. Store the set of keys in Prvate.key and Public.key files.
 * 
 * @throws NoSuchAlgorithmException
 * @throws IOException
 * @throws FileNotFoundException
 */
public static void generateKey() {
    try {
        final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(Constants.ALGORITHM);
        keyGen.initialize(1024);
        final KeyPair key = keyGen.generateKeyPair();

        File privateKeyFile = new File(Constants.PRIVATE_KEY_FILE);
        File publicKeyFile = new File(Constants.PUBLIC_KEY_FILE);

        //create the public and private key files
        createPUBPRIVFiles(privateKeyFile,publicKeyFile);

        // Saving the Public key in a file
        saveKey(publicKeyFile,key,"public");
        // Saving the Private key in a file
        saveKey(privateKeyFile,key,"private");

    } catch (Exception e) {
        e.printStackTrace();
    }

}



/** saves the keys in proper files
 * @param keyFile
 * @param key
 * @param pub_priv_flag
 */
private static void saveKey(File keyFile, KeyPair key, String pub_priv_flag) {
    try {
        ObjectOutputStream keyOS = new ObjectOutputStream(new FileOutputStream(keyFile));
        if(pub_priv_flag.equalsIgnoreCase("public")){
            keyOS.writeObject(key.getPublic());
        } else if(pub_priv_flag.equalsIgnoreCase("private")){
            keyOS.writeObject(key.getPrivate());
        }
        keyOS.close();
    }catch (Exception e) {
        e.printStackTrace();
    }
}



/**creates the public and private key files
 * @param privateKeyFile
 * @param publicKeyFile
 */
private static void createPUBPRIVFiles(File privateKeyFile, File publicKeyFile) {
    try {
        if (privateKeyFile.getParentFile() != null)
            privateKeyFile.getParentFile().mkdirs();
        privateKeyFile.createNewFile();
        if (publicKeyFile.getParentFile() != null)
            publicKeyFile.getParentFile().mkdirs();
        publicKeyFile.createNewFile();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

/**
 * The method checks if the pair of public and private key has been generated.
 * 
 * @return flag indicating if the pair of keys were generated.
 */
public static boolean areKeysPresent() {
    File privateKey = new File(Constants.PRIVATE_KEY_FILE);
    File publicKey = new File(Constants.PUBLIC_KEY_FILE);
    if (privateKey.exists() && publicKey.exists()) {
        return true;
    }
    return false;
}

/**
 * Encrypt the plain text using public key.
 * 
 * @param text
 * @param key
 */
public static byte[] encrypt(String text) {
    byte[] cipherText = null;
    try {

        ObjectInputStream inputStream = null;

        // Encrypt the string using the public key
        inputStream = new ObjectInputStream(new FileInputStream(Constants.PUBLIC_KEY_FILE));
        final PublicKey key = (PublicKey) inputStream.readObject();


        // get an RSA cipher object and print the provider
        final Cipher cipher = Cipher.getInstance(Constants.ALGORITHM);


        // encrypt the plain text using the public key
        cipher.init(Cipher.ENCRYPT_MODE, key);
        cipherText = cipher.doFinal(text.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cipherText;
}

/**
 * Decrypt text using private key.
 * @param text
 * @param key
 */
public static String decrypt(byte[] text) {
    byte[] dectyptedText = null;
    try {
        System.out.println("loading private key : "+Constants.PRIVATE_KEY_FILE);
        ObjectInputStream inputStream = null;
        inputStream = new ObjectInputStream(new FileInputStream(Constants.PRIVATE_KEY_FILE));
        final PrivateKey key = (PrivateKey) inputStream.readObject();
        final Cipher cipher = Cipher.getInstance(Constants.ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        dectyptedText = cipher.doFinal(text);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return new String(dectyptedText);
}


}

当我在AsymetricKeyCryptograph类中运行main方法时,我得到了一个正确的结果,但是当我通过发送加密字符串调用Web服务时,它会抛出一个异常:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:337)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
at javax.crypto.Cipher.doFinal(Cipher.java:1922)
at AsymetricKeyCryptography.decrypt(AsymetricKeyCryptography.java:161)
at SecureService.getRsaDecryptedData(SecureService.java:34)
at SecureService.getMsg(SecureService.java:29)

我不明白我哪里出错了

共有1个答案

皇甫乐
2023-03-14

我怀疑当你通过StringWriter将数据导入并在Jersey中将其作为字符串读回时,数据会被破坏。它应该始终保持字节数组。尝试通过ByteArrayOutputStream写入它,并将其作为字节[]或InputStream绑定到Jersey方法

 类似资料:
  • 我使用DynamoDB Encryption Client(Item Encryptor)(Lib Link)加密记录。现在我想解密它。我已经阅读了文档和GitHub页面,没有示例,所以很难弄清楚我弄错了哪部分。 这是DynamoDB事件的“新图像”。关键是“ID”。DynamoDB流记录 我尝试过: 使用decrypt\u dynamodb\u项- 使用ddb\u to\u dict,然后使用d

  • 我现在是弄一个小程序,用户会添加非常敏感的安全信息,目前是用户端添加敏感数据时使用他的union_id和固定的key作为加解密密钥,然后再AES加密后再提交到服务端,然后下次请求服务器返回加密数据后解密再使用。 这里存在一个问题,如果有黑客拿到了我的数据库,和我的算法,然后就可以解密出用户的敏感数据。因为用户union_id是会记录到服务端数据库,客户端固定的key也是可以扒到。 有什么方案可以做

  • 问题内容: 我正在开发客户端/服务器身份验证程序,但是遇到了问题。客户端可以很好地与服务器建立连接,但是一旦我输入密码和用户名,它就不会返回有效的用户名/密码。如果用户使用正确的用户名/密码服务器登录,则应返回“ Welcome,username”,如果无效,则返回“登录失败”。我查看了printwriter和bufferedreader文档,以确保我使用正确的方法在服务器/客户端之间正确传递文本

  • 我需要用RSA-2048服务器公钥加密客户端私钥。我知道私钥明显比公钥长,我不确定是否可能。。。但我看到类似的任务是用Python完成的,所以我想知道您的看法。 crypto/rsa致命错误:对于rsa公钥大小,消息太长

  • 我必须用Twofish/CBC算法在Delphi中加密字符串,将其发送到服务器并在那里解密。我已经测试了下面的代码,B64编码/解码过程有效,但是我被困在密码加密/解密上。 我正在使用DEC 5.2用于德尔福。 以下是进行加密的德尔菲代码: 和应该解密发送数据的PHP函数: 我相信我必须对salt和初始化向量做更多的改动,但是我不知道怎么做。据我所知,KDFx()函数使SHA1从用户密码和salt

  • 我正在尝试使用在我的设备上生成的RSA密钥解密我的android应用程序中的字符串。加密由php服务完成,使用我的应用程序提供的公共rsa密钥。我的问题是解密,解密失败了。 我正在做以下工作: 在用base64.encode(pubkey.getencoded())和私钥进行“base64”编码后,这两个密钥(公共和私有)都保存到文件中。->好 当我调用我的webservice时,我在post变量