TIP: Get jars based on your java installation
- Apache log4j
- http://logging.apache.org/log4j/1.2/download.html
- log4j-1.2.16
- Bouncy castle Crypto APIs
- http://www.bouncycastle.org/latest_releases.html
- bcprov-jdk15-146
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | package com.as400samplecode; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.security.Security; import org.bouncycastle.crypto.AsymmetricBlockCipher; import org.bouncycastle.crypto.engines.RSAEngine; import org.bouncycastle.crypto.params.AsymmetricKeyParameter; import org.bouncycastle.crypto.util.PublicKeyFactory; import sun.misc.BASE64Decoder; public class RSAEncryption { public static void main(String[] args) { String publicKeyFilename = null ; String inputData = null ; RSAEncryption rsaEncryption = new RSAEncryption(); if (args.length < 2 ) { System.err.println( "Usage: java " + rsaEncryption.getClass().getName()+ " Public_Key_Filename Input_String_data" ); System.exit( 1 ); } publicKeyFilename = args[ 0 ].trim(); inputData = args[ 1 ].trim(); rsaEncryption.encrypt(publicKeyFilename, inputData); } private void encrypt (String publicKeyFilename, String inputFilename, String encryptedFilename){ try { Security.addProvider( new org.bouncycastle.jce.provider.BouncyCastleProvider()); String key = readFileAsString(publicKeyFilename); BASE64Decoder b64 = new BASE64Decoder(); AsymmetricKeyParameter publicKey = (AsymmetricKeyParameter) PublicKeyFactory.createKey(b64.decodeBuffer(key)); AsymmetricBlockCipher e = new RSAEngine(); e = new org.bouncycastle.crypto.encodings.PKCS1Encoding(e); e.init( true , publicKey); String inputdata = readFileAsString(inputFilename); byte [] messageBytes = inputdata.getBytes(); byte [] hexEncodedCipher = e.processBlock(messageBytes, 0 , messageBytes.length); System.out.println(getHexString(hexEncodedCipher)); BufferedWriter out = new BufferedWriter( new FileWriter(encryptedFilename)); out.write(getHexString(hexEncodedCipher)); out.close(); } catch (Exception e) { System.out.println(e); } } private String encrypt (String publicKeyFilename, String inputData){ String encryptedData = null ; try { Security.addProvider( new org.bouncycastle.jce.provider.BouncyCastleProvider()); BASE64Decoder b64 = new BASE64Decoder(); String key = readFileAsString(publicKeyFilename); AsymmetricKeyParameter publicKey = (AsymmetricKeyParameter) PublicKeyFactory.createKey(b64.decodeBuffer(key)); AsymmetricBlockCipher e = new RSAEngine(); e = new org.bouncycastle.crypto.encodings.PKCS1Encoding(e); e.init( true , publicKey); byte [] messageBytes = inputData.getBytes(); byte [] hexEncodedCipher = e.processBlock(messageBytes, 0 , messageBytes.length); System.out.println(getHexString(hexEncodedCipher)); encryptedData = getHexString(hexEncodedCipher); } catch (Exception e) { System.out.println(e); } return encryptedData; } public static String getHexString( byte [] b) throws Exception { String result = "" ; for ( int i= 0 ; i < b.length; i++) { result += Integer.toString( ( b[i] & 0xff ) + 0x100 , 16 ).substring( 1 ); } return result; } private static String readFileAsString(String filePath) throws java.io.IOException{ StringBuffer fileData = new StringBuffer( 1000 ); BufferedReader reader = new BufferedReader( new FileReader(filePath)); char [] buf = new char [ 1024 ]; int numRead= 0 ; while ((numRead=reader.read(buf)) != - 1 ){ String readData = String.valueOf(buf, 0 , numRead); fileData.append(readData); buf = new char [ 1024 ]; } reader.close(); System.out.println(fileData.toString()); return fileData.toString(); } } |
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.