Converting Byte Sequences to Positive Integers

This section describes java.math.BigInteger methods that can be used to convert byte sequences into positive integers and convert back to byte sequences for RSA encryption and decryption operations.

From the previous tutorial, we learned that RSA keys generated from RsaKeyGenerator.java is working. Now we need to think about how to encrypt a byte sequences. We know this can be done by converting the byte sequence to positive integers, then applying the RSA encryption operation on the converted integers.

So we need to learn how to convert a byte sequence into a positive integer for the encryption purpose.

Similarly, we also need to learn how to convert a positive integer into a byte sequence after applying the RSA decryption operation to recover the original byte sequence.

For converting byte sequences into integers, the java.math.BigInteger class has offered 3 methods based the Java documentation:

Obviously, the best choice of converting a byte sequence into a positive integer is to use the BigInteger(int signum, byte[] magnitude) constructor as: "new BigInteger(1,byteArray)". This avoids getting negative numbers when the first bit of the byte sequence is a negative sign.

For converting a positive integer back to a byte sequence, we have to use toByteArray(). There are no other choices. But you may get an extra byte in the resulting byte sequence because of the extra sign bit added in the result. For example, the follow Java code will show that "barSignConverted" has 3 bytes, not 2 types.

byte[] barSign = {(byte)0xfe,(byte)0x31}; // 2 bytes of a Unicode text
BigInteger value = new BigInteger(1,barSign);  // 65073
byte[] barSignConverted = value.toByteArray();
System.out.println("Length: "+barSignConverted.length);

However, we can safely drop the first byte, if an extra byte is added for the sign bit, because it will contain the sign bit only, and we don't need the sign anyway.

Last update: 2013.

Table of Contents

 About This Book

 Cryptography Terminology

 Cryptography Basic Concepts

 Introduction to AES (Advanced Encryption Standard)

 Introduction to DES Algorithm

 DES Algorithm - Illustrated with Java Programs

 DES Algorithm Java Implementation

 DES Algorithm - Java Implementation in JDK JCE

 DES Encryption Operation Modes

 DES in Stream Cipher Modes

 PHP Implementation of DES - mcrypt

 Blowfish - 8-Byte Block Cipher

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 Introduction of RSA Algorithm

RSA Implementation using java.math.BigInteger Class

 java.Math.BigInteger Class

 Generating Prime Number with BigInteger Class

 Performance of Prime Number Generation

 RSA Encryption Implementation using BigInteger Class

 RsaKeyGenerator.java for RSA Key Generation

 RSA Keys Generated by RsaKeyGenerator.java

 RsaKeyValidator.java for RSA Key Validation

 64-bit RSA Key Validated by RsaKeyValidator.java

Converting Byte Sequences to Positive Integers

 Cleartext Block Size for RSA Encryption

 Cleartext Message Padding and Revised Block Size

 Ciphertext Block Size for RSA Encryption

 RsaKeyEncryption.java for RSA Encryption Operation

 RsaKeyDecryption.java for RSA Decryption Operation

 Testing RsaKeyEncryption.java with a 16-bit Key

 Testing RsaKeyEncryption.java with a 64-bit Key

 Testing RsaKeyEncryption.java with a 3072-bit Key

 Introduction of DSA (Digital Signature Algorithm)

 Java Default Implementation of DSA

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Cipher - Public Key Encryption and Decryption

 MD5 Mesasge Digest Algorithm

 SHA1 Mesasge Digest Algorithm

 OpenSSL Introduction and Installation

 OpenSSL Generating and Managing RSA Keys

 OpenSSL Managing Certificates

 OpenSSL Generating and Signing CSR

 OpenSSL Validating Certificate Path

 "keytool" and "keystore" from JDK

 "OpenSSL" Signing CSR Generated by "keytool"

 Migrating Keys from "keystore" to "OpenSSL" Key Files

 Certificate X.509 Standard and DER/PEM Formats

 Migrating Keys from "OpenSSL" Key Files to "keystore"

 Using Certificates in IE (Internet Explorer)

 Using Certificates in Firefox

 Using Certificates in Google Chrome

 Outdated Tutorials

 References

 PDF Printing Version