实施充气城堡aes 256 [英] implementing bouncy castle aes 256

查看:170
本文介绍了实施充气城堡aes 256的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发聊天应用程序。主要功能是以加密形式发送消息,当它们到达目的地时,它们可以被解密。我遇到的问题是邮件在目的地没有被解密,但是它们以加密形式到达目的地。

I am working on a chat application. The main feature is to send messages in encrypted form and when they reach their destination they can be decrypted. The problem I am having is that the messages are not getting decrypted at their destination but however they reach their destination in encrypted form.

代码如何工作:


  1. 客户A向客户B发送消息Hello...

  2. 当客户A点击时按钮发送消息我将该文本保存在字符串中,然后将该字符串与密钥和iv一起传递给方法加密,如此...

  1. Client A sends message "Hello" to client B...
  2. When Client A clicks on button "Send message" I save that text in a String and then that String is passed along with key and iv to the method Encrypt like this...

en=enc.encrypt(msg.getBytes(), key.getBytes(), iv.getBytes());

我将该字节( en )转换为字符串并将其发送给另一个客户端B.

I convert that byte (en) into string and sends it to the other client B.

当我打开另一个接收消息的类时,我得到字符串(en)然后再转换它转换为传递给方法Decrypt的字节。但每当我运行该项目时,它都无法运行。试图在尝试捕获中做到这一点,但也没有工作。也许是因为它已经在一个很大的尝试捕获声明已经使它更令人困惑。

When I open the other class where I receive the message I get the string (en) and then again converts it into bytes which is passed to the method Decrypt. But whenever I run the project it doesn't work. Tried to do that in try catch but didn't worked either. Maybe because it is already in a big try catch statement already which makes it even more confusing.

我的代码:

package com.socket;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;

public class Encrypt {

  public Encrypt() {
  }

  public static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data)
      throws Exception {
    int minSize = cipher.getOutputSize(data.length);

    byte[] outBuf = new byte[minSize];

    int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);

    int length2 = cipher.doFinal(outBuf, length1);

    int actualLength = length1 + length2;

    byte[] result = new byte[actualLength];

    System.arraycopy(outBuf, 0, result, 0, result.length);

    return result;
  }

  public static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv)
      throws Exception {

    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(
        new CBCBlockCipher(new AESEngine()));

    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);

    aes.init(false, ivAndKey);

    return cipherData(aes, cipher);
  }

  public byte[] encrypt(byte[] plain, byte[] key, byte[] iv) throws Exception {

    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(
        new CBCBlockCipher(

        new AESEngine()));

    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);

    aes.init(true, ivAndKey);

    return cipherData(aes, plain);
  }
}


推荐答案

你正在定期使用 String.getBytes()。这几乎可以确定您出错的位置。 getBytes()与平台有关,因此您可能在不同的系统上获得不同的字节。此外,并非所有字节都是有效的字符编码。因此,如果您的密钥/ IV包含安全的随机字节(它们应该),那么您的解密将失败...有时候。

You are using String.getBytes() on a regular basis. That's almost certain one location where you make an error. getBytes() is platform dependent, so you may get different bytes on different systems. Furthermore, not all bytes are valid character encodings. So if your key/IV consists of secure random bytes (which they should) then your decryption will fail...sometimes.

一般的答案是将字符转换为字节使用良好指定的字符编码(例如UTF-8)和字节使用像base-64或hexadecimals这样的编码。

The general answer is to convert characters to bytes using a well specified character-encoding (e.g. UTF-8) and bytes to characters using an encoding like base-64 or hexadecimals.

这篇关于实施充气城堡aes 256的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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