Last active
December 31, 2015 23:49
-
-
Save cpu/8063008 to your computer and use it in GitHub Desktop.
Example code using BSAFE & DUAL_EC_DRBG to generate n bytes of randomness from a terrifically suspect CSRNG. You must have the BSAFE shareCrypto.jar library on your classpath. If you can't find a copy of that you should get in touch with me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.security.Provider; | |
import java.security.SecureRandom; | |
import java.security.Security; | |
import com.rsa.jsafe.provider.SensitiveData; | |
//Largely cribbed from RSA BSAFE Docs. | |
public class ShittyCrypto { | |
private static final int NUM_BYTES = 32; | |
public static void main(String[] args) throws Exception { | |
int numRandBytes = NUM_BYTES; | |
if(args.length >= 1) | |
numRandBytes = Integer.parseInt(args[0]); | |
System.out.println("Generating "+ numRandBytes +" bytes from Dual_EC_DRBG - <3<3 NSA"); | |
// Remove provider if it's already registered, or insert will fail. | |
Security.removeProvider("JsafeJCE"); | |
// Create a new provider object for the JsafeJCE provider. | |
Provider jsafeProvider = new com.rsa.jsafe.provider.JsafeJCE(); | |
// Register provider in 1st position. | |
int position = Security.insertProviderAt(jsafeProvider, 1); | |
if (position != 1) { | |
throw new RuntimeException( | |
"Failed to insert provider at first position"); | |
} | |
SecureRandom random = null; | |
byte[] randomBytes = null; | |
try { | |
// Create a secure random number generator using the Dual EC DRBG | |
// algorithm. | |
random = SecureRandom.getInstance("ECDRBG", "JsafeJCE"); | |
// Seeding is the most important aspect of dealing with a secure | |
// random number generator. It is extremely important that you seed | |
// with a value that contains sufficient entropy. The following | |
// example uses a seed generator. | |
random.setSeed(generateSeed()); | |
// To generate some (pseudo) random bytes, use nextBytes() method. | |
randomBytes = new byte[numRandBytes]; | |
random.nextBytes(randomBytes); | |
print(randomBytes); | |
} finally { | |
// Cryptographic objects should be cleared once they are no longer | |
// needed. | |
SensitiveData.clear(random); | |
// Random bytes may not appear to be "cryptographic objects", | |
// however, if knowing the random bytes could aide an attacker in | |
// defeating the security of your solution, you should clear the | |
// buffer that contains the random bytes. | |
SensitiveData.clear(randomBytes); | |
} | |
} | |
protected static void print(byte[] buffer) | |
{ | |
BigInteger bi = new BigInteger(1, buffer); | |
System.out.println(String.format("%0" + (buffer.length << 1) + "X", bi)); | |
} | |
protected static byte[] generateSeed() { | |
SecureRandom seeder = new SecureRandom(); | |
return seeder.generateSeed(20); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment