A few years ago, i tried to do a mobile sms cryption applicatio with bouncycastle.
public byte[] smsEncodedMessage ( byte[] smsEncContent , byte[] smsEncPKey ) throws Exception {
try {
cipherEngine = new PaddedBufferedBlockCipher ( createEngine () );
cipherEngine.init ( true , new KeyParameter ( smsEncPKey ) );
cipherText = new byte[ cipherEngine.getOutputSize ( smsEncContent.length ) ];
cipherTextLength = cipherEngine.processBytes ( smsEncContent , 0 , smsEncContent.length , cipherText , 0 );
cipherEngine.doFinal ( cipherText , cipherTextLength );
out = new ByteArrayOutputStream ();
dout = new DataOutputStream ( out );
dout.writeShort ( cipherText.length );
out.write ( cipherText );
} catch ( Exception e ) {
main.errorMessage ( "<ERROR BLOCK - inLine < 1 > >: " + e.getMessage () );
}
return out.toByteArray ();
}
public String smsDecodedMessage ( byte smsDecContent[] , byte[] smsDecPKey ) throws Exception {
try {
in = new ByteArrayInputStream ( smsDecContent );
din = new DataInputStream ( in );
cipherTextLength = din.readShort ();
cipherText = new byte[ cipherTextLength ];
in.read ( cipherText , 0 , cipherTextLength );
cipherEngine = new PaddedBufferedBlockCipher ( createEngine () );
cipherEngine.init ( false , new KeyParameter ( smsDecPKey ) );
plainText = new byte[ cipherEngine.getOutputSize ( cipherTextLength ) ];
size = cipherEngine.processBytes ( cipherText , 0 , cipherTextLength , plainText , 0 );
cipherEngine.doFinal ( plainText , size );
resultText = ( new String ( plainText ) ).trim ();
} catch ( Exception e ) {
main.errorMessage ( "<ERROR BLOCK - inLine < 2 > >: " + e.getMessage () );
}
return resultText;
}
No comments:
Post a Comment