Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ECC算法密钥不匹配_Android_Cryptography_Public Key Encryption_Spongycastle - Fatal编程技术网

Android ECC算法密钥不匹配

Android ECC算法密钥不匹配,android,cryptography,public-key-encryption,spongycastle,Android,Cryptography,Public Key Encryption,Spongycastle,我正在尝试在Android上实现ECC算法。我目前正在使用海绵城堡来实现它 密钥生成代码段如下所示: KeyPairGenerator kpg = null; try { kpg = KeyPairGenerator.getInstance("ECIES");// Do i have to do any changes here? } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } ECGenParame

我正在尝试在Android上实现ECC算法。我目前正在使用海绵城堡来实现它

密钥生成代码段如下所示:

KeyPairGenerator kpg = null;
try {
     kpg = KeyPairGenerator.getInstance("ECIES");// Do i have to do any changes here?
} catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
}
ECGenParameterSpec brainpoolP160R1 = new ECGenParameterSpec("brainpoolP160R1");

try {
    kpg.initialize(brainpoolP160R1);
} catch (InvalidAlgorithmParameterException) {

}

KeyPair kp = kpg.generateKeyPair();

PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
加密/解密代码如下所示

Cipher c = null;

try {
    c =Cipher.getInstance("ECIES", "SC"); //Cipher.getInstance("ECIESwithAES/DHAES/PKCS7Padding", "BC");
} catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException e) {
    e.printStackTrace();
}

try {
    c.init(Cipher.ENCRYPT_MODE,(IESKey)publicKey , new SecureRandom()); 
} catch (InvalidKeyException e) {
    e.printStackTrace();
}
byte[] message = "hello world -- a nice day today".getBytes();
byte[] cipher = new byte[0];
try {
    cipher = c.doFinal(message,0,message.length);
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
// System.out.println("Ciphertext : "+ Base64.encode(cipher));
TextView eccencoded = (TextView) findViewById(R.id.eccencoded);
eccencoded.setText("[ENCODED]:\n" +
Base64.encodeToString(cipher, Base64.DEFAULT) + "\n");

try {
    c.init(Cipher.DECRYPT_MODE,(IESKey) privateKey, new SecureRandom());
} catch (InvalidKeyException e) {
    e.printStackTrace();
}

byte[] plaintext = new byte[0];
try {
    plaintext = c.doFinal(cipher,0,cipher.length);
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
TextView eccdecoded = (TextView) findViewById(R.id.eccdecoded);
eccdecoded.setText("[DECODED]:\n" +
                Base64.encodeToString(plaintext, Base64.DEFAULT) + "\n");
在这里,由于我在c.init中对私钥和公钥使用IESKey强制转换,所以我得到了错误

java.lang.ClassCastException:org.spongycastle.jce.provider.JCEECPublicKey cannot be cast to org.spongycastle.jce.interfaces.IESKey
如果我去掉了石膏,我会得到这样的错误

c.init(Cipher.DECRYPT_MODE, privateKey, new SecureRandom());
我犯了一个错误

java.security.InvalidKeyException: must be passed IES key
如果我使用

Cipher.getInstance("ECIESwithAES/DHAES/PKCS7Padding", "SC");
我使用的是scprov-jdk15-1.46.99.3-nonofficial-ROBERTO-RELEASE.jar。这是要用的罐子吗?如果没有,请建议一个更好的


另外,我应该如何更正代码以使其正常工作?

我找到了一个解决方案。。。下面是一个有效的代码

Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);

ECGenParameterSpec brainpoolP160R1 = new ECGenParameterSpec("brainpoolP256t1");
KeyPairGenerator kpg = null;
try {
    kpg = (KeyPairGenerator) KeyPairGenerator.getInstance("ECIES", "SC");
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
    e.printStackTrace();
}

try {
    kpg.initialize(brainpoolP160R1, new SecureRandom());
} catch (InvalidAlgorithmParameterException e) {
    e.printStackTrace();
}

KeyPair akey = kpg.generateKeyPair();
KeyPair bkey = kpg.generateKeyPair();
// PublicKey publicKey = keyPair.getPublic();
//PrivateKey privateKey = keyPair.getPrivate();

byte[] d = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
byte[] e = new byte[]{8, 7, 6, 5, 4, 3, 2, 1};

IESParameterSpec param = new IESParameterSpec(d, e, 256);

Cipher c = null;

try {
    c = Cipher.getInstance("ECIES");
} catch (NoSuchAlgorithmException | NoSuchPaddingException f) {
    f.printStackTrace();
}

try {
    c.init(Cipher.ENCRYPT_MODE, new IEKeySpec(akey.getPrivate(), bkey.getPublic()), param);
    //c.init(Cipher.ENCRYPT_MODE, c1Key, param);
    //c.init(Cipher.ENCRYPT_MODE, publicKey, new SecureRandom());
    // How can i put the AES128_CBC for ies parameter ? is that possible
} catch (InvalidKeyException | InvalidAlgorithmParameterException f) {
    f.printStackTrace();
}
byte[] message = theTestText.getBytes();
byte[] cipher = new byte[0];
try {
    cipher = c.doFinal(message);//,0,message.length);
} catch (IllegalBlockSizeException | BadPaddingException f) {
    f.printStackTrace();
}

TextView eccencoded = (TextView) findViewById(R.id.eccencoded);
eccencoded.setText("[ENCODED]:\n" +
        new String(cipher) + "\n");

try {
    c.init(Cipher.DECRYPT_MODE, new IEKeySpec(bkey.getPrivate(), akey.getPublic()), param);
} catch (InvalidKeyException | InvalidAlgorithmParameterException f) {
    f.printStackTrace();
}

byte[] plaintext = new byte[0];
try {
    plaintext = c.doFinal(cipher);
} catch (IllegalBlockSizeException | BadPaddingException f) {
    f.printStackTrace();
}
TextView eccdecoded = (TextView) findViewById(R.id.eccdecoded);
eccdecoded.setText("[DECODED]:\n" +
        new String(plaintext) + "\n");

你能分享代码的链接或者你在哪里找到的代码吗。