gpt4 book ai didi

blackberry - BouncyCaSTLe J2ME RSA 使用自定义 key

转载 作者:行者123 更新时间:2023-12-03 11:20:36 30 4
gpt4 key购买 nike

我想在我的黑莓应用程序中使用 BouncyCaSTLe J2ME/RIM Crypto。

我遇到的问题是我想从 C#.NET 程序生成用于加密的公钥,该程序将 key 发送到 BlackBerry。

是否可以使用原始字符串加密消息?另外,我是否需要知道其他常见变量,例如模数等?抱歉,我对密码算法完全陌生。

为此我需要 BouncyCaSTLe 还是可以使用 RIM Crypto 完成上述操作?

谢谢,康纳

最佳答案

我是使用 bouncycaSTLe 完成的,但与 RIM Crypto 类似。根据例子。如您所见,键是字符串...:

    public CypherDecypherExample()
{
String plain ="a plain string";
String cipher = null;
String decipher = null;
byte [] byte_cipher = null;
byte [] byte_plain = null;

// key |-- 128 bit -->|-- 256 bit --->|
String key = "aaaaaaaaaaaaaaaacccccccccccccccc";
String iv = "bbbbbbbbbbbbbbbb";

System.out.println("bouncycastle.plain: " + plain);

try {
byte_cipher = encrypt(plain.getBytes(), key.getBytes(), iv.getBytes());
cipher = new String(byte_cipher);
System.out.println("bouncycastle.cipher: " + cipher);

} catch (Exception e) {
e.printStackTrace();
}


try {
byte_plain = decrypt(byte_cipher, key.getBytes(), iv.getBytes());
decipher = new String(byte_plain);
System.out.println("bouncycastle.decipher: " + decipher);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data)
throws Exception
{
String plain = new String(data);
System.out.println("bouncycastle.cipherData: " + plain);

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

System.out.println("bouncycastle.cipherData returning");

return result;
}

private static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv) throws Exception
{
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher((BlockCipher) new CBCBlockCipher(
new AESEngine()));
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(false, ivAndKey);
return cipherData(aes, cipher);
}

private static 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);
}

关于blackberry - BouncyCaSTLe J2ME RSA 使用自定义 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9533768/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com