List of usage examples for org.bouncycastle.util.encoders Hex Hex
Hex
From source file:cc.telepath.phage.PhageGroup.java
License:GNU General Public License
/** * Establish a secure channel with a specific PhageIdentity. * The original contact point is determinitstic - take the hash of both keys concatenatd in descending order. * Return the secret channel being used to share the current communication AES keys. * @param i//from w ww . j a v a 2s . c om * @return secretchannel */ public String advertiseChannel(PhageIdentity i, PhageFCPClient pcl) throws IOException, FcpException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, SignatureException { Base64 base64 = new Base64(); Hex hex = new Hex(); String ownkey = new String(base64.encode(this.PublicKey.getEncoded())); String identkey = new String(base64.encode(i.getPubkey().getEncoded())); String combination = null; if (ownkey.compareTo(identkey) < 0) { combination = ownkey + identkey; } else { combination = identkey + ownkey; } // Generate a 100 character random KSK for us to meet at RandomStringUtils rsu = new RandomStringUtils(); String channel = rsu.randomAlphanumeric(100); Cipher cipher = Cipher.getInstance(i.getPubkey().getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, i.getPubkey()); String secretchannel = "KSK@" + channel; byte[] encryptData = cipher.doFinal(secretchannel.getBytes()); Signature sig = Signature.getInstance("SHA512withRSA"); sig.initSign(PrivateKey); sig.update(new String(base64.encode(encryptData)).getBytes()); byte[] signatureBytes = sig.sign(); String encryptedMessage = new String(base64.encode(encryptData)); String signature = new String(base64.encode(signatureBytes)); MessageDigest md = MessageDigest.getInstance("SHA-512"); md.update(combination.getBytes()); byte[] rendezvousbytes = md.digest(); String rendezvous = new String(hex.encode(rendezvousbytes)); String URI = pcl.putData(rendezvous, (encryptedMessage + ":" + signature).getBytes(), null, null, "text/plain", false); this.privateChannels.put(i.getFreenetPubkey(), secretchannel); return secretchannel; }
From source file:cc.telepath.phage.PhageIdentity.java
License:GNU General Public License
/** * Discover a secret channel that has been announced to our key. * @param PhageGroupPubKey//from w w w .j av a 2 s . c o m * @param pcl * @return * @throws IOException * @throws FcpException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidKeyException */ public void discoverSecretChannel(String PhageGroupPubKey, PhageFCPClient pcl) throws InvalidSigException, IOException, FcpException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, SignatureException { Base64 base64 = new Base64(); Hex hex = new Hex(); Crypto c = new Crypto(); String ownkey = new String(base64.encode(this.pubKey.getEncoded())); String combination; if (PhageGroupPubKey.compareTo(ownkey) < 0) { combination = PhageGroupPubKey + ownkey; } else { combination = ownkey + PhageGroupPubKey; } MessageDigest md = MessageDigest.getInstance("SHA-512"); md.update(combination.getBytes()); byte[] rendezvousbytes = md.digest(); String rendezvous = "KSK@" + new String(hex.encode(rendezvousbytes)); String secretChannelAnnouncement = new String(pcl.getData(rendezvous)); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey groupPk = kf.generatePublic(new X509EncodedKeySpec(base64.decode(PhageGroupPubKey))); boolean sigValid = c.sigValid(secretChannelAnnouncement.split(":")[0], secretChannelAnnouncement.split(":")[1], groupPk); String message = c.decryptMessage(this.getPrivkey(), secretChannelAnnouncement.split(":")[0]); System.out.println("SecretChannel: " + message); if (!sigValid) { throw new InvalidSigException("The message signature from " + PhageGroupPubKey + "failed!! Either invalid data was provided or somebody is impersonating this identity."); } else { this.contactChannel = message; } }