List of usage examples for java.lang String String
public String(StringBuilder builder)
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "www.java2s.com".getBytes(); byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };//from w ww . ja v a 2 s. c om SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); System.out.println("input : " + new String(input)); // encryption pass cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); ByteArrayInputStream bIn = new ByteArrayInputStream(input); CipherInputStream cIn = new CipherInputStream(bIn, cipher); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); int ch; while ((ch = cIn.read()) >= 0) { bOut.write(ch); } byte[] cipherText = bOut.toByteArray(); System.out.println("cipher: " + new String(cipherText)); // decryption pass cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); bOut = new ByteArrayOutputStream(); CipherOutputStream cOut = new CipherOutputStream(bOut, cipher); cOut.write(cipherText); cOut.close(); System.out.println("plain : " + new String(bOut.toByteArray())); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Document document = new Document(PageSize.A6); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open();//from ww w .j a v a 2 s . com document.add(new Paragraph("Hello World")); document.add(new Paragraph("Hello People")); document.close(); PdfReader reader = new PdfReader("2.pdf"); PdfDictionary page = reader.getPageN(1); PRIndirectReference objectReference = (PRIndirectReference) page.get(PdfName.CONTENTS); PRStream stream = (PRStream) PdfReader.getPdfObject(objectReference); byte[] streamBytes = PdfReader.getStreamBytes(stream); String contentStream = new String(streamBytes); System.out.println(contentStream); PRTokeniser tokenizer = new PRTokeniser(streamBytes); while (tokenizer.nextToken()) { if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) { System.out.println(tokenizer.getStringValue()); } } StringBuffer buf = new StringBuffer(); int pos = contentStream.indexOf("Hello World") + 11; buf.append(contentStream.substring(0, pos)); buf.append("Hello"); buf.append(contentStream.substring(pos)); String hackedContentStream = buf.toString(); document = new Document(PageSize.A6); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorldStreamHacked.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); cb.setLiteral(hackedContentStream); document.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = new byte[] { (byte) 0xbe, (byte) 0xef }; Cipher cipher = Cipher.getInstance("RSA/None/NoPadding", "BC"); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger("12345678", 16), new BigInteger("11", 16)); RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(new BigInteger("12345678", 16), new BigInteger("12345678", 16)); RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec); RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec); cipher.init(Cipher.ENCRYPT_MODE, pubKey); byte[] cipherText = cipher.doFinal(input); System.out.println("cipher: " + new String(cipherText)); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] plainText = cipher.doFinal(cipherText); System.out.println("plain : " + new String(plainText)); }
From source file:com.manning.blogapps.chapter10.examples.AuthDelete.java
public static void main(String[] args) throws Exception { if (args.length < 3) { System.out.println("USAGE: authdelete <username> <password> <url>"); System.exit(-1);/* w ww . jav a 2 s. c o m*/ } String credentials = args[0] + ":" + args[1]; String url = args[2]; HttpClient httpClient = new HttpClient(); DeleteMethod method = new DeleteMethod(url); method.setRequestHeader("Authorization", "Basic " + new String(Base64.encodeBase64(credentials.getBytes()))); httpClient.executeMethod(method); System.out.println("Server returned status code: "); }
From source file:europarl.Main.java
public static void main(String[] args) { //GLOBAL OPTIONS String target_word = new String("plane"); for (String arg : args) { if (arg.startsWith("-")) { } else {/*w ww . jav a2 s. c om*/ target_word = arg; } } if (!Cfg.load_config("config/configuration.properties")) return; WordAlignment align = new WordAlignment(); long startTime = System.currentTimeMillis(); align.setStemmer(new SnowballStemmer("english")); align.getFromGz("data/it-en.A3.final.gz", target_word, -1); long endTime = System.currentTimeMillis(); Main.log.info("Read in " + (endTime - startTime) / 1000.0 + " seconds"); align.print(); startTime = System.currentTimeMillis(); endTime = System.currentTimeMillis(); Main.log.info("Converted n " + (endTime - startTime) / 1000.0 + " seconds"); align.saveToArff("./data/dataset_" + target_word + ".arff"); return; }
From source file:Main.java
public static void main(String[] args) throws Exception { //Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); DHParameterSpec dhParams = new DHParameterSpec(p512, g512); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC"); keyGen.initialize(dhParams, new SecureRandom()); KeyAgreement aKeyAgree = KeyAgreement.getInstance("DH", "BC"); KeyPair aPair = keyGen.generateKeyPair(); KeyAgreement bKeyAgree = KeyAgreement.getInstance("DH", "BC"); KeyPair bPair = keyGen.generateKeyPair(); aKeyAgree.init(aPair.getPrivate());//ww w . j a v a2s. com bKeyAgree.init(bPair.getPrivate()); aKeyAgree.doPhase(bPair.getPublic(), true); bKeyAgree.doPhase(aPair.getPublic(), true); MessageDigest hash = MessageDigest.getInstance("SHA1", "BC"); System.out.println(new String(hash.digest(aKeyAgree.generateSecret()))); System.out.println(new String(hash.digest(bKeyAgree.generateSecret()))); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); DHParameterSpec dhParams = new DHParameterSpec(p512, g512); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC"); keyGen.initialize(dhParams, new SecureRandom()); KeyAgreement aKeyAgree = KeyAgreement.getInstance("DH", "BC"); KeyPair aPair = keyGen.generateKeyPair(); KeyAgreement bKeyAgree = KeyAgreement.getInstance("DH", "BC"); KeyPair bPair = keyGen.generateKeyPair(); aKeyAgree.init(aPair.getPrivate());/* w w w. ja v a2 s. com*/ bKeyAgree.init(bPair.getPrivate()); aKeyAgree.doPhase(bPair.getPublic(), true); bKeyAgree.doPhase(aPair.getPublic(), true); MessageDigest hash = MessageDigest.getInstance("SHA1", "BC"); System.out.println(new String(hash.digest(aKeyAgree.generateSecret()))); System.out.println(new String(hash.digest(bKeyAgree.generateSecret()))); }
From source file:com.manning.blogapps.chapter10.examples.AuthGetJava.java
public static void main(String[] args) throws Exception { if (args.length < 3) { System.out.println("USAGE: authget <username> <password> <url>"); System.exit(-1);//from w w w . ja v a 2s . com } String credentials = args[0] + ":" + args[1]; URL url = new URL(args[2]); URLConnection conn = url.openConnection(); conn.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64(credentials.getBytes()))); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String s = null; while ((s = in.readLine()) != null) { System.out.println(s); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "input".getBytes(); byte[] keyBytes = new byte[] { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef }; byte[] ivBytes = new byte[] { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; SecretKeySpec key = new SecretKeySpec(keyBytes, "DES"); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS7Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println("plain : " + new String(plainText)); }
From source file:com.manning.blogapps.chapter10.examples.AuthGet.java
public static void main(String[] args) throws Exception { if (args.length < 3) { System.out.println("USAGE: authget <username> <password> <url>"); System.exit(-1);/*from www . jav a 2s . c o m*/ } String credentials = args[0] + ":" + args[1]; String url = args[2]; HttpClient httpClient = new HttpClient(); GetMethod method = new GetMethod(url); method.setRequestHeader("Authorization", "Basic " + new String(Base64.encodeBase64(credentials.getBytes()))); httpClient.executeMethod(method); System.out.println(method.getResponseBodyAsString()); }