summaryrefslogtreecommitdiffstats
path: root/kshowmail/kcmconfigs/encryption.cpp
blob: dd3bcec26360ff6b0ce59fa41c9397803671de07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// C++ Implementation: encryption
//
// Description:
//
//
// Author: Ulrich Weigelt <ulrich.weigelt@gmx.de>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "encryption.h"

//used in crypt() and decrypt()
static const char scramble1 [50] = "C6FDC7A1EDFBB6FEE3DBF5BEBAEFDDF7ABC6FDC7A1EDFBB6";
static const char hexstr [17] = "0123456789ABCDEF";

int Encryption::hexbyt( const char c )
{
  if( c >= '0' && c <= '9' )
    return c - '0';
  else
    return c - 'A' + 10;
}

const QString Encryption::crypt( const KURL& url )
{
  char result[50];
  char scramble2[50];
  QString hexresult;

  memset (result, 0, 50);
  memset (scramble2, 0, 50);
  int pos = url.pass().length () + 1;
  unsigned int free = 50 - pos;

  if( url.user().length() <= free )
  {
    strcpy( &scramble2[pos], url.user() );
    pos += url.user().length();
    free -= url.user().length();
  }
  else
  {
    memcpy( &scramble2[pos], url.user().latin1(), free );
    free = 0;
  }

  if( url.host().length() <= free )
  {
    strcpy( &scramble2[pos], url.host() );
    pos += url.host().length();
    free -= url.host().length();
  }
  else
  {
    memcpy( &scramble2[pos], url.host().latin1(), free );
    free = 0;
  }

  memcpy( result, url.pass().latin1(), url.pass().length() );
  for (int i = 0; i <= 31; i++)
  {
    result[i] = (char)( result[i] ^ ( scramble1[i] ^ scramble2[i] ) );
    hexresult += hexstr[ result[i] / 16 ];
    hexresult += hexstr[ result[i] % 16 ];
  }

  return hexresult;
}

const QString Encryption::decrypt( const QString& pass )
{
  char result[50];

  memset( result, 0, 50 );
  int i;
  for( i = 0; i <= 31; i++ )
  {
    result[i] = (char)hexbyt( pass[ i * 2 ] ) * 16 + hexbyt( pass[ i * 2 + 1 ] );
    result[i] = (char)( result[i] ^ scramble1[i] );
  }

  return result;
}