实施ECC会导致错误

时间:2012-02-27 14:05:06

标签: java dictionary netbeans-6.9

我正在尝试在java中实现ECC,但是我收到了错误。

这是错误:

 java.lang.ClassNotFoundException: TestECC.TestECC
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)..

以下是代码:

 package com.acc;

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.SecureRandom;
import java.security.Security;
import java.security.Signature;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec;
import java.security.spec.EllipticCurve;

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


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();

            PrivateKey privKey = kyp.getPrivate();
            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) {
            System.out.println(e);
        }
    }

}

2 个答案:

答案 0 :(得分:0)

看起来你正试图用以下方式运行:

java TestECC.TestECC

这不是班级名称。你应该运行这个:

java com.acc.TestECC

......这是完全合格的班级名称。请注意,这与主方法中的代码有 nothing 。您可以删除所有代码,但仍然会得到相同的错误;它尚未运行任何代码。

答案 1 :(得分:0)

您正在尝试运行该课程

java TestECC.TestECC

当它不是正确的包名称时。相反,你想要

java com.acc.TestECC

确保正确设置类路径。 (或使用为您设置此功能的IDE)