Package javax.crypto

Examples of javax.crypto.Cipher


   }

   public InputStream getInputStream() throws IOException
   {
      InputStream is = delegate == null ? super.getInputStream() : delegate.getInputStream();
      Cipher cipher = null;
      try
      {
         cipher = Cipher.getInstance(algorithm);
         int size = cipher.getBlockSize();
         byte[] tmp = new byte[size];
         Arrays.fill(tmp, (byte)15);
         IvParameterSpec iv = new IvParameterSpec(tmp);
         cipher.init(Cipher.DECRYPT_MODE, key, iv);
      }
      catch(Exception e)
      {
         e.printStackTrace();
         throw new IOException("Failed to init cipher: "+e.getMessage());
View Full Code Here


   }

   public OutputStream getOutputStream() throws IOException
   {
      OutputStream os = delegate == null ? super.getOutputStream() : delegate.getOutputStream();
      Cipher cipher = null;
      try
      {
         cipher = Cipher.getInstance(algorithm);
         int size = cipher.getBlockSize();
         byte[] tmp = new byte[size];
         Arrays.fill(tmp, (byte)15);
         IvParameterSpec iv = new IvParameterSpec(tmp);
         cipher.init(Cipher.ENCRYPT_MODE, key, iv);
      }
      catch(Exception e)
      {
         throw new IOException("Failed to init cipher: "+e.getMessage());
      }
View Full Code Here

public class AESTools {
    private static final Logger LOG = LoggerFactory.getLogger(AESTools.class);

    public static String encrypt(String plainText, String encryptionKey, String salt) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE");
            SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8")));
            return Hex.encodeToString(cipher.doFinal(plainText.getBytes("UTF-8")));
        } catch (Exception e) {
            LOG.error("Could not encrypt value.", e);
        }
            return null;
    }
View Full Code Here

            return null;
    }

    public static String decrypt(String cipherText, String encryptionKey, String salt) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE");
            SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8")));
            return new String(cipher.doFinal(Hex.decode(cipherText)), "UTF-8");
        } catch (Exception e) {
            LOG.error("Could not decrypt value.", e);
        }
        return null;
    }
View Full Code Here

    try {
      // Get our secret key
      Key key = getKey();

      // Create the cipher
      Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); //$NON-NLS-1$

      // Initialize the cipher for encryption
      desCipher.init(Cipher.ENCRYPT_MODE, key);

      // Our cleartext as bytes
      byte[] cleartext = source.getBytes();

      // Encrypt the cleartext
      byte[] ciphertext = desCipher.doFinal(cleartext);

      // Return a String representation of the cipher text
      return getString(ciphertext);
    } catch (Exception e) {
            Debug.error(e.toString());
View Full Code Here

        sourceStr = "";
      // Get our secret key
      Key key = getKey();

      // Create the cipher
      Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); //$NON-NLS-1$

      // Encrypt the cleartext
      byte[] ciphertext = getBytes(sourceStr);

      // Initialize the same cipher for decryption
      desCipher.init(Cipher.DECRYPT_MODE, key);

      // Decrypt the ciphertext
      byte[] cleartext = desCipher.doFinal(ciphertext);

      // Return the clear text
      return new String(cleartext);
    } catch (Exception e) {
            Debug.error(e.toString());
View Full Code Here

    encrypt.setLocal_addr(tempAddress);
    MockObserver observer = new MockObserver();
    encrypt.setObserver(observer);
   
    // produce encrypted message
    Cipher cipher = encrypt.getSymEncodingCipher();
   
    MessageDigest digest = MessageDigest.getInstance("MD5");
    digest.reset();
    digest.update(encrypt.getDesKey().getEncoded());
      
    String symVersion = new String(digest.digest(), "UTF-8");
   
    encrypt.keyServer = false;
    Message msg = new Message();
    msg.setBuffer(cipher.doFinal("hello".getBytes()));
    msg.putHeader(EncryptHeader.KEY, new EncryptHeader(
        EncryptHeader.ENCRYPT, symVersion));
   
    Event evt = new Event(Event.MSG,msg);

    //pass in event to encrypt layer
   
    encrypt.up(evt);
   
    // assert that message is queued as we have no key
    assertTrue(observer.getUpMessages().isEmpty());
   
    // send a view change to trigger the become key server
    // we use the fact that our address is now the controller one
    Vector tempVector = new Vector();
    tempVector.add(tempAddress);
    View tempView = new View(new ViewId(tempAddress,1),tempVector);
    Event event = new Event(Event.VIEW_CHANGE,tempView);
    // this should have changed us to the key server
    encrypt.up(event);
   
    // send another encrypted message
    Message msg2 = new Message();
    msg2.setBuffer(cipher.doFinal("hello2".getBytes()));
    msg2.putHeader(EncryptHeader.KEY, new EncryptHeader(
        EncryptHeader.ENCRYPT, symVersion));
   
    // we should have three messages now in our observer
    // that are decrypted
View Full Code Here

    digest.update(server.getDesKey().getEncoded());
      
    String symVersion = new String(digest.digest(), "UTF-8");
   
    // encrypt and send an initial message to peer
    Cipher cipher = server.getSymEncodingCipher();
    Message msg = new Message();
    msg.setBuffer(cipher.doFinal("hello".getBytes()));
    msg.putHeader(EncryptHeader.KEY, new EncryptHeader(
        EncryptHeader.ENCRYPT, symVersion));
   
    Event evt = new Event(Event.MSG,msg);

    peer.up(evt);
    //assert that message is queued as we have no key from server
    assertTrue(peerObserver.getUpMessages().isEmpty());
   
    // send a view change where we are not the controller

   
    // send to peer - which should have peer2 as its key server
    peer.up(serverEvent);
    // assert that peer\ keyserver address is now set
    assertTrue(serverAddress == peer.getKeyServerAddr());
   
    // get the resulting message from the peer - should be a key request
   
    Event sent = (Event)peerObserver.getDownMessages().get("message0");
   
    assertTrue(((EncryptHeader)((Message)sent.getArg()).getHeader(ENCRYPT.EncryptHeader.KEY)).getType() == ENCRYPT.EncryptHeader.KEY_REQUEST);
    assertEquals(new String(((Message)sent.getArg()).getBuffer()),new String(peer.getKpair().getPublic().getEncoded()));
   
    // send this event to server
    server.up(sent);
   
    Event reply = (Event)serverObserver.getDownMessages().get("message1");
   

   
    //assert that reply is the session key encrypted with peer's public key
    assertTrue(((EncryptHeader)((Message)reply.getArg()).getHeader(ENCRYPT.EncryptHeader.KEY)).getType() == ENCRYPT.EncryptHeader.SECRETKEY);

 
    assertNotSame(peer.getDesKey(),server.getDesKey());
    // now send back to peer
    peer.up(reply);
   
    // assert that both now have same key
    assertEquals(peer.getDesKey(),server.getDesKey());
   
    // send another encrypted message to peer to test queue
    Message msg2 = new Message();
    msg2.setBuffer(cipher.doFinal("hello2".getBytes()));
    msg2.putHeader(EncryptHeader.KEY, new EncryptHeader(
        EncryptHeader.ENCRYPT, symVersion));
   
    Event evt2 = new Event(Event.MSG,msg2);
View Full Code Here

    Event event = new Event(Event.MSG,msg);
    encrypt.down(event);
    Message sentMsg = (Message)((Event)observer.getDownMessages().get("message0")).getArg();
    String encText = new String(sentMsg.getBuffer());
    assertNotSame(encText,messageText);
    Cipher cipher = encrypt2.getSymDecodingCipher();
    byte[] decodedBytes = cipher.doFinal(sentMsg.getBuffer());
    String temp = new String(decodedBytes);
    System.out.println("decoded text:" + temp);
    assertEquals(temp,messageText);

  }
View Full Code Here

    MockObserver observer = new MockObserver();
    encrypt.setObserver(observer);
   
    encrypt.keyServer = true;
    String messageText = "hello this is a test message";
    Cipher cipher = encrypt2.getSymEncodingCipher();
    byte[] encodedBytes = cipher.doFinal(messageText.getBytes());
    assertNotSame(new String(encodedBytes),messageText);
   
    MessageDigest digest = MessageDigest.getInstance("MD5");
    digest.reset();
    digest.update(encrypt.getDesKey().getEncoded());
View Full Code Here

TOP

Related Classes of javax.crypto.Cipher

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.