在Java中使用ECC Curve25519加密/解密数据

25
我正在尝试在我的Android应用程序中使用Curve25519来本地加密/解密AES加密密钥。我不需要进行任何密钥交换、密钥协商或签名。为什么我需要使用特定的曲线?因为我需要能够自己提供私钥并能够计算其匹配的公钥。据我所知,只有Curve25519才能做到这一点。如果我错了,请指出。

所有Curve25519实现都只进行密钥生成、密钥交换和签名/验证。

是否可以在获得Curve25519私/公钥后进行数据加密/解密,或者您可以建议任何符合我的标准的备选曲线吗?

编辑

那么我为什么需要这个?我将更仔细地解释。我的应用程序正在进行本地文件加密,特别是照片。我的目标是让用户能够拍摄照片而无需输入密码,然后输入密码以查看它们。为此,我需要能够从密码创建公共/私人密钥对,并能够在提供相同密码时动态地重新创建相同的密钥对。因此,在第一次运行时,我从密码生成ECC密钥对并将公钥存储在设备上。当用户想要拍摄新照片时,应用程序使用随机256位AES密钥加密照片,然后使用存储的公钥加密该密钥。当用户想要查看照片时,他/她提供正确的密码,我派生相同的ECC密钥对并使用我的私钥解密AES密钥,然后我可以使用AES 256解密照片。

据我所知,只有Curve25519能够给我这种能力,或者还有其他替代品。Java代码示例也欢迎提供!


你可以使用这个来实例化ECIES。 - Artjom B.
使用接收者的公钥进行密钥交换,并使用共享密钥进行对称加密,例如XSalsa20Poly1305或AES-GCM。 - CodesInChaos
可能你没有理解。我没有任何接收方,所有的加密和解密都是本地进行的。 - Alex Amiryan
2
@AlexAmiryan 即使它们在同一系统上,你仍然有两个参与方。用户是接收者,加密文件的对象是发送者。如果只有一个参与方,则使用非对称加密是毫无意义的。 - CodesInChaos
1
@AlexAmiryan 你可以使用一次性发送者密钥来加密NaCl的盒子。这几乎与ECIES相同。NaCl的加密有几种实现方式,包括NaCl本身、TweetNaCl、LibSodium或我的C#端口。只需创建一个新密钥,计算与接收方的共享密钥,从共享密钥中派生出您的AES密钥(使用KDF或哈希)。将发送者的公钥与密文一起发送,以便接收方可以计算出相同的共享密钥。 - CodesInChaos
显示剩余7条评论
1个回答

3
在Android设备上加密文件的关键在于不要存储密钥。为此,您已经添加了一个限制条件,即加密图片不需要密码。最后,由于非对称加密很慢,您需要使用AES来完成重量级工作。
这适用于RSA或任何其他非对称算法。
初始运行
1. 第一次运行时,生成密钥对并要求用户输入密码。 2. 将公钥以明文形式存储。使用从密码生成的AES密钥加密私钥。
加密
5. 为每个图片生成一个不同的AES密钥,并使用它来加密图像。使用公钥加密AES密钥,然后将其与图片一起存储。
解密
6. 要求用户输入密码。从中生成第一个AES密钥。使用它来解密私钥。使用私钥解密此图像的AES密钥。使用AES密钥解密文件并显示它。
(实际上我们有4个密钥。我们需要密钥对才能允许我们无需密码进行加密。我们需要第一个AES密钥来安全地存储私钥。我们需要第二个和可变的AES密钥来加密文件。)
我认为这应该是安全的,除了诸如键盘记录之类的攻击。Java代码应该非常简单明了。希望这很清楚。
完整示例使用AES和RSA。对于Curve,请仅更改RSA代码,尽管它不支持开箱即用,因此您需要外部库。此外,为了节省时间和简洁起见,代码比应该更安全。例如,我使用了ECB而不是CBC。
package il.co.falk;

import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;

public class SecureFile {
private PublicKey publicKey;
private byte[] privateKeyArray;
private byte[] salt = {1,2,3,4,5,6,7,8};


public static void main(String[] args) {
    String password = "PASSWORD";
    SecureFile secureFile = new SecureFile(password);
    secureFile.test();
}


public void test() {
    String password = "PASSWORD";
    String imageFile = "348756348975634897562398479623896";

    ImageAndKey imageAndKey = encryptImage(imageFile.getBytes());
    byte[] decryptedImage = decryptImage(imageAndKey, password);

    System.out.println(new String(imageFile));
    System.out.println(new String(decryptedImage));
}

public SecureFile(String password) {
    try {
        generateRSAKeys(password);
    } catch (Exception e) {
        e.printStackTrace();
    }
}



public ImageAndKey encryptImage(byte[] imageBytes) {
    try {
        byte[] secretKeyBytes = generateAESKey();
        byte[] encryptedFile = aesEncrypt(imageBytes, secretKeyBytes);
        byte[] encryptedKey = rsaEncrypt(secretKeyBytes);

        return new ImageAndKey(encryptedFile, encryptedKey);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

public byte[] decryptImage(ImageAndKey imageAndKey, String password) {
    try {
        byte[] secretKeyBytes = generateAESKey(password);
        byte[] decryptedPrivateKey = aesDecrypt(privateKeyArray, secretKeyBytes);
        byte[] decryptedKey = rsaDecrypt(imageAndKey.aesKey, decryptedPrivateKey);

        SecretKey secretKey = new SecretKeySpec(decryptedKey, "AES");
        secretKeyBytes = secretKey.getEncoded();

        byte[] decryptedBytes = aesDecrypt(imageAndKey.imageBytes, secretKeyBytes);

        return  decryptedBytes;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}



// RSA
private void generateRSAKeys(String password) throws Exception {
    final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(512); // TODO: make this 2048 at least
    final KeyPair keyPair = keyGen.generateKeyPair();
    publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();

    byte[] secretKeyBytes = generateAESKey(password);
    byte[] privateKeyBytes = privateKey.getEncoded();
    privateKeyArray = aesEncrypt(privateKeyBytes, secretKeyBytes);
}

public byte[] rsaEncrypt(byte[] plainText) throws Exception {
    final Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] cipherText = cipher.doFinal(plainText);
    return cipherText;
}

public byte[] rsaDecrypt(byte[] cipherText, byte[] decryptedPrivateKeyArray) throws Exception {
    PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decryptedPrivateKeyArray));

    final Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    byte[]  plainText = cipher.doFinal(cipherText);
    return plainText;
}

// AES
private byte[] aesEncrypt(byte[] plainText, byte[] secretKeyBytes) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(secretKeyBytes));
    byte[] cipherText = cipher.doFinal(plainText);
    return cipherText;
}

public byte[] aesDecrypt(byte[] cipherText, byte[] secretKeyBytes) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, getSecretKey(secretKeyBytes));
    byte[] plainText = cipher.doFinal(cipherText);
    return plainText;
}

private byte[] generateAESKey() throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(256);
    SecretKey secretKey = keyGen.generateKey();
    return secretKey.getEncoded();
}

private byte[] generateAESKey(String password) throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    return secret.getEncoded();
}

private SecretKey getSecretKey(byte[] secretKeyBytes) throws Exception {
    SecretKey secretKey = new SecretKeySpec(secretKeyBytes, "AES");
    return secretKey;
}



// Classes
class ImageAndKey {
    public byte[] imageBytes;
    public byte[] aesKey;

    public ImageAndKey(byte[] imageBytes, byte[] aesKey) {
        this.imageBytes = imageBytes;
        this.aesKey = aesKey;
    }
}

}


1
你之前描述得很清楚,我会按照那个方向去做。我需要的是代码片段,一个使用Curve25519 ECC进行密钥生成和数据加密/解密的示例。 - Alex Amiryan
有必要使用曲线吗?它不是标准的Android加密算法,而AES256和RSA则是。 - Roy Falk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接