如何在Java中使用Camellia 128位加密算法加密字符串?

如何在Java中使用Camellia 128位加密算法加密字符串?,java,encryption,block-cipher,128-bit,camellia,Java,Encryption,Block Cipher,128 Bit,Camellia,我目前正试图开发一个聊天应用程序,实现茶花128位加密算法。我成功地从it官方页面[此处][1]构建并运行了代码。问题是整数数据类型的加密算法。当我试图加密字符串消息时,我将其转换为ASCII格式,但它给了我 线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“10410110810811132119111114108100” 位于java.lang.NumberFormatException.forInputString(NumberFor

我目前正试图开发一个聊天应用程序,实现茶花128位加密算法。我成功地从it官方页面[此处][1]构建并运行了代码。问题是整数数据类型的加密算法。当我试图加密字符串消息时,我将其转换为ASCII格式,但它给了我

线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“10410110810811132119111114108100” 位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 位于java.lang.Integer.parseInt(Integer.java:583) 在java.lang.Integer.parseInt(Integer.java:615) 在Camellia.main(Camellia.java:54)

。我知道这是因为ASCII码太长。但是我真的被卡住了,我不知道如何使用字符串加密算法。下面是我的主要代码

 int[] key = {987777, 1, 2, 3}; // 128 bit key example
 int[] keyTable = new int[52];
 Camellia n=new Camellia();
 n.Camellia_Ekeygen(key, keyTable);

 String c = "";
 String str = "hello world";
 System.out.println(str);
 BigInteger bigInt;
 byte[] bytes;
     try{
        bytes = str.getBytes("US-ASCII");

        for(int i=0; i<str.length(); i++){
            System.out.print(bytes[i]+",");
            c += String.valueOf(bytes[i]);
        }
        System.out.println("");
        bigInt = new BigInteger(c);
        System.out.println(bigInt);
 }catch(Exception e){};

 int C = Integer.parseInt(c);

 int[] plaintext = {C, 5, 6, 7}; // 128 bit plaintext block example
 int[] ciphertext = new int[4];
 n.Camellia_EncryptBlock(plaintext, keyTable, ciphertext);
 n.Camellia_DecryptBlock(ciphertext, keyTable, plaintext);

正如@Topaco所评论的,检查加密算法的实现“只是为了好玩”需要做很多工作,因此下面您可以找到一个使用Bouncy Castle安全提供程序的完整加密/解密程序的工作示例

该程序使用128位=16字节长的密钥运行,并使用CBC模式-可以“降级”使用ECB模式,但不建议这样做,因为ECB模式不安全,不应再用于新的应用程序

它将字符串加密为字节数组并返回原始字符串:

Camellia 128 CBC encryption with Bouncy Castle
plaintext: The quick brown fox jumps over the lazy dog
plaintext  length: 43 data: 54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67
ciphertext length: 48 data: 56629f6df124b289c18da9baa80efd3bc12833c69916d2dea1bc58bad250f5a6f78bb440c086308426e1eb10e9d952bc
decry.text length: 43 data: 54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67
decryptedtext: The quick brown fox jumps over the lazy dog
安全警告:该程序没有异常处理,仅用于教育目的。

代码:


你想学习算法还是仅仅应用它?对于后者,它更易于使用,例如..@Topaco我想应用它。但实际上我想把它应用到颤振中。我尝试过使用Bouncy Castle,它与Java完美结合。但当我把它带到颤振中时,它给了我BC提供者不支持填充。仅供参考,我与Java和Dart建立了连接,因此我能够在Flatter中运行代码。但是它给了我一个错误,我用Java工具编译它时没有得到这个错误editor@Topaco更新:我可以使用AES提供程序进行加密,但我想使用Camellia,但它说提供程序不可用。我相信问题出在
Cipher desCipher=Cipher.getInstance(“AES”)我试着把它改成
Camellia/CBC/PKCS7Padding
,但它说在这篇文章中没有找到这样的提供者,你要求用Java实现。如果您现在正在寻找Dart的实现,您应该提出一个新问题,在这个问题中发布最新的Dart代码并描述问题。如有必要,您可以参考此问题。谢谢。@Topaco,对不起,我的错。实际上,我想在不使用任何库(如bouncy castle)的情况下研究该算法。你能告诉我如何使用camellia加密字符串吗?实际上我正在学习算法。我不允许使用任何像bouncy castle这样的图书馆。你能指导我至少用camellia算法加密一个字符串吗?@random peeps:很抱歉,我从来没有使用过camellia算法-尝试找到一个本地库,将结果与BC结果进行比较,并根据您的需要采用该算法。


  [1]: https://info.isl.ntt.co.jp/crypt/eng/camellia/dl/camellia-java-BSD-1.0.1.tar.gz
Camellia 128 CBC encryption with Bouncy Castle
plaintext: The quick brown fox jumps over the lazy dog
plaintext  length: 43 data: 54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67
ciphertext length: 48 data: 56629f6df124b289c18da9baa80efd3bc12833c69916d2dea1bc58bad250f5a6f78bb440c086308426e1eb10e9d952bc
decry.text length: 43 data: 54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67
decryptedtext: The quick brown fox jumps over the lazy dog
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;

public class MainSo {
    public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        System.out.println("Camellia 128 CBC encryption with Bouncy Castle");

        Security.addProvider(new BouncyCastleProvider());
        String plaintext = "The quick brown fox jumps over the lazy dog";
        byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8);

        // generate random key + iv
        SecureRandom secureRandom = new SecureRandom();
        byte[] key = new byte[16]; // 16 bytes = 128 bit key
        secureRandom.nextBytes(key);
        byte[] iv = new byte[16];
        secureRandom.nextBytes(iv);
        // setup cipher
        Cipher cipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding", "BC");
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        // encryption
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);
        byte[] ciphertext = cipher.doFinal(plaintextBytes);
        System.out.println("plaintext: " + plaintext);
        System.out.println("plaintext  length: " + plaintextBytes.length + " data: " + bytesToHex(plaintextBytes));
        System.out.println("ciphertext length: " + ciphertext.length + " data: " + bytesToHex(ciphertext));
        // decryption
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
        byte[] decryptedtext = cipher.doFinal(ciphertext);
        System.out.println("decry.text length: " + decryptedtext.length + " data: " + bytesToHex(decryptedtext));
        System.out.println("decryptedtext: " + new String(decryptedtext));
    }
    private static String bytesToHex(byte[] bytes) {
        StringBuffer result = new StringBuffer();
        for (byte b : bytes) result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        return result.toString();
    }
}