ECC算法密钥不匹配 [英] ECC algorithm key mismatch

查看:1746
本文介绍了ECC算法密钥不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在Android ECC算法。我目前使用的海绵城堡来实现它。

I am trying to implement ECC algorithm on Android. I am currently using spongy castle to implement it.

密钥产生锥体段是如下:

The key generation cone snippet is as follows :

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

加密/解密code是如下:

The encrypt/decrypt code is as follows

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

下面,因为我使用(IESKey)铸造在 c.init()我的错误

Here since i use (IESKey) casting for private and public key in c.init() i get the error

java.lang.ClassCastException:org.spongycastle.jce.provider.JCEECPublicKey cannot be cast to org.spongycastle.jce.interfaces.IESKey

如果我删除投我得到这样的错误

and if i remove the cast i get error like this

c.init(Cipher.DECRYPT_MODE, privateKey, new SecureRandom());

我得到误差

java.security.InvalidKeyException: must be passed IES key

如果我用我得到的算法没有发现错误

I get algorithm not found error if i use

Cipher.getInstance("ECIESwithAES/DHAES/PKCS7Padding", "SC");

我使用

scprov-jdk15-1.46.99.3-非官方ROBERTO-RELEASE.jar 。这是使用正确的罐子?如果没有,请提出一个更好的。

I am using scprov-jdk15-1.46.99.3-UNOFFICIAL-ROBERTO-RELEASE.jar. Is this the right jar to use? If not please suggest a better one.

另外,我应该怎么纠正我的code得到它的工作?

Also how should I rectify my code to get it working?

推荐答案

我发现了一个解决方案...这里是一个code,它的工作原理

I found a solution... Here is a code that works

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

这篇关于ECC算法密钥不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆