| | |
128-bit Encryption
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
Thaks in advance for reading my thread....
Just now we have decided that have to implement 128-bit encryption code...
and we prepared some code in java...
and thats not giving the exact ouput of encrypted and decrypted text ....
Please Provide any 128-bit Encryption code snippets so that i can move forward the below is the code which i prepared
//package encryption;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.lang.*;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
public class Encryption {
private RSAPrivateCrtKeyParameters _RSAPrivateKey;
private RSAKeyParameters _RSAPublicKey;
public static void main(String[] args) {
Encryption theEncryption = new Encryption();
String theStringBeforeEncryption = "r";
String theStringAfterEncryption = null;
byte[] theEncryptedString;
try {
System.out.println(theStringBeforeEncryption);
theEncryption.generateRSAKeyPair();
theEncryptedString = theEncryption.RSAEncrypt(theStringBeforeEncryption.getBytes());
String str=(new Base64Converter()).encode(theEncryptedString);
System.out.println(str);
System.out.println(theEncryptedString);
theStringAfterEncryption = new String(theEncryption.RSADecrypt(theEncryptedString));
System.out.println(theStringAfterEncryption);
}
catch (Exception e)
{
// TODO Handle exception!
e.printStackTrace();
}
}
private void generateRSAKeyPair ( ) throws Exception {
SecureRandom theSecureRandom = new SecureRandom();
BigInteger thePublicExponent = new BigInteger("10001", 16);
RSAKeyGenerationParameters theRSAKeyGenParam =
new RSAKeyGenerationParameters(thePublicExponent, theSecureRandom, 1024, 80);
RSAKeyPairGenerator theRSAKeyPairGen = new RSAKeyPairGenerator();
theRSAKeyPairGen.init(theRSAKeyGenParam);
AsymmetricCipherKeyPair theKeyPair = theRSAKeyPairGen.generateKeyPair();
_RSAPrivateKey = (RSAPrivateCrtKeyParameters) theKeyPair.getPrivate();
_RSAPublicKey = (RSAKeyParameters) theKeyPair.getPublic();
}
private byte [] RSAEncrypt (byte [] toEncrypt) throws Exception
{
if (_RSAPublicKey == null)
{
throw new Exception("Please generate RSA keys first in order to work");
}
AsymmetricBlockCipher theEngine = new RSAEngine();
theEngine = new PKCS1Encoding(theEngine);
theEngine.init(true, _RSAPublicKey);
return theEngine.processBlock(toEncrypt, 0, toEncrypt.length);
}
private byte [] RSADecrypt (byte [] toDecrypt) throws Exception {
if (_RSAPrivateKey == null)
{
throw new Exception("Please generate RSA keys first in order to work");
}
AsymmetricBlockCipher theEngine = new RSAEngine();
theEngine = new PKCS1Encoding(theEngine);
theEngine.init(false, _RSAPrivateKey);
return theEngine.processBlock(toDecrypt, 0, toDecrypt.length);
}
}
class Base64Converter
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
{
public static final char [ ] alphabet = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
'4', '5', '6', '7', '8', '9', '+', '/' }; // 56 to 63
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public static String encode ( String s )
//////////////////////////////////////////////////////////////////////
{
return encode ( s.getBytes ( ) );
}
public static String encode ( byte [ ] octetString )
//////////////////////////////////////////////////////////////////////
{
int bits24;
int bits6;
char [ ] out
= new char [ ( ( octetString.length - 1 ) / 3 + 1 ) * 4 ];
int outIndex = 0;
int i = 0;
while ( ( i + 3 ) <= octetString.length )
{
// store the octets
bits24 = ( octetString [ i++ ] & 0xFF ) << 16;
bits24 |= ( octetString [ i++ ] & 0xFF ) << 8;
bits24 |= ( octetString [ i++ ] & 0xFF ) << 0;
bits6 = ( bits24 & 0x00FC0000 ) >> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x00000FC0 ) >> 6;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0000003F );
out [ outIndex++ ] = alphabet [ bits6 ];
}
if ( octetString.length - i == 2 )
{
// store the octets
bits24 = ( octetString [ i ] & 0xFF ) << 16;
bits24 |= ( octetString [ i + 1 ] & 0xFF ) << 8;
bits6 = ( bits24 & 0x00FC0000 ) >> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x00000FC0 ) >> 6;
out [ outIndex++ ] = alphabet [ bits6 ];
// padding
out [ outIndex++ ] = '=';
}
else if ( octetString.length - i == 1 )
{
// store the octets
bits24 = ( octetString [ i ] & 0xFF ) << 16;
bits6 = ( bits24 & 0x00FC0000 ) >> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
// padding
out [ outIndex++ ] = '=';
out [ outIndex++ ] = '=';
}
return new String ( out );
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
}
Just now we have decided that have to implement 128-bit encryption code...
and we prepared some code in java...
and thats not giving the exact ouput of encrypted and decrypted text ....
Please Provide any 128-bit Encryption code snippets so that i can move forward the below is the code which i prepared
//package encryption;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.lang.*;
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.encodings.PKCS1Encoding;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
public class Encryption {
private RSAPrivateCrtKeyParameters _RSAPrivateKey;
private RSAKeyParameters _RSAPublicKey;
public static void main(String[] args) {
Encryption theEncryption = new Encryption();
String theStringBeforeEncryption = "r";
String theStringAfterEncryption = null;
byte[] theEncryptedString;
try {
System.out.println(theStringBeforeEncryption);
theEncryption.generateRSAKeyPair();
theEncryptedString = theEncryption.RSAEncrypt(theStringBeforeEncryption.getBytes());
String str=(new Base64Converter()).encode(theEncryptedString);
System.out.println(str);
System.out.println(theEncryptedString);
theStringAfterEncryption = new String(theEncryption.RSADecrypt(theEncryptedString));
System.out.println(theStringAfterEncryption);
}
catch (Exception e)
{
// TODO Handle exception!
e.printStackTrace();
}
}
private void generateRSAKeyPair ( ) throws Exception {
SecureRandom theSecureRandom = new SecureRandom();
BigInteger thePublicExponent = new BigInteger("10001", 16);
RSAKeyGenerationParameters theRSAKeyGenParam =
new RSAKeyGenerationParameters(thePublicExponent, theSecureRandom, 1024, 80);
RSAKeyPairGenerator theRSAKeyPairGen = new RSAKeyPairGenerator();
theRSAKeyPairGen.init(theRSAKeyGenParam);
AsymmetricCipherKeyPair theKeyPair = theRSAKeyPairGen.generateKeyPair();
_RSAPrivateKey = (RSAPrivateCrtKeyParameters) theKeyPair.getPrivate();
_RSAPublicKey = (RSAKeyParameters) theKeyPair.getPublic();
}
private byte [] RSAEncrypt (byte [] toEncrypt) throws Exception
{
if (_RSAPublicKey == null)
{
throw new Exception("Please generate RSA keys first in order to work");
}
AsymmetricBlockCipher theEngine = new RSAEngine();
theEngine = new PKCS1Encoding(theEngine);
theEngine.init(true, _RSAPublicKey);
return theEngine.processBlock(toEncrypt, 0, toEncrypt.length);
}
private byte [] RSADecrypt (byte [] toDecrypt) throws Exception {
if (_RSAPrivateKey == null)
{
throw new Exception("Please generate RSA keys first in order to work");
}
AsymmetricBlockCipher theEngine = new RSAEngine();
theEngine = new PKCS1Encoding(theEngine);
theEngine.init(false, _RSAPrivateKey);
return theEngine.processBlock(toDecrypt, 0, toDecrypt.length);
}
}
class Base64Converter
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
{
public static final char [ ] alphabet = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
'4', '5', '6', '7', '8', '9', '+', '/' }; // 56 to 63
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public static String encode ( String s )
//////////////////////////////////////////////////////////////////////
{
return encode ( s.getBytes ( ) );
}
public static String encode ( byte [ ] octetString )
//////////////////////////////////////////////////////////////////////
{
int bits24;
int bits6;
char [ ] out
= new char [ ( ( octetString.length - 1 ) / 3 + 1 ) * 4 ];
int outIndex = 0;
int i = 0;
while ( ( i + 3 ) <= octetString.length )
{
// store the octets
bits24 = ( octetString [ i++ ] & 0xFF ) << 16;
bits24 |= ( octetString [ i++ ] & 0xFF ) << 8;
bits24 |= ( octetString [ i++ ] & 0xFF ) << 0;
bits6 = ( bits24 & 0x00FC0000 ) >> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x00000FC0 ) >> 6;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0000003F );
out [ outIndex++ ] = alphabet [ bits6 ];
}
if ( octetString.length - i == 2 )
{
// store the octets
bits24 = ( octetString [ i ] & 0xFF ) << 16;
bits24 |= ( octetString [ i + 1 ] & 0xFF ) << 8;
bits6 = ( bits24 & 0x00FC0000 ) >> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x00000FC0 ) >> 6;
out [ outIndex++ ] = alphabet [ bits6 ];
// padding
out [ outIndex++ ] = '=';
}
else if ( octetString.length - i == 1 )
{
// store the octets
bits24 = ( octetString [ i ] & 0xFF ) << 16;
bits6 = ( bits24 & 0x00FC0000 ) >> 18;
out [ outIndex++ ] = alphabet [ bits6 ];
bits6 = ( bits24 & 0x0003F000 ) >> 12;
out [ outIndex++ ] = alphabet [ bits6 ];
// padding
out [ outIndex++ ] = '=';
out [ outIndex++ ] = '=';
}
return new String ( out );
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
}
![]() |
Similar Threads
- PNY Nvidia GeForce 7600GS 512MB 128-bit GDDR2 AGP 4X/8X Video Card (Monitors, Displays and Video Cards)
- Cannot open certain websites, including hotmail (Web Browsers)
- DNS ERROR>>>CANNOT CONNECT TO SECURE SITES (Viruses, Spyware and other Nasties)
- Hotmail Login (Windows NT / 2000 / XP)
- decrypting (Java)
- 128 bit encryption (Viruses, Spyware and other Nasties)
- xp passwords (Windows NT / 2000 / XP)
- 'Page cannot be displayed' (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: how to generate custom error pages instead of default error pages
- Next Thread: Dice Rolling Program
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health hql html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows





