Jumat, 24 April 2009

sHare tugas Keamanan KomputeR

1. Enkripsi

#include  
#include  
#include  
 
main() 

  char tulisan[100], terenkripsi[100]; 
  int kunci, panjang, kode; 
 
  cout << "Masukkan tulisan = ";    gets(tulisan);    cout << "Masukkan nilai kunci = ";    cin >> kunci; 
  panjang = strlen(tulisan); 
  strcpy(terenkripsi,tulisan); 
  for (int i=0;i;i++) { 
  kode = int(tulisan[i])+kunci; 
  if (kode < kode =" kode"> 126) 
  kode = kode - 95; 
  terenkripsi[i] = char(kode); 
  } 
  cout << "Terenkripsi = " <<>

2. Encryption

// Exercise 19.12: Encryption.cpp
// Encrypts data entered by users.
#include // required to perform C++ stream I/O
#include // required to access string functions

using namespace std; // for accessing C++ Standard Library members

// function prototypes
void substitutionCipher( string & );
void transpositionCipher( string & );

// function main begins program execution
int main ()
{
  // define variables
  string input; // stores the plain text that the user enters
  int selection; // stores a number corresponding to cipher type

  cout << "\nEnter plain text: ";   getline( cin, input );   cout << "\nSelect the encryption type" <<>> selection;

  // determine which encryption type to perform
  if ( selection == 1 )
  {
  substitutionCipher( input ); // call substitutionCipher
  } // end if
  else if ( selection == 2 )
  {
  transpositionCipher( input ); // call transpositionCipher
  } // end else if
  else
  {
  cout << "\nError: Please enter a valid menu option.\n" << normalalphabet = "abcdefghijklmnopqrstuvwxyz .!?," cipheralphabet = "cdefg.hijk!lmn opqr?stuv,wxyzab" cipher = "" firstword = "" lastword = "">

3. Security Test

//
// $Header: /home/portnoy/u3/src/dsds/test/SecurityTest.cpp,v 1.8 2004-02-12 21:13:20 egles_g Exp $
//
// Copyright (c) 2003, The Regents of the University of California, through
// Lawrence Berkeley National Laboratory (subject to receipt of any required
// approvals from the U.S. Dept. of Energy). All rights reserved.
//

#include
#include

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace dsds;
using namespace std;

void testCiphers();
void testGDH();
void doG1(const DHParams&);
void doG2(const DHParams&);
void doG3(const DHParams&);


int main() {
  try {
  Provider po(new OpenSSLProviderImpl);

  Provider::setProvider(po);

  cout << "Loading digest algorithms ..." <<>loadDigestAlgorithms();

  cout << "Loading ciphers ..." <<>loadCipherAlgorithms();

  cout << "Seeding generator ..." << seed = "WE NEED TO A BETTER JOB SEEDING">seedRandomGenerator(seed)) {
  cout << "Unable to seed generator" << md =" MessageDigest::getInstance(">update("toto");
  md->update("toto");
  md->update("totfldsglgkdkgkdgko");
  md->update("toto");

  cout << "digest size : " <<>digest().size() << po =" Provider::getProvider();" params =" po-">genDHParams(512, 5);

  ofstream os("prime");
   
  os.write(params.getModulus().data(), 512 / 8);
   
  const string& generator = params.getGenerator();
  int len = generator.length();

  os.write((const char *)&len, 4);
  os.write(generator.data(), len);
   
  if (!os) {
  throw Exception("WRITING DH PARAMS TO FILE FAILED");
  }

  os.close();
  } else {
  string modulus(512 / 8, '\0');

  is.read((char *) modulus.data(), 512 / 8);

  int len;

  is.read((char *)&len, 4);
  string generator(len, '\0');

  is.read((char *) generator.data(), len);
   
  if (!is) {
  throw Exception("READING DH PARAMS FROM FILE FAILED");
  }

  is.close();
  params = DHParams(modulus, generator);
  }

  doG1(params);
  doG2(params);
  doG3(params);
  cout << "OUT TEST GDH" << keygen =" KeyGen::getInstance();" gdh =" GDHKeyAgreement::getInstance();"> flow(1, params.getGenerator());
  
  for (int i = 0; i < pkey =" keyGen-">generateDhKey(params);

  gdh->init(pKey, params);
  flow = gdh->computeFlow(flow, true);

  Key sKey = gdh->computeKey(flow[0]);
  }
}

void doG2(const DHParams& params) {
  KeyGen keyGen = KeyGen::getInstance();
  Key pKey1 = keyGen->generateDhKey(params);
  Key pKey2 = keyGen->generateDhKey(params);

  if (pKey1.getKey() == pKey2.getKey()) {
  cout << "GENERATOR IS REALLY LAME" << gdh1 =" GDHKeyAgreement::getInstance();" gdh2 =" GDHKeyAgreement::getInstance();">init(pKey1, params);
  gdh2->init(pKey2, params);

  vector flow(1, params.getGenerator());
  
  flow = gdh1->computeFlow(flow, false);
  flow = gdh2->computeFlow(flow, true);

  Key sKey1 = gdh1->computeKey(flow[0]);
  Key sKey2 = gdh2->computeKey(flow[1]);

  if (sKey1.getKey() != sKey2.getKey()) {
  cout << "OOPS SECRET KEYS DIFFERS" << original = "BYE" i =" 0;" cipher =" Cipher::getInstance(">encryptInit(sKey1);

  cout << "Encrypting ..." << temp =" cipher-">update(original);
   
  temp += cipher->doFinal();

  cout << "Initializing cipher for decryption ..." <<>decryptInit(sKey2);

  cout << "Decrypting ..." << temp =" cipher-">update(temp);

  temp += cipher->doFinal();

  if (temp != original) {
  cout << "Encryption/Decryption failed" << hmac =" Hmac::getInstance(">init(sKey1);

  cout << "Update hmac ..." <<>update(original);
   
  string tag = hmac->digest();
 
  hmac->init(sKey2);
  hmac->update(original);

  cout << "Verifying hmac tag ..." <<>verify(tag)) {
  cout << "Hmac failed" << keygen =" KeyGen::getInstance();" pkey1 =" keyGen-">generateDhKey(params);
  Key pKey2 = keyGen->generateDhKey(params);
  Key pKey3 = keyGen->generateDhKey(params);

  GDHKeyAgreement gdh1 = GDHKeyAgreement::getInstance();
  GDHKeyAgreement gdh2 = GDHKeyAgreement::getInstance();
  GDHKeyAgreement gdh3 = GDHKeyAgreement::getInstance();

  gdh1->init(pKey1, params);
  gdh2->init(pKey2, params);
  gdh3->init(pKey2, params);
  vector flow(1, params.getGenerator());
  
  flow = gdh1->computeFlow(flow, false);
  flow = gdh2->computeFlow(flow, false);
  flow = gdh3->computeFlow(flow, true);

  Key sKey1 = gdh1->computeKey(flow[0]);
  Key sKey2 = gdh2->computeKey(flow[1]);
  Key sKey3 = gdh3->computeKey(flow[2]);

  if (sKey1.getKey() != sKey2.getKey() || sKey1.getKey() != sKey2.getKey()) {
  cout << "WOW SECRET KEYS DIFFERS" << original = "BYE" i =" 0;" cipher =" Cipher::getInstance(">encryptInit(sKey1);

  cout << "Encrypting ..." << temp =" cipher-">update(original);
   
  temp += cipher->doFinal();

  cout << "Initializing cipher for decryption ..." <<>decryptInit(sKey2);

  cout << "Decrypting ..." << temp =" cipher-">update(temp);

  temp += cipher->doFinal();

  if (temp != original) {
  cout << "Encryption/Decryption failed" << hmac =" Hmac::getInstance(">init(sKey1);

  cout << "Update hmac ..." <<>update(original);
   
  string tag = hmac->digest();
 
  hmac->init(sKey2);
  hmac->update(original);

  cout << "Verifying hmac tag ..." <<>verify(tag)) {
  cout << "Hmac failed" << keygen =" KeyGen::getInstance();" key =" keyGen-">generateDesKey();

  cout << "GES_KEYSIZE:" << original = "BYE" i =" 0;" cipher =" Cipher::getInstance(" cipher =" Cipher::getInstance(" cipher =" Cipher::getNullCipher();" cipher =" Cipher::getInstance(" cipher =" Cipher::getInstance(">encryptInit(key);

  cout << "Encrypting ..." << temp =" cipher-">update(original);
   
  temp += cipher->doFinal();

  cout << "Initializing cipher for decryption ..." <<>decryptInit(key);

  cout << "Decrypting ..." << temp =" cipher-">update(temp);
 
  temp += cipher->doFinal();

  if (temp != original) {
  cout << "Encryption/Decryption failed" << hmac =" Hmac::getInstance(" hmac =" Hmac::getInstance(" hmac =" Hmac::getNullHmac();" hmac =" Hmac::getNullHmac();">init(key);

  cout << "Update hmac ..." <<>update(original);
   
  string tag = hmac->digest();
 
  hmac->init(key);
  hmac->update(original);

  cout << "Verifying hmac tag ..." <<>verify(tag)) {
  cout << "Hmac failed" <<>