“尝试在椭圆曲线加密中加密时,格式错误:需要RAW字节”

时间:2013-03-22 05:54:21

标签: java cryptography jce elliptic-curve

我正在尝试使用此代码(请参阅下面的代码),我从net获取Elliptic曲线地穴。

它得到了这个错误。我在输出中打印了一些额外的细节。

java.security.InvalidKeyException:格式错误:需要RAW字节

我试图谷歌寻求解决方案来生成原始格式的密钥。还搜索了这个例外。但是无法为此找到解决方案。请让我知道如何解决这个问题。

输出

SunEC
EC
provider=SunJCE version 1.7
X.509 public key Sun EC public key, 163 bits
  public x coord: 7394977086669700979165868162456208526218668043895
  public y coord: 2664471862515380615682451494306474660059347182278
  parameters: sect163r2 [NIST B-163] (1.3.132.0.15)
private key Sun EC private key, 163 bits
  private value:  2968492174805105628259044284910042424560235657309
  parameters: sect163r2 [NIST B-163] (1.3.132.0.15)
SunJCE version 1.7
java.security.InvalidKeyException: Wrong format: RAW bytes needed
    at com.sun.crypto.provider.CipherCore.getKeyBytes(CipherCore.java:499)
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:422)
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:386)
    at com.sun.crypto.provider.DESCipher.engineInit(DESCipher.java:186)
    at javax.crypto.Cipher.init(Cipher.java:1210)
    at javax.crypto.Cipher.init(Cipher.java:1153)
    at core.elliptic.TestECC.main(TestECC.java:43)

CODE

package com.elliptic;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;


public class TestECC {

    public static void main(String args[]) {
        try {
            Provider p[] = Security.getProviders();
            Provider p1 = Security.getProvider("SunEC");
            System.out.println(p1.getName());
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", "SunEC");
            System.out.println(kpg.getAlgorithm());

            Cipher cipher = Cipher.getInstance("DES");
            System.out.println("provider=" + cipher.getProvider());

            ECGenParameterSpec ecsp = new ECGenParameterSpec("sect163r2");

            kpg.initialize(ecsp);
            KeyPair kyp = kpg.genKeyPair();
            PublicKey pubKey = kyp.getPublic();
            System.out.println(pubKey.getFormat() + " public key " + pubKey);

            PrivateKey privKey = kyp.getPrivate();
            System.out.println("private key " + privKey);

            System.out.println(cipher.getProvider());

            cipher.init(Cipher.ENCRYPT_MODE, pubKey);

            String cleartextFile = "cleartext.txt";
            String ciphertextFile = "ciphertextECIES.txt";

            byte[] block = new byte[64];
            FileInputStream fis = new FileInputStream(cleartextFile);
            FileOutputStream fos = new FileOutputStream(ciphertextFile);
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);

            int i;
            while ((i = fis.read(block)) != -1) {
                cos.write(block, 0, i);
            }
            cos.close();

            // Decrypt

            String cleartextAgainFile = "cleartextAgainECIES.txt";

            cipher.init(Cipher.DECRYPT_MODE, privKey, ecsp);

            fis = new FileInputStream(ciphertextFile);
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            fos = new FileOutputStream(cleartextAgainFile);

            while ((i = cis.read(block)) != -1) {
                fos.write(block, 0, i);
            }
            fos.close();

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

}

0 个答案:

没有答案