Kedar Bhingare

Kedar Bhingare

  • NA
  • 16
  • 354

Get Dot net specific code for following java program

Jan 18 2019 3:33 AM
I have following code which generate key.
 
 
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("License");
document.appendChild(rootElement);
Element computerElement = document.createElement("KeyRequest");
computerElement.appendChild(document.createTextNode(keyRequestTextField.getText()));
rootElement.appendChild(computerElement);
Element expirationDateElement = document.createElement("ExpirationDate");
expirationDateElement.appendChild(document.createTextNode(sdf.format((Date) expirationDateSpinner.getValue())));
rootElement.appendChild(expirationDateElement);
// Get string from xml
StringWriter writer = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(writer));
String licenseKeySource = writer.toString();
// Encrypt
// licenseKeyTextArea.setText(licenseKeySource);
Cipher cipher = Cipher.getInstance("AES");
String encryptionKey = keyRequestTextField.getText().replaceAll("-", "");
Key key = new SecretKeySpec(encryptionKey.getBytes(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] data = cipher.doFinal(licenseKeySource.getBytes(Charset.forName("UTF-8")));
// Convert to dictionary
int[] bytes;
if ( (data.length * 8) % 6 == 0) {
bytes = new int[data.length];
} else {
bytes = new int[data.length+1];
}
for (int i = 0; i < data.length; i++) {
bytes[i] = data[i] + 128;
System.out.println(bytes[i]);
}
char[] symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/".toCharArray();
//StringBuilder builder = new StringBuilder("#$%");
StringBuilder builder = new StringBuilder();
System.out.println("Length = " + bytes.length);
for (int index = 0; index < bytes.length * 8 / 6; index++) {
int position = (index * 6) / 8;
int offset = (index * 6) % 8;
int result = 0;
if (offset == 0) {
int b = bytes[position];
result = b >> 2;
} else if (offset == 2) {
int b = bytes[position];
result = b & 63;
} else if (offset == 4) {
int c1 = (bytes[position] << 2) & 63;
int c2 = bytes[position + 1] >> 6;
result = c1 | c2;
} else if (offset == 6) {
int c1 = (bytes[position] << 4) & 63;
int c2 = (bytes[position + 1] >> 4) & 15;
result = c1 | c2;
}
builder.append(symbols[result]);
}
//builder.append("%$#");
licenseKeyTextArea.setText(builder.toString());
 

Answers (1)