头上秀发渐稀 2021-07-03 16:37 采纳率: 0%
浏览 6

华为鸿蒙智能家居平台,激活码获取的一些问题

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;

public class RsaDec {

public static byte[] decrypt(byte[] byteDatas, String privateKeyStr) throws Exception {
    byte[] keyBytes = Base64.decodeBase64(privateKeyStr);
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int inputLen = byteDatas.length;
    int offSet = 0;
    byte[] cache;
    int i = 0;

    while (inputLen - offSet > 0) {
        if (inputLen - offSet > 128) {
            cache = cipher.doFinal(byteDatas, offSet, 128);
        } else {
            cache = cipher.doFinal(byteDatas, offSet, inputLen - offSet);
        }
        out.write(cache, 0, cache.length);
        i++;
        offSet = i * 128;
    }

    byte[] decryptedData = out.toByteArray();
    out.close();
    return decryptedData;
}

public static void main(String[] args) {
    File sourceFile = new File(args[0]);
    File targetFile = new File(args[1]);
    File privateKeyFile = new File(args[2]);

    try {
        String privateKey =
            FileUtils.readFileToString(privateKeyFile).replaceAll("\\s", "").replaceAll("---+[a-zA-Z]+---+", "");
        byte[] bytes = decrypt(FileUtils.readFileToByteArray(sourceFile), privateKey);
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        FileUtils.copyInputStreamToFile(in, targetFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

这段是华为官网给出了的解码RSA加密的JAVA代码,但是复制到Eclipse编译完成之后,运行出现以下错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at RsaDec.main(RsaDec.java:46)

希望有大佬知道是怎么解决的指点下,感谢感谢!

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-07 18:03
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度