实现ECC导致错误

问题描述:

我想在java中实现ECC,但出现错误。实现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); 
     } 
    } 

} 
+0

最后一年的项目? – 2012-02-27 14:09:56

+0

@DaveNewton我怀疑它,4-5年后你应该了解包装。 – jn1kk 2012-02-27 14:20:38

它看起来像你想用这条命令:

java TestECC.TestECC 

其中ISN是班级名称。你应该运行这个:

java com.acc.TestECC 

...这是完全合格的类名称。请注意,这有什么与您的主要方法内的代码。你可以删除所有的代码,你仍然会得到相同的错误;它还没有运行你的任何代码。

您正在尝试运行的类

java TestECC.TestECC 

时是不正确的包名。相反,你想

java com.acc.TestECC 

确保正确设置你的类路径。 (或者使用一个为您设置的IDE)