冰梦无痕 2017-08-06 08:16 采纳率: 0%
浏览 784

关于RSA加密BUG问题懂这个的大神帮我看下谢谢

 /**
     * 加密。注意密钥是可以获取到它适用的算法的。
     * 
     * @param plainText
     * @param privateKey
     * @return
     */
    public static byte[] encode(byte[] plainFile, PrivateKey privateKey) {
         Cipher cipher;
        try {
            cipher = Cipher.getInstance(privateKey.getAlgorithm());
             cipher.init(2, privateKey);
                int inputLen = plainFile.length;
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int offSet = 0;

                for(int i = 0; inputLen - offSet > 0; offSet = i * 128) {
                    byte[] cache;
                    if(inputLen - offSet > 256) {
                        cache = cipher.doFinal(plainFile, offSet, 128);
                    } else {
                        cache = cipher.doFinal(plainFile, offSet, inputLen - offSet);
                    }

                    out.write(cache, 0, cache.length);
                    ++i;
                }

                byte[] decryptedData = out.toByteArray();
                out.close();
                return decryptedData;
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

         return null;
    }
    /**
     * 解密
     * 
     * @param encodedText
     * @param publicKey
     * @return
     */
    public static byte[] decode(byte[] encodedText, PublicKey publicKey) {
         Cipher cipher;
        try {
            cipher = Cipher.getInstance(publicKey.getAlgorithm());
             cipher.init(1, publicKey);
                int inputLen = encodedText.length;
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int offSet = 0;

                for(int i = 0; inputLen - offSet > 0; offSet = i * 244) {
                    byte[] cache;
                    if(inputLen - offSet > 244) {
                        cache = cipher.doFinal(encodedText, offSet, 244);
                    } else {
                        cache = cipher.doFinal(encodedText, offSet, inputLen - offSet);
                    }

                    out.write(cache, 0, cache.length);
                    ++i;
                }

                byte[] encryptedData = out.toByteArray();
                out.close();
                return encryptedData;
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
 import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.X509Certificate;

import com.insigma.siis.local.utils.ServerCertificateUtil;

public class Test {

    private static final String CERT_PATH = "C:\\Users\\handong\\Downloads\\workspace\\myCer.cer";

    // JDK6只支持X.509标准的证书
    private static final String CERT_TYPE = "X.509";

    public static void main(String[] args) throws IOException {
        // 接收证书
        X509Certificate receivedCertificate = null;

        // 获取公钥
        PublicKey publicKey = null;
        receivedCertificate = ClientCertificateUtil.getCertificateByCertPath(CERT_PATH, CERT_TYPE);

        publicKey = ClientCertificateUtil.getPublicKey(receivedCertificate);

        // 获取密钥库
        KeyStore keyStore = ServerCertificateUtil.getKeyStore("Changeme123",
                "C:\\Users\\handong\\Downloads\\workspace\\myKeystore.keystore");
        // 获取私钥
        PrivateKey privateKey = ServerCertificateUtil.getPrivateKey(keyStore, "myCertificate", "Changeme123");

        byte[] as = { 0x12 };

        byte[] sdf = ServerCertificateUtil.encode(as, privateKey);
        byte[] fd = ClientCertificateUtil.decode(sdf, publicKey);
        for (int i = 0; i < fd.length; i++) {
            System.out.println(fd[1]);
        }

    }

图片说明
不知道问题出在哪了,我用的RSA

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2018-06-19 15:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接