2

내 블랙 베리 애플리케이션에 BouncyCastle J2ME/RIM Crypto를 사용하고 싶습니다.BouncyCastle 사용자 정의 키를 사용하는 J2ME RSA

내가 겪고있는 문제는 BlackBerry에 키를 보내는 C# .NET 프로그램에서 암호화를위한 공개 키를 생성하고 싶다는 것입니다.

원시 문자열을 사용하여 메시지를 암호화 할 수 있습니까? 또한, 모듈로 등과 같은 다른 일반적인 변수를 알아야합니까? 죄송하지만 저는 암호 알고리즘에 완전히 익숙하지 않습니다.

BouncyCastle이 필요합니까? 아니면 위의 사항을 RIM Crypto와 함께 할 수 있습니까?

덕분에, 코너

답변

3

RSA 공개 키는 생각했던 것과 같은 두 가지 구성 요소로 이루어져 있습니다.

ExponentModulus이 있습니다. 이 두 가지 모두 숫자지만 .NET 클라이언트에서 Base64 문자열로 Blackberry로 전달하고 Byte 배열을 매개 변수로 사용하므로 RIM Crypto 함수에서 사용할 때는 바이트 배열로 디코딩합니다. 거의 모든 그것도 사람들이 있어요

byte[] exponent = Base64InputStream.decode("exponent base64 string"); 
byte[] modulus = Base64InputStream.decode("modulus base64 string"); 

NoCopyByteArrayOutputStream cipherUserData = new NoCopyByteArrayOutputStream();    
RSACryptoSystem cryptoSystem = new RSACryptoSystem(1024); 

// Create Public key using your variables from before 
RSAPublicKey publicKey = new RSAPublicKey(cryptoSystem, exponent, modulus); 

// Encryption engine objects 
RSAEncryptorEngine eEngine = new RSAEncryptorEngine(publicKey); 
PKCS1FormatterEngine fEngine = new PKCS1FormatterEngine(eEngine); 
BlockEncryptor cryptoStream = new BlockEncryptor(fEngine, cipherUserData); 


// Read the user data and encrypt while doing so. Remember, cryptoStream writes its data to 
// cipherUserData so this is where the encrypted version of userData will end up. 
cryptoStream.write(userData, 0, userData.length); 

cryptoStream.close(); 
cipherUserData.close(); 

String encryptedUserData = new String(cipherUserData.toByteArray()); 

, 그것은 간단합니다하지만

중요 RSA 암호화 목적으로 제한되어 나에게 API 문서 :)에서이를 얻기 위해 오랜 시간이 걸렸습니다 즉, < = 키 크기 인 메시지 만 암호화 할 수 있습니다. 1024 비트 RSA의 경우 117 바이트이고 2048 RSA의 경우 245 바이트입니다. 더 큰 메시지를 암호화하려면 AES 등을 사용하여 메시지를 암호화 한 다음 AES 키를 RSA 공개 키로 암호화하십시오. AES 암호문을 해독하기 위해 AES 암호문과 키가 포함 된 RSA 암호문을 보냅니다.

내가 위에 쓴 것은 땜질과 독서의 날을 보냈다. 누군가가 목표보다 빨리 목표를 달성하는 데 도움이되기를 바랍니다. :)

3

내가 bouncycastle를 사용하여했지만 RIM 암호화와 유사하다. 예를 따르십시오. 당신이 볼 수 있듯이 키는 문자열입니다 ... :

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); 
} 
+0

감사합니다. 128Bit, 256Bit이 무슨 뜻이야? 또한 iv (Initialization Vector) 란 무엇입니까? 어떻게 이것을 생성합니까? 감사. – conor

+0

128 비트 키를 사용하려면 문자열을 128 비트 부호 (처음 16 문자)로 끝냅니다. 256 비트 키를 선호하는 경우 32 문자를 사용하십시오. iv는 다른 문자열입니다 (선택 사항). 저는 암호화 전문가가 아닙니다 ... – rosco

+1

키를 문자열로 사용해서는 안되며 인코딩을 지정하지 않고 getBytes를 사용하지 마십시오. –

관련 문제