public String encodeEncryptionMethod(EncryptionMethod encryptionMethod) { StringBuffer s = new StringBuffer(""); switch (encryptionMethod.getEncryptionMethod()) { case EncryptionMethod.BASE64: s.append("base64:").append(encryptionMethod.getEncryptionKey()); break; case EncryptionMethod.CLEAR: s.append("clear:").append(encryptionMethod.getEncryptionKey()); break; case EncryptionMethod.URI: s.append("uri:").append(encryptionMethod.getEncryptionKey()); break; } return s.toString(); }
/** * Decode EncryptionMethod object from given text. * * @param text * the text value of the EncryptionMethod. * @return EncryptionMethod object. */ public EncryptionMethod decodeEncryptionMethod(String value) throws ParseException { // encryptiondata =("clear"":"encryptionKey ) // /("base64"":"encodedEncryptionKey ) // /("uri"":"URItoObtainKey ) // /("prompt");defined in SDP,not usable in MGCP! int pos = value.indexOf(':'); if (pos < 0) { throw new ParseException("Invalid value for EncryptionData: " + value, 0); } String method = value.substring(0, pos).trim(); String key = value.substring(pos + 1).trim(); if (method.equalsIgnoreCase("clear")) { return new EncryptionMethod(EncryptionMethod.CLEAR, key); } else if (method.equalsIgnoreCase("base64")) { return new EncryptionMethod(EncryptionMethod.BASE64, key); } else if (method.equalsIgnoreCase("uri")) { return new EncryptionMethod(EncryptionMethod.URI, key); } else { throw new ParseException("Invalid value for EncryptionData: " + method, 0); } }
public static EncryptionMethod decode(byte[] value,int offset,int length) throws ParseException { if(length>=URI_VALUE.length && value[offset + URI_VALUE.length-1]==StringFunctions.COLON_BYTE) { for(int i=0;i<URI_VALUE.length-1;i++) if(value[offset+i]!=URI_VALUE[i] && value[offset+i]!=(byte)(URI_VALUE[i]+StringFunctions.CASE_STEP)) throw new ParseException("Invalid value for EncryptionData: " + new String(value,offset,length), 0); return new EncryptionMethod(EncryptionMethod.URI, new String(value,offset+URI_VALUE.length,length-URI_VALUE.length)); } else if(length>=CLEAR_VALUE.length && value[offset + CLEAR_VALUE.length-1]==StringFunctions.COLON_BYTE) { for(int i=0;i<CLEAR_VALUE.length-1;i++) if(value[offset+i]!=CLEAR_VALUE[i] && value[offset+i]!=(byte)(CLEAR_VALUE[i]+StringFunctions.CASE_STEP)) throw new ParseException("Invalid value for EncryptionData: " + new String(value,offset,length), 0); return new EncryptionMethod(EncryptionMethod.CLEAR, new String(value,offset+CLEAR_VALUE.length,length-CLEAR_VALUE.length)); } else if(length>=BASE64_VALUE.length && value[offset + BASE64_VALUE.length-1]==StringFunctions.COLON_BYTE) { for(int i=0;i<BASE64_VALUE.length-1;i++) if(value[offset+i]!=BASE64_VALUE[i] && value[offset+i]!=(byte)(BASE64_VALUE[i]+StringFunctions.CASE_STEP)) throw new ParseException("Invalid value for EncryptionData: " + new String(value,offset,length), 0); return new EncryptionMethod(EncryptionMethod.BASE64, new String(value,offset+BASE64_VALUE.length,length-BASE64_VALUE.length)); } throw new ParseException("Invalid value for EncryptionData: " + new String(value,offset,length), 0); }
public static int encode(byte[] destination,int offset,EncryptionMethod encryptionMethod) { int usedLength=0; byte[] key=encryptionMethod.getEncryptionKey().getBytes(); switch (encryptionMethod.getEncryptionMethod()) { case EncryptionMethod.BASE64: System.arraycopy(BASE64_VALUE, 0, destination, offset, BASE64_VALUE.length); usedLength=BASE64_VALUE.length; System.arraycopy(key, 0, destination, offset+usedLength, key.length); usedLength+=key.length; break; case EncryptionMethod.CLEAR: System.arraycopy(CLEAR_VALUE, 0, destination, offset, CLEAR_VALUE.length); usedLength=CLEAR_VALUE.length; System.arraycopy(key, 0, destination, offset+usedLength, key.length); usedLength+=key.length; break; case EncryptionMethod.URI: System.arraycopy(URI_VALUE, 0, destination, offset, URI_VALUE.length); usedLength=URI_VALUE.length; System.arraycopy(key, 0, destination, offset+usedLength, key.length); usedLength+=key.length; break; } return usedLength; } }
public static EncryptionMethod decode(byte[] value,int offset,int length) throws ParseException { if(length>=URI_VALUE.length && value[offset + URI_VALUE.length-1]==StringFunctions.COLON_BYTE) { for(int i=0;i<URI_VALUE.length-1;i++) if(value[offset+i]!=URI_VALUE[i] && value[offset+i]!=(byte)(URI_VALUE[i]+StringFunctions.CASE_STEP)) throw new ParseException("Invalid value for EncryptionData: " + new String(value,offset,length), 0); return new EncryptionMethod(EncryptionMethod.URI, new String(value,offset+URI_VALUE.length,length-URI_VALUE.length)); } else if(length>=CLEAR_VALUE.length && value[offset + CLEAR_VALUE.length-1]==StringFunctions.COLON_BYTE) { for(int i=0;i<CLEAR_VALUE.length-1;i++) if(value[offset+i]!=CLEAR_VALUE[i] && value[offset+i]!=(byte)(CLEAR_VALUE[i]+StringFunctions.CASE_STEP)) throw new ParseException("Invalid value for EncryptionData: " + new String(value,offset,length), 0); return new EncryptionMethod(EncryptionMethod.CLEAR, new String(value,offset+CLEAR_VALUE.length,length-CLEAR_VALUE.length)); } else if(length>=BASE64_VALUE.length && value[offset + BASE64_VALUE.length-1]==StringFunctions.COLON_BYTE) { for(int i=0;i<BASE64_VALUE.length-1;i++) if(value[offset+i]!=BASE64_VALUE[i] && value[offset+i]!=(byte)(BASE64_VALUE[i]+StringFunctions.CASE_STEP)) throw new ParseException("Invalid value for EncryptionData: " + new String(value,offset,length), 0); return new EncryptionMethod(EncryptionMethod.BASE64, new String(value,offset+BASE64_VALUE.length,length-BASE64_VALUE.length)); } throw new ParseException("Invalid value for EncryptionData: " + new String(value,offset,length), 0); }
public static int encode(byte[] destination,int offset,EncryptionMethod encryptionMethod) { int usedLength=0; byte[] key=encryptionMethod.getEncryptionKey().getBytes(); switch (encryptionMethod.getEncryptionMethod()) { case EncryptionMethod.BASE64: System.arraycopy(BASE64_VALUE, 0, destination, offset, BASE64_VALUE.length); usedLength=BASE64_VALUE.length; System.arraycopy(key, 0, destination, offset+usedLength, key.length); usedLength+=key.length; break; case EncryptionMethod.CLEAR: System.arraycopy(CLEAR_VALUE, 0, destination, offset, CLEAR_VALUE.length); usedLength=CLEAR_VALUE.length; System.arraycopy(key, 0, destination, offset+usedLength, key.length); usedLength+=key.length; break; case EncryptionMethod.URI: System.arraycopy(URI_VALUE, 0, destination, offset, URI_VALUE.length); usedLength=URI_VALUE.length; System.arraycopy(key, 0, destination, offset+usedLength, key.length); usedLength+=key.length; break; } return usedLength; } }